package handler import ( "log" "net/http" "strconv" "vidarchive/internal/models" ) func (h *Handler) Downloads(w http.ResponseWriter, r *http.Request) { status := r.URL.Query().Get("status") sortBy := sortFromRequest(w, r, "queue_sort", "date") downloads, err := h.downloadSvc.GetAll(status, sortBy) if err != nil { h.serverError(w, r, "list downloads", err) return } settings := h.settingsOrDefault() h.renderWithRequest(w, r, "queue", PageData{ Title: "Queue", ActiveTab: "queue", AutoRefresh: settings.AutoRefreshDownloads, RefreshSec: settings.RefreshInterval, Data: struct { Items []*models.Download Status string SortBy string }{ Items: downloads, Status: status, SortBy: sortBy, }, }) } func (h *Handler) DownloadDetail(w http.ResponseWriter, r *http.Request) { id, ok := parseID(w, r) if !ok { return } download, err := h.downloadSvc.GetByID(id) if err != nil { http.Error(w, "Not found", http.StatusNotFound) return } h.renderWithRequest(w, r, "queue_detail", PageData{ Title: "Queue Details", ActiveTab: "queue", Data: download, }) } func (h *Handler) CreateDownload(w http.ResponseWriter, r *http.Request) { if err := r.ParseForm(); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } url := r.FormValue("url") if url == "" { flashError(w, "A URL is required to start a download.") http.Redirect(w, r, "/download", http.StatusSeeOther) return } var presetID *int64 if pid := r.FormValue("preset_id"); pid != "" { id, err := strconv.ParseInt(pid, 10, 64) if err == nil { presetID = &id } } formatOverride := r.FormValue("format_override") customFlags := r.FormValue("custom_flags") outputDir := r.FormValue("output_dir") download, err := h.downloadSvc.Create(url, presetID, formatOverride, customFlags, outputDir) if err != nil { redirectWithError(w, r, "/download", "Couldn't queue this download.", err) return } h.workerPool.Submit(download) redirectWithSuccess(w, r, "/queue", "Download queued.") } func (h *Handler) DeleteDownload(w http.ResponseWriter, r *http.Request) { id, ok := parseID(w, r) if !ok { return } if err := h.downloadSvc.Delete(id); err != nil { redirectWithError(w, r, "/queue", "Couldn't remove this download.", err) return } redirectWithSuccess(w, r, "/queue", "Download removed.") } func (h *Handler) ClearAllDownloads(w http.ResponseWriter, r *http.Request) { if err := h.downloadSvc.DeleteAll(); err != nil { redirectWithError(w, r, "/queue", "Couldn't clear the queue.", err) return } redirectWithSuccess(w, r, "/queue", "Queue cleared.") } func (h *Handler) DownloadForm(w http.ResponseWriter, r *http.Request) { presets, err := h.presetSvc.GetAll() if err != nil { h.serverError(w, r, "list presets", err) return } // A missing default preset is normal (the user may not have set one); the // template handles a nil DefaultPreset, so this isn't surfaced as an error. defaultPreset, _ := h.presetSvc.GetDefault() url := r.URL.Query().Get("url") var formats []*models.FormatInfo var flash *Flash if r.URL.Query().Get("list_formats") == "1" && url != "" { var err error if formats, err = h.downloadSvc.ListFormats(r.Context(), url); err != nil { log.Printf("DownloadForm: listing formats for %q failed: %v", url, err) flash = &Flash{Kind: "error", Message: "Couldn't list formats: " + err.Error()} } } h.renderWithRequest(w, r, "download_form", PageData{ Title: "Download", ActiveTab: "download", Flash: flash, Data: struct { Presets []*models.Preset DefaultPreset *models.Preset URL string FormatOverride string CustomFlags string OutputDir string Formats []*models.FormatInfo ShowFormats bool }{ Presets: presets, DefaultPreset: defaultPreset, URL: url, FormatOverride: r.URL.Query().Get("format_override"), CustomFlags: r.URL.Query().Get("custom_flags"), OutputDir: r.URL.Query().Get("output_dir"), Formats: formats, ShowFormats: r.URL.Query().Get("list_formats") == "1", }, }) }