Skip to content

Commit

Permalink
fix: case-insensitive ext check (#2383)
Browse files Browse the repository at this point in the history
  • Loading branch information
rgmz authored Feb 6, 2024
1 parent 901c851 commit 8104611
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 47 deletions.
105 changes: 61 additions & 44 deletions pkg/common/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,65 @@ import (

var (
KB, MB, GB, TB, PB = 1e3, 1e6, 1e9, 1e12, 1e15
IgnoredExtensions = []string{
// multimedia/containers
"mp4",
"avi",
"mpeg",
"mpg",
"mov",
"wmv",
"m4p",
"swf",
"mp2",
"flv",
"vob",
"webm",
"hdv",
"3gp",
"ogg",
"mp3",
"wav",
"flac",
"webp",
"pdf",

ignoredExtensions = map[string]struct{}{
// images
"png",
"jpg",
"jpeg",
"gif",
"tiff",
"apng": {},
"avif": {},
"bmp": {},
"gif": {},
"icns": {}, // Apple icon image file
"ico": {}, // Icon file
"jpg": {},
"jpeg": {},
"png": {},
"svg": {},
"svgz": {}, // Compressed Scalable Vector Graphics file
"tga": {},
"tif": {},
"tiff": {},

// audio
"fev": {}, // video game audio
"fsb": {},
"m2a": {},
"m4a": {},
"mp2": {},
"mp3": {},
"snag": {},

// video
"264": {},
"3gp": {},
"avi": {},
"flac": {},
"flv": {},
"hdv": {},
"m4p": {},
"mov": {},
"mp4": {},
"mpg": {},
"mpeg": {},
"ogg": {},
"qt": {},
"swf": {},
"vob": {},
"wav": {},
"webm": {},
"webp": {},
"wmv": {},

"fnt", // Windows font file
"fon", // Generic font file
"ttf", // TrueType font
"otf", // OpenType font
"woff", // Web Open Font Format
"woff2", // Web Open Font Format 2
"eot", // Embedded OpenType font
"svgz", // Compressed Scalable Vector Graphics file
"icns", // Apple icon image file
"ico", // Icon file
// documents
"pdf": {},
"psd": {},

// fonts
"eot": {}, // Embedded OpenType font
"fnt": {}, // Windows font file
"fon": {}, // Generic font file
"otf": {}, // OpenType font
"ttf": {}, // TrueType font
"woff": {}, // Web Open Font Format
"woff2": {}, // Web Open Font Format 2
}

binaryExtensions = map[string]struct{}{
Expand Down Expand Up @@ -86,13 +105,11 @@ var (
}
)

// SkipFile returns true if the file extension is in the ignoredExtensions list.
func SkipFile(filename string) bool {
for _, ext := range IgnoredExtensions {
if strings.TrimPrefix(filepath.Ext(filename), ".") == ext {
return true
}
}
return false
ext := strings.ToLower(strings.TrimPrefix(filepath.Ext(filename), "."))
_, ok := ignoredExtensions[ext]
return ok
}

// IsBinary returns true if the file extension is in the binaryExtensions list.
Expand Down
14 changes: 11 additions & 3 deletions pkg/common/vars_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package common

import "testing"
import (
"strings"
"testing"
)

func TestSkipFile(t *testing.T) {
type testCase struct {
Expand All @@ -9,12 +12,17 @@ func TestSkipFile(t *testing.T) {
}

// Add a test case for each ignored extension.
testCases := make([]testCase, 0, len(IgnoredExtensions)+1)
for _, ext := range IgnoredExtensions {
testCases := make([]testCase, 0, (len(ignoredExtensions)+1)*2)
for ext := range ignoredExtensions {
testCases = append(testCases, testCase{
file: "test." + ext,
want: true,
})

testCases = append(testCases, testCase{
file: "test." + strings.ToUpper(ext),
want: true,
})
}

// Add a test case for a file that should not be skipped.
Expand Down

0 comments on commit 8104611

Please # to comment.