feat: add locally syncing and watchdog
This commit is contained in:
@@ -1,13 +1,74 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"eden-server/internal/utility"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"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 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)
|
||||
if !ok {
|
||||
return File{}, fmt.Errorf("unsupported filetype")
|
||||
}
|
||||
|
||||
checksum, err := utility.HashReader(r)
|
||||
if err != nil {
|
||||
slog.Error("failed to calculate hash of file at given path", "error", err)
|
||||
return File{}, err
|
||||
}
|
||||
|
||||
safeName := GenerateSafeName(category, ext)
|
||||
destPath := filepath.Join(contentDirectory, safeName)
|
||||
|
||||
fData := File{
|
||||
MediaType: category,
|
||||
MetaName: origName,
|
||||
FileName: safeName,
|
||||
FilePath: destPath,
|
||||
Checksum: checksum,
|
||||
}
|
||||
|
||||
return fData, nil
|
||||
}
|
||||
|
||||
func GetState(db *gorm.DB) (State, error) {
|
||||
var state State
|
||||
|
||||
return state, db.First(&state).Error
|
||||
if err := db.First(&state).Error; err != nil {
|
||||
return State{}, err
|
||||
}
|
||||
|
||||
return state, nil
|
||||
}
|
||||
|
||||
func GetFiles(db *gorm.DB) ([]File, error) {
|
||||
|
||||
Reference in New Issue
Block a user