RAW: Remove unwanted characters like quotes from file extensions #1362
This commit is contained in:
parent
c0eba718c9
commit
8eb970ae8b
7 changed files with 113 additions and 67 deletions
|
@ -31,8 +31,8 @@ func (b Blacklist) Contains(ext string) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
// Normalize extension string.
|
||||
ext = strings.ToLower(strings.Trim(ext, ".,;: "))
|
||||
// Remove unwanted characters from file extension and make it lowercase for comparison.
|
||||
ext = TrimExt(ext)
|
||||
|
||||
// Skip check if extension is empty.
|
||||
if ext == "" {
|
||||
|
@ -66,8 +66,8 @@ func (b Blacklist) Set(extensions string) {
|
|||
|
||||
// Add adds a file extension to the blacklist.
|
||||
func (b Blacklist) Add(ext string) {
|
||||
// Normalize extension string.
|
||||
ext = strings.ToLower(strings.Trim(ext, ".,;: "))
|
||||
// Remove unwanted characters from file extension and make it lowercase for comparison.
|
||||
ext = TrimExt(ext)
|
||||
|
||||
if ext == "" {
|
||||
return
|
||||
|
|
|
@ -32,6 +32,11 @@ func TestNewBlacklist(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestBlacklist_Ok(t *testing.T) {
|
||||
t.Run("CanonCR2", func(t *testing.T) {
|
||||
list := NewBlacklist("cr2")
|
||||
assert.False(t, list.Ok(".cr2"))
|
||||
assert.True(t, list.Contains(".cr2"))
|
||||
})
|
||||
t.Run("Raw", func(t *testing.T) {
|
||||
list := NewBlacklist("RAF, Cr3, aaf ")
|
||||
assert.False(t, list.Ok(".raf"))
|
||||
|
|
33
pkg/fs/ext.go
Normal file
33
pkg/fs/ext.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
package fs
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
YamlExt = ".yml"
|
||||
JpegExt = ".jpg"
|
||||
AvcExt = ".avc"
|
||||
FujiRawExt = ".raf"
|
||||
CanonCr3Ext = ".cr3"
|
||||
)
|
||||
|
||||
// FileExtensions maps file extensions to standard formats
|
||||
type FileExtensions map[string]FileFormat
|
||||
|
||||
// TypeExtensions maps standard formats to file extensions.
|
||||
type TypeExtensions map[FileFormat][]string
|
||||
|
||||
// NormalizeExt returns the file extension without dot and in lowercase.
|
||||
func NormalizeExt(fileName string) string {
|
||||
if dot := strings.LastIndex(fileName, "."); dot != -1 && len(fileName[dot+1:]) >= 1 {
|
||||
return strings.ToLower(fileName[dot+1:])
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// TrimExt removes unwanted characters from file extension strings, and makes it lowercase for comparison.
|
||||
func TrimExt(ext string) string {
|
||||
return strings.ToLower(strings.Trim(ext, " .,;:“”'`\""))
|
||||
}
|
70
pkg/fs/ext_test.go
Normal file
70
pkg/fs/ext_test.go
Normal file
|
@ -0,0 +1,70 @@
|
|||
package fs
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNormalizeExt(t *testing.T) {
|
||||
t.Run("none", func(t *testing.T) {
|
||||
result := NormalizeExt("testdata/test")
|
||||
assert.Equal(t, "", result)
|
||||
})
|
||||
|
||||
t.Run("dot", func(t *testing.T) {
|
||||
result := NormalizeExt("testdata/test.")
|
||||
assert.Equal(t, "", result)
|
||||
})
|
||||
|
||||
t.Run("test.z", func(t *testing.T) {
|
||||
result := NormalizeExt("testdata/test.z")
|
||||
assert.Equal(t, "z", result)
|
||||
})
|
||||
|
||||
t.Run("test.jpg", func(t *testing.T) {
|
||||
result := NormalizeExt("testdata/test.jpg")
|
||||
assert.Equal(t, "jpg", result)
|
||||
})
|
||||
|
||||
t.Run("test.PNG", func(t *testing.T) {
|
||||
result := NormalizeExt("testdata/test.PNG")
|
||||
assert.Equal(t, "png", result)
|
||||
})
|
||||
|
||||
t.Run("test.MOV", func(t *testing.T) {
|
||||
result := NormalizeExt("testdata/test.MOV")
|
||||
assert.Equal(t, "mov", result)
|
||||
})
|
||||
|
||||
t.Run("test.xmp", func(t *testing.T) {
|
||||
result := NormalizeExt("testdata/test.xMp")
|
||||
assert.Equal(t, "xmp", result)
|
||||
})
|
||||
|
||||
t.Run("test.MP", func(t *testing.T) {
|
||||
result := NormalizeExt("testdata/test.mp")
|
||||
assert.Equal(t, "mp", result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestTrimExt(t *testing.T) {
|
||||
t.Run("WithDot", func(t *testing.T) {
|
||||
assert.Equal(t, "raf", TrimExt(".raf"))
|
||||
})
|
||||
t.Run("Normalized", func(t *testing.T) {
|
||||
assert.Equal(t, "cr3", TrimExt("cr3"))
|
||||
})
|
||||
t.Run("Uppercase", func(t *testing.T) {
|
||||
assert.Equal(t, "aaf", TrimExt("AAF"))
|
||||
})
|
||||
t.Run("Empty", func(t *testing.T) {
|
||||
assert.Equal(t, "", TrimExt(""))
|
||||
})
|
||||
t.Run("MixedCaseWithDot", func(t *testing.T) {
|
||||
assert.Equal(t, "raw", TrimExt(".Raw"))
|
||||
})
|
||||
t.Run("TypographicQuotes", func(t *testing.T) {
|
||||
assert.Equal(t, "jpeg", TrimExt(" “JPEG” "))
|
||||
})
|
||||
}
|
|
@ -45,17 +45,6 @@ const (
|
|||
FormatOther FileFormat = "" // Unknown file format.
|
||||
)
|
||||
|
||||
type FileExtensions map[string]FileFormat
|
||||
type TypeExtensions map[FileFormat][]string
|
||||
|
||||
const (
|
||||
YamlExt = ".yml"
|
||||
JpegExt = ".jpg"
|
||||
AvcExt = ".avc"
|
||||
FujiRawExt = ".raf"
|
||||
CanonCr3Ext = ".cr3"
|
||||
)
|
||||
|
||||
// FileExt contains the filename extensions of file formats known to PhotoPrism.
|
||||
var FileExt = FileExtensions{
|
||||
".bmp": FormatBitmap,
|
||||
|
@ -330,12 +319,3 @@ func (t FileFormat) FindAll(fileName string, dirs []string, baseDir string, stri
|
|||
|
||||
return results
|
||||
}
|
||||
|
||||
// NormalizedExt returns the file extension without dot and in lowercase.
|
||||
func NormalizedExt(fileName string) string {
|
||||
if dot := strings.LastIndex(fileName, "."); dot != -1 && len(fileName[dot+1:]) >= 1 {
|
||||
return strings.ToLower(fileName[dot+1:])
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
|
|
@ -143,48 +143,6 @@ func TestFileType_FindAll(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestNormalizedExt(t *testing.T) {
|
||||
t.Run("none", func(t *testing.T) {
|
||||
result := NormalizedExt("testdata/test")
|
||||
assert.Equal(t, "", result)
|
||||
})
|
||||
|
||||
t.Run("dot", func(t *testing.T) {
|
||||
result := NormalizedExt("testdata/test.")
|
||||
assert.Equal(t, "", result)
|
||||
})
|
||||
|
||||
t.Run("test.z", func(t *testing.T) {
|
||||
result := NormalizedExt("testdata/test.z")
|
||||
assert.Equal(t, "z", result)
|
||||
})
|
||||
|
||||
t.Run("test.jpg", func(t *testing.T) {
|
||||
result := NormalizedExt("testdata/test.jpg")
|
||||
assert.Equal(t, "jpg", result)
|
||||
})
|
||||
|
||||
t.Run("test.PNG", func(t *testing.T) {
|
||||
result := NormalizedExt("testdata/test.PNG")
|
||||
assert.Equal(t, "png", result)
|
||||
})
|
||||
|
||||
t.Run("test.MOV", func(t *testing.T) {
|
||||
result := NormalizedExt("testdata/test.MOV")
|
||||
assert.Equal(t, "mov", result)
|
||||
})
|
||||
|
||||
t.Run("test.xmp", func(t *testing.T) {
|
||||
result := NormalizedExt("testdata/test.xMp")
|
||||
assert.Equal(t, "xmp", result)
|
||||
})
|
||||
|
||||
t.Run("test.MP", func(t *testing.T) {
|
||||
result := NormalizedExt("testdata/test.mp")
|
||||
assert.Equal(t, "mp", result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestFileExt(t *testing.T) {
|
||||
t.Run("mp", func(t *testing.T) {
|
||||
assert.True(t, FileExt.Known("file.mp"))
|
||||
|
|
|
@ -32,7 +32,7 @@ func MimeType(filename string) string {
|
|||
return ""
|
||||
} else if t, err := filetype.Get(buffer); err == nil && t != filetype.Unknown {
|
||||
return t.MIME.Value
|
||||
} else if t := filetype.GetType(NormalizedExt(filename)); t != filetype.Unknown {
|
||||
} else if t := filetype.GetType(NormalizeExt(filename)); t != filetype.Unknown {
|
||||
return t.MIME.Value
|
||||
} else {
|
||||
return ""
|
||||
|
|
Loading…
Reference in a new issue