made filetype wrapper easier to test

This commit is contained in:
Johannes Bülow 2025-06-12 12:57:51 +02:00
parent 5b0f04c241
commit 8dc19d23ad
Signed by: jmb
GPG key ID: B56971CF7B8F83A6

View file

@ -21,22 +21,14 @@ func GetFileType(fileId string) (string, error) {
// We only have to pass the file header = first 261 bytes // We only have to pass the file header = first 261 bytes
head := make([]byte, 261) head := make([]byte, 261)
file.Read(head) file.Read(head)
kind, err := filetype.Match(head) return GetBytesFileType(head)
if err != nil {
slog.Error("Could not determine file type", "error", err, "fileid", fileId)
return "application/octet-stream", err
}
if kind == filetype.Unknown {
return "application/octet-stream", nil
}
return kind.MIME.Value, nil
} }
// Returns the MimeType for a []byte // Returns the MimeType for a []byte
// We only have to pass the file header = first 261 bytes // We only have to pass the file header = first 261 bytes
func GetBytesFileType(data []byte) (string, error) { func GetBytesFileType(data []byte) (string, error) {
kind, err := filetype.Match(data) kind, err := filetype.Match(data)
slog.Debug("GetBytesFileType", "data", data, "file-mime", kind.MIME.Value)
if err != nil { if err != nil {
slog.Error("Could not determine file type", "error", err) slog.Error("Could not determine file type", "error", err)
return "application/octet-stream", err return "application/octet-stream", err