feat: more reorganisation and keys endpoint
This commit is contained in:
@@ -7,36 +7,14 @@ import (
|
||||
"orbits-server/internal/shared/utility"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 0: unspecified
|
||||
// 1: video
|
||||
// 2: presentation
|
||||
// 3: internet URL
|
||||
func CategorizeMediaType(ext string) (MediaType, bool) {
|
||||
|
||||
switch ext {
|
||||
case ".mp4", ".mov", ".avi", ".mkv", ".webm", ".m4a":
|
||||
return Video, true
|
||||
case ".pptx", ".ppt", ".key", ".odp":
|
||||
return Presentation, true
|
||||
default:
|
||||
slog.Debug("marking file as invalid due to its undefined extension")
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
|
||||
func GenerateSafeName(category MediaType, ext string) string {
|
||||
return uuid.New().String() + "_" + string(category) + ext
|
||||
}
|
||||
|
||||
// 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, ok := CategorizeMediaType(ext)
|
||||
category, ok := utility.CategorizeMediaType(ext)
|
||||
if !ok {
|
||||
return File{}, fmt.Errorf("unsupported filetype")
|
||||
}
|
||||
@@ -47,7 +25,7 @@ func BuildFileRecord(r io.Reader, origName string, contentDirectory string) (Fil
|
||||
return File{}, err
|
||||
}
|
||||
|
||||
safeName := GenerateSafeName(category, ext)
|
||||
safeName := utility.GenerateSafeName(category, ext)
|
||||
destPath := filepath.Join(contentDirectory, safeName)
|
||||
|
||||
fData := File{
|
||||
|
||||
Reference in New Issue
Block a user