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
}
func serveFile(c *gin.Context, filename string) error {
c.Writer.Header().Set("Content-Disposition", "attachment; filename="+strconv.Quote(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,13 +73,11 @@ 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)
}
})
// r.Static("/data", "./data")
r.GET("/download", func(c *gin.Context) {
if err := download(c, youtube_dl.NewYoutubeDlDownloader(c.Query("url"), "yt-dlp")); err != nil {
panic(err)

View file

@ -9,6 +9,7 @@ const (
type Tasks map[string]*Task
type Task struct {
Id string
Status int
DownloadPath string
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)
tasks[video.ID] = &task.Task{
Id: video.ID,
Status: task.StatusCompleted,
DownloadPath: filepath.Join("data", video.ID+filepath.Ext(video.Filename)),
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 {

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

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