FEAT: Initial Upload Page
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"thamanyah/cms/v2/internal/services"
|
||||
"thamanyah/cms/v2/internal/views"
|
||||
)
|
||||
|
||||
const (
|
||||
videoUploadPrefix = "videos"
|
||||
uploadURLExpiry = 15 * time.Minute
|
||||
maxJSONBodySize = 1 << 20 // 1 MiB
|
||||
)
|
||||
|
||||
func NewVideo(w http.ResponseWriter, r *http.Request) {
|
||||
views.VideoUpload().Render(r.Context(), w)
|
||||
}
|
||||
|
||||
type presignRequest struct {
|
||||
FileName string `json:"fileName"`
|
||||
ContentType string `json:"contentType"`
|
||||
}
|
||||
|
||||
type presignResponse struct {
|
||||
UploadURL string `json:"uploadUrl"`
|
||||
Key string `json:"key"`
|
||||
}
|
||||
|
||||
func PresignVideoUpload(w http.ResponseWriter, r *http.Request) {
|
||||
r.Body = http.MaxBytesReader(w, r.Body, maxJSONBodySize)
|
||||
|
||||
var req presignRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeJSONError(w, http.StatusBadRequest, "Invalid request.")
|
||||
return
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(req.ContentType, "video/") {
|
||||
writeJSONError(w, http.StatusUnprocessableEntity, fmt.Sprintf("Unsupported file type (%s). Please upload a video.", req.ContentType))
|
||||
return
|
||||
}
|
||||
|
||||
filename, err := randomFilename(req.FileName)
|
||||
if err != nil {
|
||||
writeJSONError(w, http.StatusInternalServerError, "Could not prepare upload.")
|
||||
return
|
||||
}
|
||||
key := videoUploadPrefix + "/" + filename
|
||||
|
||||
presignedURL, err := services.S3Client.GetPresignedURL(r.Context(), key, "video/mp4", time.Hour)
|
||||
if err != nil {
|
||||
writeJSONError(w, http.StatusInternalServerError, "Could not prepare upload.")
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(presignResponse{UploadURL: presignedURL, Key: key})
|
||||
}
|
||||
|
||||
type completeRequest struct {
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
Category string `json:"category"`
|
||||
Tags string `json:"tags"`
|
||||
FileName string `json:"fileName"`
|
||||
Key string `json:"key"`
|
||||
}
|
||||
|
||||
func CompleteVideoUpload(w http.ResponseWriter, r *http.Request) {
|
||||
r.Body = http.MaxBytesReader(w, r.Body, maxJSONBodySize)
|
||||
|
||||
var req completeRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
renderUploadError(w, r, "Invalid request.")
|
||||
return
|
||||
}
|
||||
|
||||
title := strings.TrimSpace(req.Title)
|
||||
if title == "" {
|
||||
renderUploadError(w, r, "Title is required.")
|
||||
return
|
||||
}
|
||||
|
||||
key := strings.TrimSpace(req.Key)
|
||||
if key == "" || !strings.HasPrefix(key, videoUploadPrefix+"/") {
|
||||
renderUploadError(w, r, "A video file is required.")
|
||||
return
|
||||
}
|
||||
|
||||
views.VideoUploadSuccess(views.VideoMetadata{
|
||||
Title: title,
|
||||
Description: strings.TrimSpace(req.Description),
|
||||
Category: strings.TrimSpace(req.Category),
|
||||
Tags: strings.TrimSpace(req.Tags),
|
||||
FileName: strings.TrimSpace(req.FileName),
|
||||
StoredAs: key,
|
||||
SizeBytes: 60,
|
||||
}).Render(r.Context(), w)
|
||||
}
|
||||
|
||||
func writeJSONError(w http.ResponseWriter, status int, message string) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": message})
|
||||
}
|
||||
|
||||
func renderUploadError(w http.ResponseWriter, r *http.Request, message string) {
|
||||
fmt.Println("Error Ocurred: " + message)
|
||||
w.WriteHeader(http.StatusUnprocessableEntity)
|
||||
views.VideoUploadError(message).Render(r.Context(), w)
|
||||
}
|
||||
|
||||
func randomFilename(original string) (string, error) {
|
||||
ext := filepath.Ext(filepath.Base(original))
|
||||
buf := make([]byte, 16)
|
||||
if _, err := rand.Read(buf); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(buf) + ext, nil
|
||||
}
|
||||
Reference in New Issue
Block a user