2017-07-02 18:00:12 +02:00
package torrents
import (
"time"
"github.com/NyaaPantsu/nyaa/models"
"github.com/NyaaPantsu/nyaa/utils/log"
"github.com/NyaaPantsu/nyaa/utils/validator/torrent"
)
func Create ( user * models . User , uploadForm * torrentValidator . TorrentRequest ) ( * models . Torrent , error ) {
torrent := models . Torrent {
Name : uploadForm . Name ,
Category : uploadForm . CategoryID ,
SubCategory : uploadForm . SubCategoryID ,
Status : uploadForm . Status ,
Hidden : uploadForm . Hidden ,
Hash : uploadForm . Infohash ,
Date : time . Now ( ) ,
Filesize : uploadForm . Filesize ,
2017-07-04 02:07:25 +02:00
Languages : uploadForm . Languages ,
2017-07-02 18:00:12 +02:00
Description : uploadForm . Description ,
WebsiteLink : uploadForm . WebsiteLink ,
UploaderID : user . ID }
2017-07-04 02:07:25 +02:00
torrent . EncodeLanguages ( ) // Convert languages array in language string
2017-07-02 18:00:12 +02:00
torrent . ParseTrackers ( uploadForm . Trackers )
models . ORM . Create ( & torrent )
if models . ElasticSearchClient != nil {
err := torrent . AddToESIndex ( models . ElasticSearchClient )
if err == nil {
log . Infof ( "Successfully added torrent to ES index." )
} else {
log . Errorf ( "Unable to add torrent to ES index: %s" , err )
}
} else {
log . Error ( "Unable to create elasticsearch client" )
}
NewTorrentEvent ( user , & torrent )
if len ( uploadForm . FileList ) > 0 {
for _ , uploadedFile := range uploadForm . FileList {
file := models . File { TorrentID : torrent . ID , Filesize : uploadForm . Filesize }
err := file . SetPath ( uploadedFile . Path )
if err != nil {
return & torrent , err
}
models . ORM . Create ( & file )
}
}
return & torrent , nil
}