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

42 lines
813 B
Go

package handlers
import (
"log"
"net/http"
"os"
"strings"
"aroll/store"
)
func DownloadHandler(st *store.Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
key := strings.TrimPrefix(r.URL.Path, "/")
if !strings.HasPrefix(key, "output-") {
http.NotFound(w, r)
return
}
path, ok := st.Path(key)
if !ok {
http.Error(w, "file not found or already downloaded", http.StatusNotFound)
return
}
defer st.Delete(key)
f, err := os.Open(path)
if err != nil {
http.Error(w, "file not found", http.StatusNotFound)
return
}
defer f.Close()
fi, _ := f.Stat()
w.Header().Set("Content-Disposition", `attachment; filename="aroll-cut.mp4"`)
http.ServeContent(w, r, "aroll-cut.mp4", fi.ModTime(), f)
log.Println("Download handler works")
}
}