feat: more reorganisation and keys endpoint
This commit is contained in:
@@ -2,6 +2,7 @@ package database
|
||||
|
||||
import (
|
||||
"orbits-server/internal/server/bootstrap"
|
||||
"orbits-server/internal/shared/utility"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
@@ -35,7 +36,7 @@ func KickoffDatabase(workDir string) (*gorm.DB, error) {
|
||||
if err := db.FirstOrCreate(&Command{}, Command{
|
||||
ID: 0,
|
||||
State: "idle",
|
||||
MediaType: Unspecified,
|
||||
MediaType: utility.Unspecified,
|
||||
}).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -1,18 +1,10 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"orbits-server/internal/shared/utility"
|
||||
"time"
|
||||
)
|
||||
|
||||
type MediaType string
|
||||
|
||||
const (
|
||||
Video MediaType = "video"
|
||||
Presentation MediaType = "presentation"
|
||||
Internet MediaType = "internet"
|
||||
Unspecified MediaType = "unspecified"
|
||||
)
|
||||
|
||||
type Timestamps struct {
|
||||
CreatedAt time.Time `gorm:"not null;"`
|
||||
UpdatedAt time.Time `gorm:"not null;"`
|
||||
@@ -26,10 +18,10 @@ type Command struct {
|
||||
// video
|
||||
// presentation
|
||||
// internet URL
|
||||
MediaType MediaType `gorm:"type:varchar(20);not null"` // Must specify what kind of file it is
|
||||
MediaType utility.MediaType `gorm:"type:varchar(20);not null;"` // Must specify what kind of file it is
|
||||
// Must be target list who are compelled to listen to the command
|
||||
// can be none when there is no targets specified (init stage)
|
||||
Targets []string `gorm:"type:json"`
|
||||
Targets []string `gorm:"type:json;"`
|
||||
// Must be the location where the file is downloadable on the API
|
||||
// can be none when there is no media specified (init stage)
|
||||
Location string
|
||||
@@ -64,7 +56,7 @@ type File struct {
|
||||
// video
|
||||
// presentation
|
||||
// internet URL
|
||||
MediaType MediaType `gorm:"type:varchar(20);not null;"`
|
||||
MediaType utility.MediaType `gorm:"type:varchar(20);not null;"`
|
||||
// the name given by the user
|
||||
MetaName string
|
||||
FileName string `gorm:"not null;"`
|
||||
|
||||
@@ -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