Skip to content

Commit

Permalink
Modified the behaviour to generate torrent preivously. It is a synchr…
Browse files Browse the repository at this point in the history
…one function now. So we need to make it asynchronous in download.go

Now torrents file are always generated at upload time in a background processus with the correct trackers (needed ones + the ones provided by the user).
I made the anidex tracker as a needed one to allow multiupload even if the user hasn't included in his torrent file/magnet url.
  • Loading branch information
akuma06 committed Oct 31, 2017
1 parent 5a6063d commit 0acc4d7
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 41 deletions.
1 change: 1 addition & 0 deletions config/default_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ torrents:
# NeededTrackers : Array indexes of Trackers for needed tracker in a torrent file
needed:
- 0
- 12
# TorrentOrder : Default sorting field for torrents
order: date
# TorrentSort : Default sorting order for torrents
Expand Down
9 changes: 2 additions & 7 deletions controllers/torrent/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ func DownloadTorrent(c *gin.Context) {
trackers = torrent.GetTrackersArray()
}
magnet := format.InfoHashToMagnet(strings.TrimSpace(torrent.Hash), torrent.Name, trackers...)
if upload.GenerateTorrent(magnet) != nil {
//Error during the generation
generating = false
}
go upload.GenerateTorrent(magnet)
}
}
c.JSON(200, gin.H{ // Better to use gin for that, less code
Expand Down Expand Up @@ -94,9 +91,7 @@ func DownloadTorrent(c *gin.Context) {
}
magnet := format.InfoHashToMagnet(strings.TrimSpace(torrent.Hash), torrent.Name, trackers...)
variables.Set("magnet", magnet)
if upload.GenerateTorrent(magnet) != nil {
messages.AddError("errors", "Could not generate torrent file")
}
go upload.GenerateTorrent(magnet)
templates.Render(c, "errors/torrent_file_missing.jet.html", variables)
return
}
Expand Down
12 changes: 8 additions & 4 deletions controllers/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ func UploadPostHandler(c *gin.Context) {
// add to db
torrent, err := torrents.Create(user, &uploadForm)
log.CheckErrorWithMessage(err, "ERROR_TORRENT_CREATED: Error while creating entry in db")

if AnidexUpload || NyaaSiUpload || TokyoToshoUpload {
go func(anidexApiKey string, anidexFormCategory string, anidexFormLang string, nyaasiApiKey string, toshoApiKey string) {
err := upload.GotFile(torrent, uploadForm.Magnet)
err := upload.GotFile(torrent)
if err != nil {
log.CheckError(err)
return
Expand All @@ -108,10 +109,13 @@ func UploadPostHandler(c *gin.Context) {
// After that, we redirect to the page for upload status
url := fmt.Sprintf("/upload/status/%d", torrent.ID)
c.Redirect(302, url)
} else {
url := "/view/" + strconv.FormatUint(uint64(torrent.ID), 10)
c.Redirect(302, url+"?success")
return
}

// We don't need it to be synchronous since the generation can be left in a background process
go upload.GotFile(torrent)
url := "/view/" + strconv.FormatUint(uint64(torrent.ID), 10)
c.Redirect(302, url+"?success")
}
}

Expand Down
12 changes: 11 additions & 1 deletion utils/upload/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package upload
import (
"fmt"
"os"
"strings"
"time"

"github.com/NyaaPantsu/nyaa/config"
"github.com/NyaaPantsu/nyaa/models"
"github.com/NyaaPantsu/nyaa/utils/format"
"github.com/NyaaPantsu/nyaa/utils/log"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/bencode"
Expand Down Expand Up @@ -76,7 +78,15 @@ func GenerateTorrent(magnet string) error {
}

// GotFile will check if a torrent file exists and if not, try to generate it
func GotFile(torrent *models.Torrent, magnet string) error {
func GotFile(torrent *models.Torrent) error {
var trackers []string
if torrent.Trackers == "" {
trackers = config.Get().Torrents.Trackers.Default
} else {
trackers = torrent.GetTrackersArray()
}
// We generate a new magnet link with all trackers (ours + ones from uploader)
magnet := format.InfoHashToMagnet(strings.TrimSpace(torrent.Hash), torrent.Name, trackers...)
//Check if file exists and open
_, err := os.Open(torrent.GetPath())
if err != nil {
Expand Down
32 changes: 3 additions & 29 deletions utils/upload/upload.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package upload

import (
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"os"
"reflect"
"strings"

Expand Down Expand Up @@ -123,7 +118,7 @@ func ExtractInfo(c *gin.Context, r *torrentValidator.TorrentRequest) error {
return err
}

tfile, err := r.ValidateMultipartUpload(c, uploadFormTorrent)
_, err = r.ValidateMultipartUpload(c, uploadFormTorrent)
if err != nil {
return err
}
Expand All @@ -133,16 +128,8 @@ func ExtractInfo(c *gin.Context, r *torrentValidator.TorrentRequest) error {
if err != nil {
return err
}

// after data has been checked & extracted, write it to disk
if len(config.Get().Torrents.FileStorage) > 0 && r.Filesize > 0 {
err := writeTorrentToDisk(tfile, r.Infohash+".torrent", &r.Filepath)
if err != nil {
return err
}
} else {
r.Filepath = ""
}
// We are not saving the file here because we need to add our own tracker list to the torrent file, therefore, we generate the torrent file at upload time through GenerateTorrent()
// when it is magnet or torrent file upload

return nil
}
Expand Down Expand Up @@ -210,19 +197,6 @@ func UpdateUnscopeTorrent(r *torrentValidator.UpdateRequest, t *models.Torrent,
return t
}

func writeTorrentToDisk(file multipart.File, name string, fullpath *string) error {
_, seekErr := file.Seek(0, io.SeekStart)
if seekErr != nil {
return seekErr
}
b, err := ioutil.ReadAll(file)
if err != nil {
return err
}
*fullpath = fmt.Sprintf("%s%c%s", config.Get().Torrents.FileStorage, os.PathSeparator, name)
return ioutil.WriteFile(*fullpath, b, 0644)
}

// NewTorrentRequest : creates a new torrent request struc with some default value
func NewTorrentRequest(params ...string) *torrentValidator.TorrentRequest {
torrentRequest := &torrentValidator.TorrentRequest{}
Expand Down

0 comments on commit 0acc4d7

Please sign in to comment.