39 lines
765 B
Go
39 lines
765 B
Go
package handlers
|
|
|
|
import (
|
|
"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)
|
|
}
|
|
}
|