Started to work on media upload and organization
This commit is contained in:
117
backend/api/get_media.go
Normal file
117
backend/api/get_media.go
Normal file
@@ -0,0 +1,117 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
"fr.latosa-escrima/utils"
|
||||
)
|
||||
|
||||
func HandleGetMedia(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 HandleGetMediaDetails(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 HandleGetMediaFile(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)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user