Merge branch 'master' of ssh://git.andreafazzi.eu:10022/andrea/youtube-dl-service

This commit is contained in:
Andrea Fazzi 2021-11-09 06:36:02 +01:00
commit 1ffc77d980
5 changed files with 19 additions and 14 deletions

View file

@ -46,10 +46,10 @@ func status(c *gin.Context, downloader youtube.Downloader) error {
return nil return nil
} }
func serveFile(c *gin.Context, filename string) error { func data(c *gin.Context, id string) error {
c.Writer.Header().Set("Content-Disposition", "attachment; filename="+strconv.Quote(filename)) c.Writer.Header().Set("Content-Disposition", "attachment; filename="+strconv.Quote(tasks[id].Filename))
c.Writer.Header().Set("Content-Type", "application/octet-stream") 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 return nil
} }
@ -73,13 +73,11 @@ func main() {
})) }))
r.GET("/data/:filename", func(c *gin.Context) { 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) 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 {
panic(err) panic(err)

View file

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

View file

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

View file

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

3
frontend/src/config.js Normal file
View file

@ -0,0 +1,3 @@
export const config = {
endpoint: "http://localhost:8080"
}