101 lines
2.4 KiB
Go
101 lines
2.4 KiB
Go
package api
|
|
|
|
import (
|
|
"eden-server/internal/crypto"
|
|
"eden-server/internal/database"
|
|
"eden-server/internal/utility"
|
|
"errors"
|
|
"log/slog"
|
|
"net/http"
|
|
"path/filepath"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func spawnFileRoutes(file *gin.RouterGroup, env utility.Environment, db *gorm.DB) {
|
|
// /file/<file-name>
|
|
file.GET("/:filename", func(c *gin.Context) {
|
|
f := c.Param("filename")
|
|
p := filepath.Join(env.DataDirectory, "content", f)
|
|
|
|
c.File(p)
|
|
})
|
|
|
|
// define the upload route
|
|
// /file/upload
|
|
file.POST("/upload", func(c *gin.Context) {
|
|
f, err := c.FormFile("file")
|
|
if err != nil {
|
|
slog.Debug("no file or file headers provided on the request", "error", err)
|
|
c.JSON(http.StatusBadRequest, RespObj{
|
|
Msg: "a file is required",
|
|
})
|
|
return
|
|
}
|
|
|
|
checksum, err := crypto.CalculateHashFromRequest(f)
|
|
if err != nil {
|
|
slog.Error("failed to calculate hash of file at given path", "error", err)
|
|
c.JSON(http.StatusInternalServerError, RespObj{
|
|
Msg: ieMes,
|
|
})
|
|
return
|
|
}
|
|
|
|
e := filepath.Ext(f.Filename)
|
|
m := categorizeFilemode(e)
|
|
if m == database.Unspecified {
|
|
slog.Debug("discarding file since its filetype is unsupported")
|
|
c.JSON(http.StatusUnsupportedMediaType, RespObj{
|
|
Msg: "unsupported filetype",
|
|
})
|
|
return
|
|
}
|
|
|
|
safeName := uuid.New().String() + "_" + string(m) + e
|
|
destPath := filepath.Join(env.DataDirectory, "content", safeName)
|
|
|
|
fData := database.File{
|
|
MediaType: m,
|
|
MetaName: f.Filename,
|
|
FileName: safeName,
|
|
FilePath: destPath,
|
|
Checksum: checksum,
|
|
}
|
|
|
|
// first register the file to the database
|
|
// error out if something bad happens (duplicate, failing db, etc)
|
|
err = database.RegisterFile(db, fData)
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrDuplicatedKey) {
|
|
slog.Debug("discarding file since its a duplicate", "error", err)
|
|
c.JSON(http.StatusConflict, RespObj{
|
|
Msg: "file is a duplicate",
|
|
})
|
|
return
|
|
}
|
|
|
|
slog.Error("discarding file since an error occured", "error", err)
|
|
c.JSON(http.StatusInternalServerError, RespObj{
|
|
Msg: ieMes,
|
|
})
|
|
return
|
|
}
|
|
|
|
// save to filesystem after everything has given a green light
|
|
if err := c.SaveUploadedFile(f, destPath); err != nil {
|
|
slog.Error("failed to receive the file over http:", "error", err)
|
|
c.JSON(http.StatusInternalServerError, RespObj{
|
|
Msg: ieMes,
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusCreated, RespObj{
|
|
Msg: "file has succesfully been uploaded",
|
|
})
|
|
})
|
|
}
|