downloader/download_torrent_file.go

42 lines
953 B
Go
Raw Normal View History

2022-08-23 11:48:26 +02:00
package main
import (
"fmt"
"io"
"net/http"
"os"
"path"
"path/filepath"
"git.milar.in/nyaanime/model"
)
func DownloadTorrent(animeEp model.AnimeEpisode, torrent *model.ParsedTorrent) error {
if err := SetAnimeEpDownloading(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
}