From ce89e24ec9851f24f2d4ffcbf033fc8b6eba5587 Mon Sep 17 00:00:00 2001 From: Seednode Date: Mon, 11 Sep 2023 10:50:34 -0500 Subject: [PATCH] Moved fileType() function into types.go --- cmd/files.go | 29 ----------------------------- cmd/types.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/cmd/files.go b/cmd/files.go index a28da30..fd55a0d 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -25,8 +25,6 @@ import ( _ "golang.org/x/image/bmp" _ "golang.org/x/image/webp" - - "github.com/h2non/filetype" ) type maxConcurrency int @@ -369,33 +367,6 @@ func pathIsValid(filePath string, paths []string) bool { } } -func fileType(path string, types *SupportedTypes) (bool, *SupportedType, string, error) { - file, err := os.Open(path) - switch { - case errors.Is(err, os.ErrNotExist): - return false, nil, "", nil - case err != nil: - return false, nil, "", err - } - defer file.Close() - - head := make([]byte, 261) - file.Read(head) - - extension := filepath.Ext(path) - - fileType := types.Type(extension) - - isSupported := types.IsSupported(head) - if !isSupported { - return false, nil, "", nil - } - - mimeType := (filetype.GetType(strings.TrimPrefix(extension, "."))).MIME.Value - - return isSupported, fileType, mimeType, nil -} - func pathHasSupportedFiles(path string, types *SupportedTypes) (bool, error) { hasSupportedFiles := make(chan bool, 1) diff --git a/cmd/types.go b/cmd/types.go index 2e15ff7..50b0dc8 100644 --- a/cmd/types.go +++ b/cmd/types.go @@ -4,6 +4,15 @@ Copyright © 2023 Seednode package cmd +import ( + "errors" + "os" + "path/filepath" + "strings" + + "github.com/h2non/filetype" +) + type SupportedType struct { title func(queryParams, filePath, mime, fileName string, width, height int) string body func(queryParams, filePath, mime, fileName string, width, height int) string @@ -56,3 +65,30 @@ func (s *SupportedTypes) IsSupported(head []byte) bool { return r } + +func fileType(path string, types *SupportedTypes) (bool, *SupportedType, string, error) { + file, err := os.Open(path) + switch { + case errors.Is(err, os.ErrNotExist): + return false, nil, "", nil + case err != nil: + return false, nil, "", err + } + defer file.Close() + + head := make([]byte, 261) + file.Read(head) + + extension := filepath.Ext(path) + + fileType := types.Type(extension) + + isSupported := types.IsSupported(head) + if !isSupported { + return false, nil, "", nil + } + + mimeType := (filetype.GetType(strings.TrimPrefix(extension, "."))).MIME.Value + + return isSupported, fileType, mimeType, nil +}