feat: init commit
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"eden-server/internal/database"
|
||||
"eden-server/internal/runtime"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 0: unspecified
|
||||
// 1: video
|
||||
// 2: presentation
|
||||
// 3: internet URL
|
||||
func categorizeFile(ext string) string {
|
||||
switch ext {
|
||||
case ".mp4", ".mov", ".avi", ".mkv", ".webm", ".m4a":
|
||||
return "video"
|
||||
case ".pptx", ".ppt", ".key", ".odp":
|
||||
return "presentation"
|
||||
default:
|
||||
return "unspecified"
|
||||
}
|
||||
}
|
||||
|
||||
func spawnRoutes(r *gin.Engine, env runtime.Environment, db *gorm.DB) {
|
||||
// The Root endpoint '/' displays a simple HTML template.
|
||||
// from root: ./templates/index.html
|
||||
r.GET("/", func(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "index.html",
|
||||
gin.H{
|
||||
"title": env.Codename,
|
||||
"version": env.Version,
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
// prefix: api
|
||||
// Display the information on what is going on at the moment
|
||||
r.GET("/api/status", func(c *gin.Context) {
|
||||
state, err := database.GetAppState(db)
|
||||
if err != nil {
|
||||
slog.Warn("unable to determine state")
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, state)
|
||||
})
|
||||
|
||||
// define the upload route
|
||||
r.POST("/api/upload", func(c *gin.Context) {
|
||||
file, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
slog.Error("missing file upload", "error", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"msg": "file is required"})
|
||||
return
|
||||
}
|
||||
|
||||
ext := filepath.Ext(file.Filename)
|
||||
catName := categorizeFile(ext)
|
||||
safeName := uuid.New().String()[:8] + "_" + catName + ext
|
||||
destPath := filepath.Join(env.WorkDir, "content", "fresh", safeName)
|
||||
|
||||
if err := c.SaveUploadedFile(file, destPath); err != nil {
|
||||
slog.Error("failed to receive the file over http:", "error", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"msg": "internal error"})
|
||||
} else {
|
||||
database.RegisterFile(db, catName, destPath)
|
||||
c.JSON(http.StatusCreated, gin.H{"msg": "file has succesfully been uploaded"})
|
||||
}
|
||||
})
|
||||
|
||||
// define a route to check what is registered
|
||||
r.GET("/api/available", func(c *gin.Context) {
|
||||
files, err := database.GetFiles(db)
|
||||
if err != nil {
|
||||
slog.Error("failed to retrieve available files", "error", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"msg": "internal error"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, files)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user