queue.go
| 1 | package handler |
| 2 | |
| 3 | import ( |
| 4 | "log" |
| 5 | "net/http" |
| 6 | "strconv" |
| 7 | |
| 8 | "vidarchive/internal/models" |
| 9 | ) |
| 10 | |
| 11 | func (h *Handler) Downloads(w http.ResponseWriter, r *http.Request) { |
| 12 | status := r.URL.Query().Get("status") |
| 13 | sortBy := sortFromRequest(w, r, "queue_sort", "date") |
| 14 | |
| 15 | downloads, err := h.downloadSvc.GetAll(status, sortBy) |
| 16 | if err != nil { |
| 17 | h.serverError(w, r, "list downloads", err) |
| 18 | return |
| 19 | } |
| 20 | |
| 21 | settings := h.settingsOrDefault() |
| 22 | |
| 23 | h.renderWithRequest(w, r, "queue", PageData{ |
| 24 | Title: "Queue", |
| 25 | ActiveTab: "queue", |
| 26 | AutoRefresh: settings.AutoRefreshDownloads, |
| 27 | RefreshSec: settings.RefreshInterval, |
| 28 | Data: struct { |
| 29 | Items []*models.Download |
| 30 | Status string |
| 31 | SortBy string |
| 32 | }{ |
| 33 | Items: downloads, |
| 34 | Status: status, |
| 35 | SortBy: sortBy, |
| 36 | }, |
| 37 | }) |
| 38 | } |
| 39 | |
| 40 | func (h *Handler) DownloadDetail(w http.ResponseWriter, r *http.Request) { |
| 41 | id, ok := parseID(w, r) |
| 42 | if !ok { |
| 43 | return |
| 44 | } |
| 45 | |
| 46 | download, err := h.downloadSvc.GetByID(id) |
| 47 | if err != nil { |
| 48 | http.Error(w, "Not found", http.StatusNotFound) |
| 49 | return |
| 50 | } |
| 51 | |
| 52 | h.renderWithRequest(w, r, "queue_detail", PageData{ |
| 53 | Title: "Queue Details", |
| 54 | ActiveTab: "queue", |
| 55 | Data: download, |
| 56 | }) |
| 57 | } |
| 58 | |
| 59 | func (h *Handler) CreateDownload(w http.ResponseWriter, r *http.Request) { |
| 60 | if err := r.ParseForm(); err != nil { |
| 61 | http.Error(w, err.Error(), http.StatusBadRequest) |
| 62 | return |
| 63 | } |
| 64 | |
| 65 | url := r.FormValue("url") |
| 66 | if url == "" { |
| 67 | flashError(w, "A URL is required to start a download.") |
| 68 | http.Redirect(w, r, "/download", http.StatusSeeOther) |
| 69 | return |
| 70 | } |
| 71 | |
| 72 | var presetID *int64 |
| 73 | if pid := r.FormValue("preset_id"); pid != "" { |
| 74 | id, err := strconv.ParseInt(pid, 10, 64) |
| 75 | if err == nil { |
| 76 | presetID = &id |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | formatOverride := r.FormValue("format_override") |
| 81 | customFlags := r.FormValue("custom_flags") |
| 82 | outputDir := r.FormValue("output_dir") |
| 83 | |
| 84 | download, err := h.downloadSvc.Create(url, presetID, formatOverride, customFlags, outputDir) |
| 85 | if err != nil { |
| 86 | redirectWithError(w, r, "/download", "Couldn't queue this download.", err) |
| 87 | return |
| 88 | } |
| 89 | |
| 90 | h.workerPool.Submit(download) |
| 91 | |
| 92 | redirectWithSuccess(w, r, "/queue", "Download queued.") |
| 93 | } |
| 94 | |
| 95 | func (h *Handler) DeleteDownload(w http.ResponseWriter, r *http.Request) { |
| 96 | id, ok := parseID(w, r) |
| 97 | if !ok { |
| 98 | return |
| 99 | } |
| 100 | |
| 101 | if err := h.downloadSvc.Delete(id); err != nil { |
| 102 | redirectWithError(w, r, "/queue", "Couldn't remove this download.", err) |
| 103 | return |
| 104 | } |
| 105 | |
| 106 | redirectWithSuccess(w, r, "/queue", "Download removed.") |
| 107 | } |
| 108 | |
| 109 | func (h *Handler) ClearAllDownloads(w http.ResponseWriter, r *http.Request) { |
| 110 | if err := h.downloadSvc.DeleteAll(); err != nil { |
| 111 | redirectWithError(w, r, "/queue", "Couldn't clear the queue.", err) |
| 112 | return |
| 113 | } |
| 114 | |
| 115 | redirectWithSuccess(w, r, "/queue", "Queue cleared.") |
| 116 | } |
| 117 | |
| 118 | func (h *Handler) DownloadForm(w http.ResponseWriter, r *http.Request) { |
| 119 | presets, err := h.presetSvc.GetAll() |
| 120 | if err != nil { |
| 121 | h.serverError(w, r, "list presets", err) |
| 122 | return |
| 123 | } |
| 124 | |
| 125 | // A missing default preset is normal (the user may not have set one); the |
| 126 | // template handles a nil DefaultPreset, so this isn't surfaced as an error. |
| 127 | defaultPreset, _ := h.presetSvc.GetDefault() |
| 128 | |
| 129 | url := r.URL.Query().Get("url") |
| 130 | var formats []*models.FormatInfo |
| 131 | var flash *Flash |
| 132 | if r.URL.Query().Get("list_formats") == "1" && url != "" { |
| 133 | var err error |
| 134 | if formats, err = h.downloadSvc.ListFormats(url); err != nil { |
| 135 | log.Printf("DownloadForm: listing formats for %q failed: %v", url, err) |
| 136 | flash = &Flash{Kind: "error", Message: "Couldn't list formats: " + err.Error()} |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | h.renderWithRequest(w, r, "download_form", PageData{ |
| 141 | Title: "Download", |
| 142 | ActiveTab: "download", |
| 143 | Flash: flash, |
| 144 | Data: struct { |
| 145 | Presets []*models.Preset |
| 146 | DefaultPreset *models.Preset |
| 147 | URL string |
| 148 | FormatOverride string |
| 149 | CustomFlags string |
| 150 | OutputDir string |
| 151 | Formats []*models.FormatInfo |
| 152 | ShowFormats bool |
| 153 | }{ |
| 154 | Presets: presets, |
| 155 | DefaultPreset: defaultPreset, |
| 156 | URL: url, |
| 157 | FormatOverride: r.URL.Query().Get("format_override"), |
| 158 | CustomFlags: r.URL.Query().Get("custom_flags"), |
| 159 | OutputDir: r.URL.Query().Get("output_dir"), |
| 160 | Formats: formats, |
| 161 | ShowFormats: r.URL.Query().Get("list_formats") == "1", |
| 162 | }, |
| 163 | }) |
| 164 | } |
| 165 |