62 lines
1.3 KiB
Go
62 lines
1.3 KiB
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"os"
|
|
|
|
"aroll/store"
|
|
"aroll/transcode"
|
|
"aroll/ws"
|
|
)
|
|
|
|
type exportRequest struct {
|
|
Filename string `json:"filename"`
|
|
Segments []transcode.Segment `json:"segments"`
|
|
}
|
|
|
|
func ExportHandler(st *store.Store, hub *ws.Hub) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
var req exportRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "bad JSON: "+err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
if len(req.Segments) == 0 {
|
|
http.Error(w, "no segments", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
inPath, ok := st.Path(req.Filename)
|
|
if !ok {
|
|
http.Error(w, "file not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
go func() {
|
|
/* ToDo: Make sure that other video formats gets transcoded */
|
|
outputKey := "output-" + store.NewID() + ".mp4"
|
|
outPath := st.Allocate(outputKey)
|
|
|
|
if err := transcode.ExportSegments(inPath, outPath, req.Segments, hub.Broadcast); err != nil {
|
|
os.Remove(outPath)
|
|
broadcastError(hub, err.Error())
|
|
return
|
|
}
|
|
|
|
msg, _ := json.Marshal(map[string]string{
|
|
"type": "done",
|
|
"message": outputKey,
|
|
})
|
|
hub.Broadcast(msg)
|
|
}()
|
|
|
|
w.WriteHeader(http.StatusAccepted)
|
|
}
|
|
}
|