Reorganized API + added db migrations
Read the README file for more informations
This commit is contained in:
41
backend/api/media/delete.go
Normal file
41
backend/api/media/delete.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package media
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func HandleDelete(w http.ResponseWriter, r *http.Request) {
|
||||
uuid := r.PathValue("media_uuid")
|
||||
var media core.Media
|
||||
res, err := core.DB.NewDelete().
|
||||
Model(&media).
|
||||
Where("id = ?", uuid).
|
||||
Returning("*").
|
||||
Exec(context.Background())
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
log.Println(res)
|
||||
err = os.Remove(media.Path)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Image successfully deleted.",
|
||||
}.Respond(w, http.StatusOK)
|
||||
}
|
||||
117
backend/api/media/media.go
Normal file
117
backend/api/media/media.go
Normal file
@@ -0,0 +1,117 @@
|
||||
package media
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
"fr.latosa-escrima/utils"
|
||||
)
|
||||
|
||||
func HandleMedia(w http.ResponseWriter, r *http.Request) {
|
||||
queryParams := r.URL.Query()
|
||||
page, err := strconv.Atoi(queryParams.Get("page"))
|
||||
limit, err := strconv.Atoi(queryParams.Get("limit"))
|
||||
if page < 0 {
|
||||
page = 0
|
||||
}
|
||||
if limit <= 0 {
|
||||
limit = 10
|
||||
}
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
offset := (page - 1) * limit
|
||||
|
||||
total, err := core.DB.NewSelect().
|
||||
Model((*core.Media)(nil)).
|
||||
Count(context.Background())
|
||||
|
||||
totalPages := int(math.Max(1, float64(total/limit)))
|
||||
|
||||
var media []core.Media
|
||||
err = core.DB.NewSelect().
|
||||
Model(&media).
|
||||
Limit(limit).
|
||||
Offset(offset).
|
||||
Scan(context.Background())
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
baseURL := utils.GetURL(r)
|
||||
media = utils.Map(media, func(m core.Media) core.Media {
|
||||
m.Author = nil
|
||||
m.URL = fmt.Sprintf("%s%s/file", baseURL, m.ID)
|
||||
return m
|
||||
})
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Media successfully retrieved",
|
||||
Data: core.Paginated[core.Media]{
|
||||
Page: page,
|
||||
Limit: limit,
|
||||
TotalPages: totalPages,
|
||||
Items: media,
|
||||
},
|
||||
}.Respond(w, http.StatusOK)
|
||||
}
|
||||
|
||||
func HandleMediaDetails(w http.ResponseWriter, r *http.Request) {
|
||||
uuid := r.PathValue("media_uuid")
|
||||
var media core.Media
|
||||
err := core.DB.NewSelect().
|
||||
Model(&media).
|
||||
Where("id = ?", uuid).
|
||||
Limit(1).
|
||||
Scan(context.Background())
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
baseURL := utils.GetURL(r)
|
||||
media.URL = fmt.Sprintf("%s/file", baseURL)
|
||||
|
||||
media.Author = nil
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Media retrieved",
|
||||
Data: media,
|
||||
}.Respond(w, http.StatusOK)
|
||||
}
|
||||
|
||||
func HandleMediaFile(w http.ResponseWriter, r *http.Request) {
|
||||
uuid := r.PathValue("media_uuid")
|
||||
var media core.Media
|
||||
err := core.DB.NewSelect().
|
||||
Model(&media).
|
||||
Where("id = ?", uuid).
|
||||
Limit(1).
|
||||
Scan(context.Background())
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
http.ServeFile(w, r, media.Path)
|
||||
|
||||
}
|
||||
3
backend/api/media/update.go
Normal file
3
backend/api/media/update.go
Normal file
@@ -0,0 +1,3 @@
|
||||
package media
|
||||
|
||||
// TODO
|
||||
119
backend/api/media/upload.go
Normal file
119
backend/api/media/upload.go
Normal file
@@ -0,0 +1,119 @@
|
||||
package media
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
"fr.latosa-escrima/utils"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func HandleUpload(w http.ResponseWriter, r *http.Request) {
|
||||
// Parse the multipart form
|
||||
err := r.ParseMultipartForm(10 << 20) // Limit file size to 10 MB
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusRequestEntityTooLarge)
|
||||
return
|
||||
}
|
||||
|
||||
// Retrieve the file from the form
|
||||
file, fileHeader, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Create the destination file
|
||||
if !utils.DoesPathExist("media") {
|
||||
fmt.Println("Creating media forlder")
|
||||
err = os.MkdirAll("media", os.ModePerm)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
p := filepath.Join("media", fileHeader.Filename)
|
||||
dst, err := os.Create(p)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer dst.Close()
|
||||
|
||||
// Copy the file content to the destination file
|
||||
_, err = io.Copy(dst, file)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
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.",
|
||||
}.Respond(w, http.StatusCreated)
|
||||
}
|
||||
62
backend/api/media/verify.go
Normal file
62
backend/api/media/verify.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package media
|
||||
|
||||
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 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)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user