Skip to content
Permalink
Browse files

Reduce dependency on OS in determining if file is video.

Introduce magic.IsVideoFileName, add test for it.

Fixes this error:

--- FAIL: TestIsVideo (0.08s)
    search_test.go:41: IsVideo failed video=true filename=movie.mkv mimetype=application/octet-stream
FAIL
FAIL	perkeep.org/pkg/types/camtypes	0.399s

Discussion about this problem is here:
https://groups.google.com/forum/#!topic/perkeep/Wdb_0Os6ZkY

Change-Id: I14f54bd60c9495d6aa665036b83c813673608ad7
  • Loading branch information
ginabythebay committed Apr 2, 2019
1 parent 7c03aef commit 8e56f1b3055acec70c38803d3683c7da2e21429f
Showing with 25 additions and 6 deletions.
  1. +13 −5 internal/magic/magic.go
  2. +11 −0 internal/magic/magic_test.go
  3. +1 −1 pkg/types/camtypes/search.go
@@ -219,15 +219,23 @@ type errReader struct{ err error }

func (er errReader) Read([]byte) (int, error) { return 0, er.err }

// TODO(mpl): unexport VideoExtensions

// VideoExtensions are common video filename extensions that are not
// covered by mime.TypeByExtension.
var VideoExtensions = map[string]bool{
// videoExtensions are common video filename extensions that may not
// be covered by mime.TypeByExtension.
var videoExtensions = map[string]bool{
"3gp": true,
"avi": true,
"m1v": true,
"m2v": true,
"m4v": true,
"mkv": true,
"mov": true,
"mp4": true,
}

// IsVideoFileName reports whether the filename has an extension associated
// with a video file format.
func IsVideoFileName(filename string) bool {
return HasExtension(filename, videoExtensions)
}

// HasExtension returns whether the file extension of filename is among
@@ -92,6 +92,17 @@ func TestMagic(t *testing.T) {
}
}

func TestIsVideoFilename(t *testing.T) {
for _, tt := range tests {
want := strings.HasPrefix(tt.want, "video/")
got := IsVideoFileName(tt.fileName)
if got != want {
t.Errorf("IsVideoFileName(%q) = %t; want %t",
tt.fileName, got, want)
}
}
}

// runFileCmd runs the file utility and returns the mime-type and true
// or an empty string and false on any error. It also mimics the MIMEType
// behaviour.
@@ -137,7 +137,7 @@ func (fi *FileInfo) IsVideo() bool {
return true
}

if magic.HasExtension(fi.FileName, magic.VideoExtensions) {
if magic.IsVideoFileName(fi.FileName) {
return true
}

0 comments on commit 8e56f1b

Please sign in to comment.
You can’t perform that action at this time.