Files
Aroll-Cutter/handlers/upload.go
2026-02-25 23:27:31 -08:00

50 lines
1.1 KiB
Go

package handlers
import (
"encoding/json"
"log"
"net/http"
"path/filepath"
"aroll/store"
)
func UploadHandler(st *store.Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
r.Body = http.MaxBytesReader(w, r.Body, 5<<30)
if err := r.ParseMultipartForm(32 << 20); err != nil {
http.Error(w, "parse form: "+err.Error(), http.StatusBadRequest)
return
}
defer r.MultipartForm.RemoveAll()
file, header, err := r.FormFile("video")
if err != nil {
http.Error(w, "missing 'video' field", http.StatusBadRequest)
return
}
defer file.Close()
ext := filepath.Ext(header.Filename)
if ext == "" {
ext = ".mp4"
}
key, err := st.Save(file, ext)
if err != nil {
http.Error(w, "save: "+err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"filename": key})
log.Println("Upload handler works")
}
}