From e1f5308dd3e7fda793ed8e174dfe21c0522d01771a94eb96af4d7ba02607a31c Mon Sep 17 00:00:00 2001 From: DaanSelen Date: Tue, 21 Apr 2026 16:59:37 +0200 Subject: [PATCH] chore: fix a small issue with video modes --- cmd/server/main.go | 2 +- internal/api/routes_api.go | 8 ++++---- internal/api/routes_file.go | 4 ++-- internal/database/database.go | 7 +++++-- internal/database/define.go | 16 ++++++++-------- internal/database/watchdog.go | 9 ++++++++- 6 files changed, 28 insertions(+), 18 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index 2f4ba4f..8a8602c 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -30,8 +30,8 @@ func main() { db, err := database.KickoffDatabase(env.DataDirectory) if err != nil { slog.Error("failed to initiate a database connection") + os.Exit(1) } - slog.Info("kicking off database watchdog", "watch_interval", env.WatchInterval) database.KickoffDatabaseWatchdog(env, db) diff --git a/internal/api/routes_api.go b/internal/api/routes_api.go index a4cfba0..9198bf1 100644 --- a/internal/api/routes_api.go +++ b/internal/api/routes_api.go @@ -13,14 +13,14 @@ import ( // 1: video // 2: presentation // 3: internet URL -func categorizeFilemode(ext string) database.Mode { +func categorizeFilemode(ext string) database.MediaType { switch ext { case ".mp4", ".mov", ".avi", ".mkv", ".webm", ".m4a": - return database.ModeVideo + return database.Video case ".pptx", ".ppt", ".key", ".odp": - return database.ModePresentation + return database.Presentation default: - return database.ModeUnspecified + return database.Unspecified } } diff --git a/internal/api/routes_file.go b/internal/api/routes_file.go index a639106..c82a08c 100644 --- a/internal/api/routes_file.go +++ b/internal/api/routes_file.go @@ -37,7 +37,7 @@ func spawnFileRoutes(file *gin.RouterGroup, env runtime.Environment, db *gorm.DB e := filepath.Ext(f.Filename) m := categorizeFilemode(e) - if m == database.ModeUnspecified { + if m == database.Unspecified { slog.Warn("discarding file since its filetype is unsupported") c.JSON(http.StatusUnsupportedMediaType, RespObj{ Msg: "unsupported filetype", @@ -66,7 +66,7 @@ func spawnFileRoutes(file *gin.RouterGroup, env runtime.Environment, db *gorm.DB log.Println(cSum) fData := database.File{ - Mode: m, + MediaType: m, GivenName: f.Filename, Filepath: destPath, Checksum: cSum, diff --git a/internal/database/database.go b/internal/database/database.go index 844ae4e..774a165 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -21,14 +21,17 @@ func KickoffDatabase(workDir string) (*gorm.DB, error) { if err := db.AutoMigrate(&State{}); err != nil { return nil, err } + if err := db.AutoMigrate(&Device{}); err != nil { + return nil, err + } if err := db.AutoMigrate(&File{}); err != nil { return nil, err } // create the first row if it does not exist yet if err := db.FirstOrCreate(&State{}, State{ - ID: 0, - Mode: "unspecified", + ID: 0, + MediaType: "unspecified", }).Error; err != nil { return nil, err } diff --git a/internal/database/define.go b/internal/database/define.go index e754392..91bf5fa 100644 --- a/internal/database/define.go +++ b/internal/database/define.go @@ -6,13 +6,13 @@ import ( "gorm.io/datatypes" ) -type Mode string +type MediaType string const ( - ModeUnspecified Mode = "unspecified" - ModeVideo Mode = "video" - ModePresentation Mode = "presentation" - ModeInternet Mode = "internet" + Unspecified MediaType = "unspecified" + Video MediaType = "video" + Presentation MediaType = "presentation" + Internet MediaType = "internet" ) type State struct { @@ -21,7 +21,7 @@ type State struct { // video // presentation // internet URL - Mode Mode `gorm:"type:varchar(20);not null"` // Must specify what kind of file it is + MediaType MediaType `gorm:"type:varchar(20);not null"` // Must specify what kind of file it is Targets datatypes.JSON Location string // Must be the location where the file is downloadable on the API UpdatedAt time.Time @@ -39,8 +39,8 @@ type Device struct { } type File struct { - ID int `gorm:"primaryKey"` - Mode Mode `gorm:"type:varchar(20);not null"` + ID int `gorm:"primaryKey"` + MediaType MediaType `gorm:"type:varchar(20);not null;"` GivenName string Filepath string Checksum string // base64 encoded sha512 checksum diff --git a/internal/database/watchdog.go b/internal/database/watchdog.go index ce4fdee..fadbd3f 100644 --- a/internal/database/watchdog.go +++ b/internal/database/watchdog.go @@ -3,6 +3,7 @@ package database import ( "log" "log/slog" + "os" "gorm.io/gorm" ) @@ -16,5 +17,11 @@ func watchdog(w string, db *gorm.DB) { return } - log.Println(files) + for _, f := range files { + i, err := os.Stat(f.Filepath) + if err != nil { + slog.Error("failed to stat the details for one or more files", "error", err) + } + log.Println(i) + } }