Files
latosa-escrima/backend/api/verify_media.go
2025-01-20 10:27:54 +01:00

63 lines
1.2 KiB
Go

package api
import (
"encoding/json"
"io"
"net/http"
"path/filepath"
"fr.latosa-escrima/api/core"
"fr.latosa-escrima/utils"
)
const MAX_SIZE_BYTES = 10 * 1024 * 1024
type FileArgs struct {
Name string `json:"name"`
Type string `json:"type"`
SizeByte int64 `json:"size"`
}
func HandleVerifyMedia(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusInternalServerError)
return
}
var file FileArgs
err = json.Unmarshal(body, &file)
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusInternalServerError)
return
}
if utils.DoesPathExist(filepath.Join("media", file.Name)) {
core.JSONError{
Status: core.Error,
Message: "File already exists.",
}.Respond(w, http.StatusInternalServerError)
return
}
if file.SizeByte > MAX_SIZE_BYTES {
core.JSONError{
Status: core.Error,
Message: "File is too big.",
}.Respond(w, http.StatusRequestEntityTooLarge)
return
}
core.JSONSuccess{
Status: core.Success,
Message: "File can be uploaded.",
}.Respond(w, http.StatusOK)
}