Add config.js

This commit is contained in:
Andrea Fazzi 2021-11-08 09:24:49 +01:00
parent 3d4777bcf4
commit d9909a0f41
4 changed files with 16 additions and 14 deletions

View file

@ -46,10 +46,10 @@ func status(c *gin.Context, downloader youtube.Downloader) error {
return nil
}
func serveFile(c *gin.Context, filename string) error {
c.Writer.Header().Set("Content-Disposition", "attachment; filename="+strconv.Quote(tasks[filepath.Base(filename)].Filename))
func data(c *gin.Context, id string) error {
c.Writer.Header().Set("Content-Disposition", "attachment; filename="+strconv.Quote(tasks[id].Filename))
c.Writer.Header().Set("Content-Type", "application/octet-stream")
http.ServeFile(c.Writer, c.Request, filepath.Join("data", filename))
http.ServeFile(c.Writer, c.Request, filepath.Join("data", tasks[id].Filename))
return nil
}
@ -73,7 +73,7 @@ func main() {
}))
r.GET("/data/:filename", func(c *gin.Context) {
if err := serveFile(c, c.Param("filename")); err != nil {
if err := data(c, c.Param("filename")); err != nil {
panic(err)
}
})

View file

@ -9,7 +9,7 @@ const (
type Tasks map[string]*Task
type Task struct {
Status int
DownloadPath string
Filename string
Id string
Status int
Filename string
}

View file

@ -51,9 +51,9 @@ func (d *YoutubeDlDownloader) StartDownload(video *youtube.Video, tasks task.Tas
log.Printf("Download of video ID %s COMPLETED.", video.ID)
tasks[video.ID] = &task.Task{
Status: task.StatusCompleted,
DownloadPath: filepath.Join("data", video.ID+filepath.Ext(video.Filename)),
Filename: video.Filename,
Id: video.ID,
Status: task.StatusCompleted,
Filename: video.Filename,
}
}

View file

@ -1,4 +1,6 @@
<script>
import { config } from './config.js';
export let url;
let video_info = {};
@ -11,7 +13,7 @@
let download_path = "#";
async function startDownload() {
const res = await fetch(`https://yt-dls-api.andreafazzi.eu/download?url=${url}`);
const res = await fetch(`${config.endpoint}/download?url=${url}`);
video_info = await res.json();
if (res.ok) {
@ -23,12 +25,12 @@
}
async function getStatus() {
const res = await fetch(`https://yt-dls-api.andreafazzi.eu/status?url=${url}`);
const res = await fetch(`${config.endpoint}/status?url=${url}`);
status = await res.json();
if (res.ok) {
if (status.DownloadPath) {
download_path = `https://yt-dls-api.andreafazzi.eu/${status.DownloadPath}`;
download_path = `${config.endpoint}/data/${status.Id}`;
}
return status;
} else {
@ -38,7 +40,7 @@
}
const interval = setInterval(() => { getStatus(); }, 1000);
</script>
{#await startDownload()}