Skip to content

Commit

Permalink
add copy url
Browse files Browse the repository at this point in the history
add wget cmd
  • Loading branch information
vintikzzz committed Oct 26, 2024
1 parent 5ec7449 commit 9441409
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 12 deletions.
15 changes: 14 additions & 1 deletion assets/src/js/lib/progressLog.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand Down
24 changes: 19 additions & 5 deletions handlers/job/script/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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":
Expand Down
4 changes: 2 additions & 2 deletions handlers/resource/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ 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",
)
}

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",
)
}
Expand Down
11 changes: 11 additions & 0 deletions services/job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
}

Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions templates/views/action/download_file.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{{ define "main" }}
<script>
var url = {{ .Data.URL }};
var file = new URL(url).pathname.split('/').pop();
var wgetCmd = `wget -c -O "${file}" -t 20 "${url}"`;
</script>
<div class="pt-3 sm:text-right ml-3">
<a class="btn btn-sm btn-accent m-2 closeable-close" onclick="navigator.clipboard.writeText(wgetCmd)">copy wget cmd</a>
<a class="btn btn-sm btn-accent m-2 closeable-close" onclick="navigator.clipboard.writeText(url)">copy url</a>
<a class="btn btn-sm btn-accent m-2 closeable-close" download href="{{ .Data.URL }}">start download</a>
<a class="close m-2 closeable-close">cancel</a>
</div>
{{ end }}
3 changes: 3 additions & 0 deletions templates/views/action/download_torrent.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@
downloadLink.click();
})();
</script>
<div class="pt-3 sm:text-right ml-3">
<a class="close m-2 closeable-close">close</a>
</div>
{{ end }}
3 changes: 0 additions & 3 deletions templates/views/action/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
{{ if has .Data "Job" }}
<form class="hidden progress-alert progress-alert-block mb-10 closeable" data-async-progress-log="{{ .Data.Job | makeJobLogURL }}" data-async-target="main">
<div class="log-target"></div>
<div class="pt-3 justify-end flex hidden alert-close-wrapper">
<a class="close closeable-close">got it!</a>
</div>
</form>
{{ end }}
{{ "action.js" | asset }}
Expand Down
2 changes: 1 addition & 1 deletion templates/views/resource/get.html
Original file line number Diff line number Diff line change
@@ -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 }}
Expand Down

0 comments on commit 9441409

Please sign in to comment.