Refactor file type functions
This commit is contained in:
@@ -1,23 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"mime"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var excludeAudioType = []string{
|
||||
"audio/x-mpegurl",
|
||||
"audio/x-scpls",
|
||||
}
|
||||
|
||||
func IsAudioFile(filePath string) bool {
|
||||
extension := filepath.Ext(filePath)
|
||||
mimeType := mime.TypeByExtension(extension)
|
||||
return !StringInSlice(mimeType, excludeAudioType) && strings.HasPrefix(mimeType, "audio/")
|
||||
}
|
||||
|
||||
func IsImageFile(filePath string) bool {
|
||||
extension := filepath.Ext(filePath)
|
||||
return strings.HasPrefix(mime.TypeByExtension(extension), "image/")
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Files", func() {
|
||||
Describe("IsAudioFile", func() {
|
||||
It("returns true for a MP3 file", func() {
|
||||
Expect(IsAudioFile(filepath.Join("path", "to", "test.mp3"))).To(BeTrue())
|
||||
})
|
||||
|
||||
It("returns true for a FLAC file", func() {
|
||||
Expect(IsAudioFile("test.flac")).To(BeTrue())
|
||||
})
|
||||
|
||||
It("returns false for a non-audio file", func() {
|
||||
Expect(IsAudioFile("test.jpg")).To(BeFalse())
|
||||
})
|
||||
|
||||
It("returns false for m3u files", func() {
|
||||
Expect(IsAudioFile("test.m3u")).To(BeFalse())
|
||||
})
|
||||
|
||||
It("returns false for pls files", func() {
|
||||
Expect(IsAudioFile("test.pls")).To(BeFalse())
|
||||
})
|
||||
})
|
||||
|
||||
Describe("IsImageFile", func() {
|
||||
It("returns true for a PNG file", func() {
|
||||
Expect(IsImageFile(filepath.Join("path", "to", "test.png"))).To(BeTrue())
|
||||
})
|
||||
|
||||
It("returns true for a JPEG file", func() {
|
||||
Expect(IsImageFile("test.JPEG")).To(BeTrue())
|
||||
})
|
||||
|
||||
It("returns false for a non-image file", func() {
|
||||
Expect(IsImageFile("test.mp3")).To(BeFalse())
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -17,15 +17,6 @@ func NoArticle(name string) string {
|
||||
return name
|
||||
}
|
||||
|
||||
func StringInSlice(a string, slice []string) bool {
|
||||
for _, b := range slice {
|
||||
if b == a {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func InsertString(slice []string, value string, index int) []string {
|
||||
return append(slice[:index], append([]string{value}, slice[index:]...)...)
|
||||
}
|
||||
|
||||
@@ -35,20 +35,6 @@ var _ = Describe("Strings", func() {
|
||||
})
|
||||
})
|
||||
|
||||
Describe("StringInSlice", func() {
|
||||
It("returns false if slice is empty", func() {
|
||||
Expect(StringInSlice("test", nil)).To(BeFalse())
|
||||
})
|
||||
|
||||
It("returns false if string is not found in slice", func() {
|
||||
Expect(StringInSlice("aaa", []string{"bbb", "ccc"})).To(BeFalse())
|
||||
})
|
||||
|
||||
It("returns true if string is found in slice", func() {
|
||||
Expect(StringInSlice("bbb", []string{"bbb", "aaa", "ccc"})).To(BeTrue())
|
||||
})
|
||||
})
|
||||
|
||||
Describe("MoveString", func() {
|
||||
It("moves item to end of slice", func() {
|
||||
Expect(MoveString([]string{"1", "2", "3"}, 0, 2)).To(ConsistOf("2", "3", "1"))
|
||||
|
||||
Reference in New Issue
Block a user