first successful export
This commit is contained in:
@@ -1,40 +1,38 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"os"
|
||||
"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 {
|
||||
path, ok := st.Path(key)
|
||||
if !ok {
|
||||
http.Error(w, "file not found or already downloaded", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
defer st.Delete(key)
|
||||
|
||||
// Delete from Redis now — one download only
|
||||
defer st.Delete(ctx, key)
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
http.Error(w, "file not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
w.Header().Set("Content-Type", "video/mp4")
|
||||
fi, _ := f.Stat()
|
||||
w.Header().Set("Content-Disposition", `attachment; filename="aroll-cut.mp4"`)
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(data)))
|
||||
w.Write(data)
|
||||
http.ServeContent(w, r, "aroll-cut.mp4", fi.ModTime(), f)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user