downloader/download_torrent_file.go

41 lines
950 B
Go
Raw Permalink Normal View History

2022-08-23 11:48:26 +02:00
package main
import (
"io"
"net/http"
"os"
"path"
"path/filepath"
"git.milar.in/nyaanime/model"
)
func DownloadTorrent(animeEp model.AnimeEpisode, torrent *model.ParsedTorrent) error {
2022-08-23 13:24:03 +02:00
if err := SetCurrentlyDownloading(animeEp); err != nil {
2022-08-23 11:48:26 +02:00
return ErrLockFileCreationFailed.Wrap(err, animeEp.Anime.Title.Romaji, animeEp.Episode)
}
torrentFilePath := filepath.Join(TorrentPath, path.Base(torrent.Torrent.Link))
2022-08-25 23:25:48 +02:00
//fmt.Printf("download: %s -> %s\n", torrent.Torrent.Link, torrentFilePath)
2022-08-23 11:48:26 +02:00
resp, err := http.Get(torrent.Torrent.Link)
if err != nil {
return ErrDownloadTorrentFileFailed.Wrap(err, torrent.Torrent.Link)
}
defer resp.Body.Close()
file, err := os.Create(torrentFilePath)
if err != nil {
return ErrSaveTorrentFileFailed.Wrap(err, torrent.Torrent.Link)
}
defer file.Close()
_, err = io.Copy(file, resp.Body)
if err != nil {
return ErrSaveTorrentFileFailed.Wrap(err, torrent.Torrent.Link)
}
return nil
}