initial commit
This commit is contained in:
40
download_torrent_file.go
Normal file
40
download_torrent_file.go
Normal file
@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"git.tordarus.net/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
|
||||
}
|
Reference in New Issue
Block a user