downloader/download_torrent_file.go

41 lines
950 B
Go

package main
import (
"io"
"net/http"
"os"
"path"
"path/filepath"
"git.milar.in/nyaanime/model"
)
func DownloadTorrent(animeEp model.AnimeEpisode, torrent *model.ParsedTorrent) error {
if err := SetCurrentlyDownloading(animeEp); err != nil {
return ErrLockFileCreationFailed.Wrap(err, animeEp.Anime.Title.Romaji, animeEp.Episode)
}
torrentFilePath := filepath.Join(TorrentPath, path.Base(torrent.Torrent.Link))
//fmt.Printf("download: %s -> %s\n", torrent.Torrent.Link, torrentFilePath)
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
}