(Feat): Initial Commit
This commit is contained in:
33
internal/util/mime/mime.go
Normal file
33
internal/util/mime/mime.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package mime
|
||||
|
||||
import "github.com/gabriel-vasile/mimetype"
|
||||
|
||||
var _ MIME = (*mimetype.MIME)(nil)
|
||||
|
||||
// MIME interface enables to use either *mimetype.MIME or KnownMIME as mimetype.
|
||||
type MIME interface {
|
||||
String() string
|
||||
Extension() string
|
||||
Is(expectedMIME string) bool
|
||||
}
|
||||
|
||||
// KnownMIME implements the MIME interface to be able to pass a *mimetype.MIME
|
||||
// compatible value if the mimetype is already known so mimetype detection is not
|
||||
// needed. It is therefore possible to skip mimetype detection if the mimetype is known
|
||||
// or it is not possible to use a readSeeker but a mimetype is required.
|
||||
type KnownMIME struct {
|
||||
MimeType string
|
||||
FileExtension string
|
||||
}
|
||||
|
||||
func (m *KnownMIME) String() string {
|
||||
return m.MimeType
|
||||
}
|
||||
|
||||
func (m *KnownMIME) Extension() string {
|
||||
return m.FileExtension
|
||||
}
|
||||
|
||||
func (m *KnownMIME) Is(expectedMIME string) bool {
|
||||
return expectedMIME == m.MimeType
|
||||
}
|
||||
30
internal/util/mime/mime_test.go
Normal file
30
internal/util/mime/mime_test.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package mime_test
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"allaboutapps.dev/aw/go-starter/internal/util"
|
||||
"allaboutapps.dev/aw/go-starter/internal/util/mime"
|
||||
"github.com/gabriel-vasile/mimetype"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestKnownMIME(t *testing.T) {
|
||||
filePath := filepath.Join(util.GetProjectRootDir(), "test", "testdata", "example.jpg")
|
||||
|
||||
var detectedMIME mime.MIME
|
||||
var err error
|
||||
detectedMIME, err = mimetype.DetectFile(filePath)
|
||||
require.NoError(t, err)
|
||||
|
||||
var knownMIME mime.MIME = &mime.KnownMIME{
|
||||
MimeType: "image/jpeg",
|
||||
FileExtension: ".jpg",
|
||||
}
|
||||
|
||||
assert.Equal(t, detectedMIME.Extension(), knownMIME.Extension())
|
||||
assert.Equal(t, detectedMIME.String(), knownMIME.String())
|
||||
assert.True(t, knownMIME.Is(detectedMIME.String()))
|
||||
}
|
||||
Reference in New Issue
Block a user