Moved fileType() function into types.go

This commit is contained in:
Seednode 2023-09-11 10:50:34 -05:00
parent e8ee96c7d6
commit ce89e24ec9
2 changed files with 36 additions and 29 deletions

View File

@ -25,8 +25,6 @@ import (
_ "golang.org/x/image/bmp" _ "golang.org/x/image/bmp"
_ "golang.org/x/image/webp" _ "golang.org/x/image/webp"
"github.com/h2non/filetype"
) )
type maxConcurrency int 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) { func pathHasSupportedFiles(path string, types *SupportedTypes) (bool, error) {
hasSupportedFiles := make(chan bool, 1) hasSupportedFiles := make(chan bool, 1)

View File

@ -4,6 +4,15 @@ Copyright © 2023 Seednode <seednode@seedno.de>
package cmd package cmd
import (
"errors"
"os"
"path/filepath"
"strings"
"github.com/h2non/filetype"
)
type SupportedType struct { type SupportedType struct {
title func(queryParams, filePath, mime, fileName string, width, height int) string title func(queryParams, filePath, mime, fileName string, width, height int) string
body 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 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
}