Media upload
This commit is contained in:
73
backend/api/upload_media.go
Normal file
73
backend/api/upload_media.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
"fr.latosa-escrima/utils"
|
||||
)
|
||||
|
||||
func HandleUploadMedia(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
|
||||
}
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "File uploaded successfully.",
|
||||
}.Respond(w, http.StatusCreated)
|
||||
}
|
||||
62
backend/api/verify_media.go
Normal file
62
backend/api/verify_media.go
Normal file
@@ -0,0 +1,62 @@
|
||||
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)
|
||||
|
||||
}
|
||||
@@ -19,7 +19,7 @@ require (
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/puzpuzpuz/xsync/v3 v3.4.0 // indirect
|
||||
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
|
||||
github.com/uptrace/bun/extra/bundebug v1.2.8 // indirect
|
||||
github.com/uptrace/bun/extra/bundebug v1.2.8 // direct
|
||||
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
|
||||
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
|
||||
golang.org/x/crypto v0.31.0 // indirect
|
||||
|
||||
@@ -97,10 +97,17 @@ func main() {
|
||||
// "/users/{user_uuid}/events/{event_uuid}": {Handler: nil, Middleware: nil},
|
||||
// "/users/{user_uuid}/events/{event_uuid}/delete": {Handler: nil, Middleware: nil},
|
||||
// "/users/{user_uuid}/events/{event_uuid}/update": {Handler: nil, Middleware: nil},
|
||||
"/blogs/new": {Handler: api.HandleCreateBlog, Middlewares: nil},
|
||||
"/blogs/new": {Handler: api.HandleCreateBlog, Middlewares: nil},
|
||||
"/blogs/{uuid}": {
|
||||
Handler: api.HandleGetBlog,
|
||||
Middlewares: []core.Middleware{api.Methods("GET")}, },
|
||||
Handler: api.HandleGetBlog,
|
||||
Middlewares: []core.Middleware{api.Methods("GET")}},
|
||||
"/media/upload": {
|
||||
Handler: api.HandleUploadMedia,
|
||||
Middlewares: []core.Middleware{api.Methods("POST"), api.AuthJWT}},
|
||||
"/media/verify": {
|
||||
Handler: api.HandleVerifyMedia,
|
||||
Middlewares: []core.Middleware{api.Methods("POST"), api.AuthJWT},
|
||||
},
|
||||
})
|
||||
|
||||
fmt.Printf("Serving on port %s\n", port)
|
||||
|
||||
11
backend/utils/path_exists.go
Normal file
11
backend/utils/path_exists.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package utils
|
||||
|
||||
import "os"
|
||||
|
||||
func DoesPathExist(path string) bool {
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user