scanfile/server/internal/processing/basic/libmagic.go

55 lines
1.7 KiB
Go

package basic
import (
"log/slog"
"os/exec"
"strings"
"git.jmbit.de/jmb/scanfile/server/internal/store"
)
type FileCmdResult struct {
Type string
MimeType string
Apple string
Extension string
}
//FileCmd() runs "/usr/bin/file" on the object. Should be replaced with libmagic bindings instead
func FileCmd(fileName string) (FileCmdResult, error) {
var returnStruct FileCmdResult
filepath, err := store.AbsPath(fileName)
if err != nil {
return returnStruct, err
}
cmd := exec.Command("/usr/bin/file", "-b", filepath)
result, err := cmd.Output()
if err != nil {
slog.Error("Error running file command", "file-uuid", fileName, "error", err)
return returnStruct, err
}
returnStruct.Type = strings.TrimRight(string(result), "\n ")
cmd = exec.Command("/usr/bin/file", "-b", "--mime-type", filepath)
result, err = cmd.Output()
if err != nil {
slog.Error("Error running file (mime-type) command", "file-uuid", fileName, "error", err)
return returnStruct, err
}
returnStruct.MimeType = strings.TrimRight(string(result), "\n ")
cmd = exec.Command("/usr/bin/file", "-b", "--apple", filepath)
result, err = cmd.Output()
if err != nil {
slog.Error("Error running file (apple) command", "file-uuid", fileName, "error", err)
return returnStruct, err
}
returnStruct.Apple = strings.TrimRight(string(result), "\n ")
cmd = exec.Command("/usr/bin/file", "-b", "--extension", filepath)
result, err = cmd.Output()
if err != nil {
slog.Error("Error running file (extension) command", "file-uuid", fileName, "error", err)
return returnStruct, err
}
returnStruct.Extension = strings.TrimRight(string(result), "\n ")
return returnStruct, nil
}