41 lines
984 B
Go
41 lines
984 B
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"aroll/store"
|
|
)
|
|
|
|
// DownloadHandler fetches the exported video from Redis, streams it to the
|
|
// browser, then immediately deletes the key. One download per export.
|
|
func DownloadHandler(st *store.Store) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
key := strings.TrimPrefix(r.URL.Path, "/")
|
|
|
|
// Only serve keys we created in ExportHandler
|
|
if !strings.HasPrefix(key, "output-") {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
data, err := st.Get(ctx, key)
|
|
if err != nil {
|
|
http.Error(w, "file not found or already downloaded", http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
// Delete from Redis now — one download only
|
|
defer st.Delete(ctx, key)
|
|
|
|
w.Header().Set("Content-Type", "video/mp4")
|
|
w.Header().Set("Content-Disposition", `attachment; filename="aroll-cut.mp4"`)
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(data)))
|
|
w.Write(data)
|
|
}
|
|
}
|