Skip to content

Commit 92adfbb

Browse files
committed
fix(downloader): check if the file already exists
1 parent 517ce04 commit 92adfbb

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

services/youtube/download/download.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ func init() {
1818
// YOUTUBE_MUSIC_DOWNLOAD_PATH, where the file name will be <video_id.mp3> to be served under /music/{id}
1919
// and retuens an occurring error
2020
func DownloadYoutubeVideo(id string) error {
21-
path := os.Getenv("YOUTUBE_MUSIC_DOWNLOAD_PATH")
22-
// TODO: check if the file is already downloaded!
21+
path := fmt.Sprintf("%s/%s.mp3", os.Getenv("YOUTUBE_MUSIC_DOWNLOAD_PATH"), id)
22+
if _, err := os.Stat(path); err == nil {
23+
log.Infof("The song with id %s is already downloaded\n", id)
24+
return nil
25+
}
2326
// TODO: write a downloader instead of using this sub process command thingy.
24-
cmd := exec.Command("yt-dlp", "-x", "--audio-format", "mp3", "--audio-quality", "best", "-o", fmt.Sprintf("%s/%s.%%(ext)s", path, id), "https://www.youtube.com/watch?v="+id)
27+
cmd := exec.Command("yt-dlp", "-x", "--audio-format", "mp3", "--audio-quality", "best", "-o", path, "https://www.youtube.com/watch?v="+id)
2528
cmd.Stdout = os.Stdout
2629
cmd.Stderr = os.Stderr
2730
err := cmd.Run()

0 commit comments

Comments
 (0)