Started to work on media upload and organization

This commit is contained in:
cdricms
2025-01-23 20:10:15 +01:00
parent f9dce4b40b
commit cdd8e34096
21 changed files with 1069 additions and 159 deletions

View File

@@ -1,6 +1,7 @@
package api
import (
"context"
"fmt"
"io"
"net/http"
@@ -9,6 +10,8 @@ import (
"fr.latosa-escrima/api/core"
"fr.latosa-escrima/utils"
"github.com/golang-jwt/jwt/v5"
"github.com/google/uuid"
)
func HandleUploadMedia(w http.ResponseWriter, r *http.Request) {
@@ -66,6 +69,49 @@ func HandleUploadMedia(w http.ResponseWriter, r *http.Request) {
return
}
token, ok := r.Context().Value("token").(*jwt.Token)
if !ok {
core.JSONError{
Status: core.Error,
Message: "Couldn't retrieve your JWT.",
}.Respond(w, http.StatusInternalServerError)
return
}
claims, ok := token.Claims.(jwt.MapClaims)
if !ok {
core.JSONError{
Status: core.Error,
Message: "Invalid token claims.",
}.Respond(w, http.StatusInternalServerError)
return
}
id, err := uuid.Parse(claims["user_id"].(string))
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusInternalServerError)
return
}
media := &core.Media{
AuthorID: id,
Type: fileHeader.Header.Get("Content-Type"),
Alt: "To be implemented",
Path: p,
Size: fileHeader.Size,
}
_, err = core.DB.NewInsert().Model(media).Exec(context.Background())
if err != nil {
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusInternalServerError)
return
}
core.JSONSuccess{
Status: core.Success,
Message: "File uploaded successfully.",