Trying to fix filename download

This commit is contained in:
Andrea Fazzi 2021-11-01 11:34:43 +01:00
parent cf3bf25b05
commit f9f1698355
7 changed files with 57 additions and 49 deletions

1
backend/.gitignore vendored
View file

@ -1 +1,2 @@
youtube-dl-service youtube-dl-service

View file

@ -4,7 +4,8 @@ import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"time" "path/filepath"
"strconv"
"git.andreafazzi.eu/andrea/youtube-dl-service/task" "git.andreafazzi.eu/andrea/youtube-dl-service/task"
"git.andreafazzi.eu/andrea/youtube-dl-service/youtube" "git.andreafazzi.eu/andrea/youtube-dl-service/youtube"
@ -25,11 +26,7 @@ func download(c *gin.Context, downloader youtube.Downloader) error {
if err != nil { if err != nil {
return err return err
} }
c.JSON(200, gin.H{ c.JSON(http.StatusOK, video)
"title": video.Title,
"duration": float64(video.Duration) / float64(time.Minute),
"thumbnails": video.Thumbnails[0],
})
go downloader.StartDownload(video, tasks) go downloader.StartDownload(video, tasks)
@ -43,15 +40,19 @@ func status(c *gin.Context, downloader youtube.Downloader) error {
} }
task, ok := tasks[id] task, ok := tasks[id]
if ok { if ok {
c.JSON(200, gin.H{ c.JSON(http.StatusOK, task)
"status": task.Status,
"download_path": task.DownloadPath,
})
} }
return nil return nil
} }
func serveFile(c *gin.Context, filename string) error {
c.Writer.Header().Set("Content-Disposition", "attachment; filename="+strconv.Quote(filename))
c.Writer.Header().Set("Content-Type", "application/octet-stream")
http.ServeFile(c.Writer, c.Request, filepath.Join("data", filename))
return nil
}
func main() { func main() {
r := gin.Default() r := gin.Default()
r.Use(cors.New(cors.Config{ r.Use(cors.New(cors.Config{
@ -67,7 +68,13 @@ func main() {
c.AbortWithStatus(http.StatusInternalServerError) c.AbortWithStatus(http.StatusInternalServerError)
})) }))
r.Static("/data", "./data") r.GET("/data/:filename", func(c *gin.Context) {
if err := serveFile(c, c.Param("filename")); err != nil {
panic(err)
}
})
// r.Static("/data", "./data")
r.GET("/download", func(c *gin.Context) { r.GET("/download", func(c *gin.Context) {
if err := download(c, youtube_dl.NewYoutubeDlDownloader(c.Query("url"), "yt-dlp")); err != nil { if err := download(c, youtube_dl.NewYoutubeDlDownloader(c.Query("url"), "yt-dlp")); err != nil {

Binary file not shown.

View file

@ -671,7 +671,7 @@ var app = (function () {
attr_dev(button, "class", "btn btn-outline-secondary"); attr_dev(button, "class", "btn btn-outline-secondary");
attr_dev(button, "type", "submit"); attr_dev(button, "type", "submit");
button.disabled = true; button.disabled = true;
add_location(button, file$3, 38, 6, 889); add_location(button, file$3, 38, 6, 894);
}, },
m: function mount(target, anchor) { m: function mount(target, anchor) {
insert_dev(target, button, anchor); insert_dev(target, button, anchor);
@ -712,7 +712,7 @@ var app = (function () {
button.textContent = "Download"; button.textContent = "Download";
attr_dev(button, "class", "btn btn-outline-primary"); attr_dev(button, "class", "btn btn-outline-primary");
attr_dev(button, "type", "submit"); attr_dev(button, "type", "submit");
add_location(button, file$3, 36, 6, 761); add_location(button, file$3, 36, 6, 766);
}, },
m: function mount(target, anchor) { m: function mount(target, anchor) {
insert_dev(target, button, anchor); insert_dev(target, button, anchor);
@ -768,10 +768,10 @@ var app = (function () {
attr_dev(input, "class", "form-control"); attr_dev(input, "class", "form-control");
attr_dev(input, "placeholder", "Insert a Youtube url here..."); attr_dev(input, "placeholder", "Insert a Youtube url here...");
attr_dev(input, "aria-label", "URL for the Youtube video to download."); attr_dev(input, "aria-label", "URL for the Youtube video to download.");
add_location(input, file$3, 34, 4, 560); add_location(input, file$3, 34, 4, 565);
attr_dev(div, "class", "input-group"); attr_dev(div, "class", "input-group");
add_location(div, file$3, 33, 2, 530); add_location(div, file$3, 33, 2, 535);
attr_dev(form, "class", "needs-validation"); attr_dev(form, "class", "needs-validation mt-3");
add_location(form, file$3, 32, 0, 496); add_location(form, file$3, 32, 0, 496);
}, },
l: function claim(nodes) { l: function claim(nodes) {
@ -926,7 +926,7 @@ var app = (function () {
p = element("p"); p = element("p");
t = text(t_value); t = text(t_value);
set_style(p, "color", "red"); set_style(p, "color", "red");
add_location(p, file$2, 58, 2, 1594); add_location(p, file$2, 58, 2, 1595);
}, },
m: function mount(target, anchor) { m: function mount(target, anchor) {
insert_dev(target, p, anchor); insert_dev(target, p, anchor);
@ -955,7 +955,7 @@ var app = (function () {
let div2; let div2;
let div0; let div0;
let h5; let h5;
let t0_value = /*video_info*/ ctx[3].title + ""; let t0_value = /*video_info*/ ctx[3].Title + "";
let t0; let t0;
let t1; let t1;
let div1; let div1;
@ -968,7 +968,7 @@ var app = (function () {
let t6; let t6;
let img; let img;
let img_src_value; let img_src_value;
let if_block = /*status*/ ctx[1].status && create_if_block$1(ctx); let if_block = /*status*/ ctx[1].Status && create_if_block$1(ctx);
const block = { const block = {
c: function create() { c: function create() {
@ -989,22 +989,22 @@ var app = (function () {
t5 = text(/*url*/ ctx[0]); t5 = text(/*url*/ ctx[0]);
t6 = space(); t6 = space();
img = element("img"); img = element("img");
add_location(h5, file$2, 48, 49, 1206); add_location(h5, file$2, 48, 49, 1204);
attr_dev(div0, "class", "pb-2 flex-grow-1 bd-highlight"); attr_dev(div0, "class", "pb-2 flex-grow-1 bd-highlight");
add_location(div0, file$2, 48, 6, 1163); add_location(div0, file$2, 48, 6, 1161);
add_location(small0, file$2, 49, 42, 1282); add_location(small0, file$2, 49, 42, 1280);
attr_dev(div1, "class", "pb-2 px-2 bd-highlight"); attr_dev(div1, "class", "pb-2 px-2 bd-highlight");
add_location(div1, file$2, 49, 6, 1246); add_location(div1, file$2, 49, 6, 1244);
attr_dev(div2, "class", "d-flex bd-highlight"); attr_dev(div2, "class", "d-flex bd-highlight");
add_location(div2, file$2, 47, 4, 1123); add_location(div2, file$2, 47, 4, 1121);
add_location(small1, file$2, 54, 7, 1504); add_location(small1, file$2, 54, 7, 1502);
add_location(p, file$2, 54, 4, 1501); add_location(p, file$2, 54, 4, 1499);
if (!src_url_equal(img.src, img_src_value = /*video_info*/ ctx[3].thumbnails.URL)) attr_dev(img, "src", img_src_value); if (!src_url_equal(img.src, img_src_value = /*video_info*/ ctx[3].Thumbnails[0].URL)) attr_dev(img, "src", img_src_value);
add_location(img, file$2, 55, 4, 1533); add_location(img, file$2, 55, 4, 1531);
attr_dev(a, "href", /*download_path*/ ctx[2]); attr_dev(a, "href", /*download_path*/ ctx[2]);
attr_dev(a, "class", "list-group-item list-group-item-action"); attr_dev(a, "class", "list-group-item list-group-item-action");
attr_dev(a, "aria-current", "true"); attr_dev(a, "aria-current", "true");
add_location(a, file$2, 46, 2, 1027); add_location(a, file$2, 46, 2, 1025);
}, },
m: function mount(target, anchor) { m: function mount(target, anchor) {
insert_dev(target, a, anchor); insert_dev(target, a, anchor);
@ -1025,7 +1025,7 @@ var app = (function () {
append_dev(a, img); append_dev(a, img);
}, },
p: function update(ctx, dirty) { p: function update(ctx, dirty) {
if (/*status*/ ctx[1].status) { if (/*status*/ ctx[1].Status) {
if (if_block) { if (if_block) {
if_block.p(ctx, dirty); if_block.p(ctx, dirty);
} else { } else {
@ -1061,11 +1061,11 @@ var app = (function () {
return block; return block;
} }
// (51:6) {#if status.status} // (51:6) {#if status.Status}
function create_if_block$1(ctx) { function create_if_block$1(ctx) {
let div; let div;
let span; let span;
let t_value = /*statusBadge*/ ctx[4][/*status*/ ctx[1].status].text + ""; let t_value = /*statusBadge*/ ctx[4][/*status*/ ctx[1].Status].text + "";
let t; let t;
let span_class_value; let span_class_value;
@ -1074,10 +1074,10 @@ var app = (function () {
div = element("div"); div = element("div");
span = element("span"); span = element("span");
t = text(t_value); t = text(t_value);
attr_dev(span, "class", span_class_value = /*statusBadge*/ ctx[4][/*status*/ ctx[1].status].class); attr_dev(span, "class", span_class_value = /*statusBadge*/ ctx[4][/*status*/ ctx[1].Status].class);
add_location(span, file$2, 51, 37, 1380); add_location(span, file$2, 51, 37, 1378);
attr_dev(div, "class", "pb-2 px-2 bd-highlight"); attr_dev(div, "class", "pb-2 px-2 bd-highlight");
add_location(div, file$2, 51, 1, 1344); add_location(div, file$2, 51, 1, 1342);
}, },
m: function mount(target, anchor) { m: function mount(target, anchor) {
insert_dev(target, div, anchor); insert_dev(target, div, anchor);
@ -1085,9 +1085,9 @@ var app = (function () {
append_dev(span, t); append_dev(span, t);
}, },
p: function update(ctx, dirty) { p: function update(ctx, dirty) {
if (dirty & /*status*/ 2 && t_value !== (t_value = /*statusBadge*/ ctx[4][/*status*/ ctx[1].status].text + "")) set_data_dev(t, t_value); if (dirty & /*status*/ 2 && t_value !== (t_value = /*statusBadge*/ ctx[4][/*status*/ ctx[1].Status].text + "")) set_data_dev(t, t_value);
if (dirty & /*status*/ 2 && span_class_value !== (span_class_value = /*statusBadge*/ ctx[4][/*status*/ ctx[1].status].class)) { if (dirty & /*status*/ 2 && span_class_value !== (span_class_value = /*statusBadge*/ ctx[4][/*status*/ ctx[1].Status].class)) {
attr_dev(span, "class", span_class_value); attr_dev(span, "class", span_class_value);
} }
}, },
@ -1100,7 +1100,7 @@ var app = (function () {
block, block,
id: create_if_block$1.name, id: create_if_block$1.name,
type: "if", type: "if",
source: "(51:6) {#if status.status}", source: "(51:6) {#if status.Status}",
ctx ctx
}); });
@ -1115,7 +1115,7 @@ var app = (function () {
c: function create() { c: function create() {
p = element("p"); p = element("p");
p.textContent = "waiting..."; p.textContent = "waiting...";
add_location(p, file$2, 44, 2, 988); add_location(p, file$2, 44, 2, 986);
}, },
m: function mount(target, anchor) { m: function mount(target, anchor) {
insert_dev(target, p, anchor); insert_dev(target, p, anchor);
@ -1233,8 +1233,8 @@ var app = (function () {
$$invalidate(1, status = await res.json()); $$invalidate(1, status = await res.json());
if (res.ok) { if (res.ok) {
if (status.download_path) { if (status.DownloadPath) {
$$invalidate(2, download_path = `http://localhost:8080/${status.download_path}`); $$invalidate(2, download_path = `http://localhost:8080/${status.DownloadPath}`);
} }
return status; return status;

File diff suppressed because one or more lines are too long

View file

@ -27,8 +27,8 @@
status = await res.json(); status = await res.json();
if (res.ok) { if (res.ok) {
if (status.download_path) { if (status.DownloadPath) {
download_path = `http://localhost:8080/${status.download_path}`; download_path = `http://localhost:8080/${status.DownloadPath}`;
} }
return status; return status;
} else { } else {
@ -46,14 +46,14 @@
{:then video_info} {:then video_info}
<a href={download_path} class="list-group-item list-group-item-action" aria-current="true"> <a href={download_path} class="list-group-item list-group-item-action" aria-current="true">
<div class="d-flex bd-highlight"> <div class="d-flex bd-highlight">
<div class="pb-2 flex-grow-1 bd-highlight"><h5>{video_info.title}</h5></div> <div class="pb-2 flex-grow-1 bd-highlight"><h5>{video_info.Title}</h5></div>
<div class="pb-2 px-2 bd-highlight"><small>2 minutes ago</small></div> <div class="pb-2 px-2 bd-highlight"><small>2 minutes ago</small></div>
{#if status.status} {#if status.Status}
<div class="pb-2 px-2 bd-highlight"><span class={statusBadge[status.status].class}>{statusBadge[status.status].text}</span></div> <div class="pb-2 px-2 bd-highlight"><span class={statusBadge[status.Status].class}>{statusBadge[status.Status].text}</span></div>
{/if} {/if}
</div> </div>
<p><small>{url}</small></p> <p><small>{url}</small></p>
<img src={video_info.thumbnails.URL}/> <img src={video_info.Thumbnails[0].URL}/>
</a> </a>
{:catch error} {:catch error}
<p style="color: red">{error.message}</p> <p style="color: red">{error.message}</p>

View file

@ -30,7 +30,7 @@
} }
</script> </script>
<form class="needs-validation"> <form class="needs-validation mt-3">
<div class="input-group"> <div class="input-group">
<input bind:value={url} on:keyup={validateURL} type="url" class="form-control" placeholder="Insert a Youtube url here..." aria-label="URL for the Youtube video to download."> <input bind:value={url} on:keyup={validateURL} type="url" class="form-control" placeholder="Insert a Youtube url here..." aria-label="URL for the Youtube video to download.">
{#if url_is_ok} {#if url_is_ok}