63 lines
1.2 KiB
Go
63 lines
1.2 KiB
Go
package media
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"path/filepath"
|
|
|
|
"fr.latosa-escrima/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 HandleVerify(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)
|
|
|
|
}
|