39 lines
930 B
Go
39 lines
930 B
Go
package database
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"log/slog"
|
|
"orbits-server/internal/shared/utility"
|
|
"path/filepath"
|
|
)
|
|
|
|
// it has been made more general for DRY purposes
|
|
// this function should only be called after manually checking the filetype
|
|
func BuildFileRecord(r io.Reader, origName string, contentDirectory string) (File, error) {
|
|
ext := filepath.Ext(origName)
|
|
category := utility.CategorizeMediaType(ext)
|
|
if category == utility.Unspecified {
|
|
return File{}, fmt.Errorf("unsupported filetype")
|
|
}
|
|
|
|
checksum, err := utility.GenerateHashFromReader(r)
|
|
if err != nil {
|
|
slog.Error("failed to calculate hash of file at given path", "error", err)
|
|
return File{}, err
|
|
}
|
|
|
|
safeName := utility.GenerateSafeName(category, ext)
|
|
destPath := filepath.Join(contentDirectory, safeName)
|
|
|
|
f := File{
|
|
MediaType: category,
|
|
MetaName: origName,
|
|
FileName: safeName,
|
|
FilePath: destPath,
|
|
Checksum: checksum,
|
|
}
|
|
|
|
return f, nil
|
|
}
|