From 9441409d6ca520e2118b3d116fc3daf7f775a0f8 Mon Sep 17 00:00:00 2001 From: Pavel Tatarskiy Date: Sat, 26 Oct 2024 18:05:47 +0300 Subject: [PATCH] add copy url add wget cmd --- assets/src/js/lib/progressLog.js | 15 +++++++++++- handlers/job/script/action.go | 24 ++++++++++++++++---- handlers/resource/helpers.go | 4 ++-- services/job/job.go | 11 +++++++++ templates/views/action/download_file.html | 13 +++++++++++ templates/views/action/download_torrent.html | 3 +++ templates/views/action/post.html | 3 --- templates/views/resource/get.html | 2 +- 8 files changed, 63 insertions(+), 12 deletions(-) create mode 100644 templates/views/action/download_file.html diff --git a/assets/src/js/lib/progressLog.js b/assets/src/js/lib/progressLog.js index 114d682..682f8ff 100644 --- a/assets/src/js/lib/progressLog.js +++ b/assets/src/js/lib/progressLog.js @@ -91,6 +91,16 @@ class Renderer { }); } } + addCustom(data) { + const div = document.createElement('div'); + this.lt.appendChild(div); + loadAsyncView(div, data.body); + for (const close of div.querySelectorAll('.closeable-close')) { + close.addEventListener('click', () => { + this.el.classList.add('hidden'); + }); + } + } addSummary(data) { if (!data.message) return; const pre = document.createElement('pre'); @@ -188,9 +198,12 @@ class Renderer { } if (data.level === 'rendertemplate') { data.render = (el) => { - loadAsyncView(el, data.body, data.template); + loadAsyncView(el, data.body); }; } + if (data.level === 'custom') { + this.addCustom(data); + } if (data.level === 'download') { this.addSummary(data); window.location = data.location; diff --git a/handlers/job/script/action.go b/handlers/job/script/action.go index 91d6d06..422890e 100644 --- a/handlers/job/script/action.go +++ b/handlers/job/script/action.go @@ -136,7 +136,11 @@ func (s *ActionScript) renderActionTemplate(j *job.Job, c *gin.Context, sc *Stre return nil } -func (s *ActionScript) download(j *job.Job, claims *api.Claims, resourceID string, itemID string) (err error) { +type FileDownload struct { + URL string +} + +func (s *ActionScript) download(j *job.Job, c *gin.Context, claims *api.Claims, resourceID string, itemID string) (err error) { j.InProgress("retrieving download link") ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() @@ -146,13 +150,21 @@ func (s *ActionScript) download(j *job.Job, claims *api.Claims, resourceID strin } j.Done() de := resp.ExportItems["download"] - url := de.URL + //url := de.URL if !de.ExportMetaItem.Meta.Cache { if err := s.warmUp(j, "warming up torrent client", resp.ExportItems["download"].URL, resp.ExportItems["torrent_client_stat"].URL, int(resp.Source.Size), 1_000_000, 0, "", true); err != nil { return err } } - j.Download(url) + j.DoneWithMessage("success! file is ready for download!") + tpl := s.tb.Build("action/download_file").WithLayoutBody(`{{ template "main" . }}`) + str, err := tpl.ToString(c, &FileDownload{ + URL: de.URL, + }) + if err != nil { + return err + } + j.Custom("action/download_file", strings.TrimSpace(str)) return } @@ -194,7 +206,7 @@ func (s *ActionScript) downloadTorrent(j *job.Job, c *gin.Context, claims *api.C if err != nil { return err } - j.RenderTemplate("action/download_torrent", strings.TrimSpace(str)) + j.Custom("action/download_torrent", strings.TrimSpace(str)) return nil } @@ -282,7 +294,9 @@ type ActionScript struct { func (s *ActionScript) Run(j *job.Job) (err error) { switch s.action { case "download": - return s.download(j, s.claims, s.resourceId, s.itemId) + return s.download(j, s.c, s.claims, s.resourceId, s.itemId) + case "download-dir": + return s.download(j, s.c, s.claims, s.resourceId, s.itemId) case "download-torrent": return s.downloadTorrent(j, s.c, s.claims, s.resourceId) case "preview-image": diff --git a/handlers/resource/helpers.go b/handlers/resource/helpers.go index c5b6e79..1262da6 100644 --- a/handlers/resource/helpers.go +++ b/handlers/resource/helpers.go @@ -103,7 +103,7 @@ func (s *Helper) MakeVideo(ctx *w.Context, gd *GetData) *ButtonItem { func (s *Helper) MakeDirDownload(ctx *w.Context, gd *GetData) *ButtonItem { return s.MakeDirButton(ctx, gd, fmt.Sprintf("Download Directory as ZIP [%v]", h.Bytes(uint64(gd.List.Size))), - "download", + "download-dir", "/download-dir", ) } @@ -111,7 +111,7 @@ func (s *Helper) MakeDirDownload(ctx *w.Context, gd *GetData) *ButtonItem { func (s *Helper) MakeTorrentDownload(ctx *w.Context, gd *GetData) *ButtonItem { return s.MakeResourceButton(ctx, gd, fmt.Sprintf("Download Torrent File"), - "download", + "download-torrent", "/download-torrent", ) } diff --git a/services/job/job.go b/services/job/job.go index fa265bc..85d54dd 100644 --- a/services/job/job.go +++ b/services/job/job.go @@ -74,6 +74,7 @@ const ( Redirect LogItemLevel = "redirect" Download LogItemLevel = "download" RenderTemplate LogItemLevel = "rendertemplate" + Custom LogItemLevel = "custom" StatusUpdate LogItemLevel = "statusupdate" Close LogItemLevel = "close" Open LogItemLevel = "open" @@ -90,6 +91,7 @@ var levelMap = map[LogItemLevel]log.Level{ Redirect: log.InfoLevel, StatusUpdate: log.InfoLevel, RenderTemplate: log.InfoLevel, + Custom: log.InfoLevel, Close: log.InfoLevel, } @@ -343,6 +345,15 @@ func (s *Job) close() { } } +func (s *Job) Custom(name string, body string) *Job { + _ = s.log(LogItem{ + Level: Custom, + Template: name, + Body: body, + }) + return s +} + type Jobs struct { queue string mux sync.Mutex diff --git a/templates/views/action/download_file.html b/templates/views/action/download_file.html new file mode 100644 index 0000000..095cb7f --- /dev/null +++ b/templates/views/action/download_file.html @@ -0,0 +1,13 @@ +{{ define "main" }} + +
+ copy wget cmd + copy url + start download + cancel +
+{{ end }} \ No newline at end of file diff --git a/templates/views/action/download_torrent.html b/templates/views/action/download_torrent.html index 95f02e4..387791d 100644 --- a/templates/views/action/download_torrent.html +++ b/templates/views/action/download_torrent.html @@ -8,4 +8,7 @@ downloadLink.click(); })(); +
+ close +
{{ end }} \ No newline at end of file diff --git a/templates/views/action/post.html b/templates/views/action/post.html index 2cbc460..70c9b84 100644 --- a/templates/views/action/post.html +++ b/templates/views/action/post.html @@ -8,9 +8,6 @@ {{ if has .Data "Job" }} {{ end }} {{ "action.js" | asset }} diff --git a/templates/views/resource/get.html b/templates/views/resource/get.html index 6b5483e..61228be 100644 --- a/templates/views/resource/get.html +++ b/templates/views/resource/get.html @@ -1,7 +1,7 @@ {{ define "title" }} {{ with .Data }} {{ if has . "Item" }} - Watching file {{ .Item.PathStr }} | {{ .Resource.Name }} + {{ .Item.PathStr }} | {{ .Resource.Name }} {{ else }} {{ .Resource.Name }} {{ end }}