2017-05-05 14:20:51 +02:00
package model
import (
2017-05-27 00:45:18 +02:00
"context"
2017-05-09 16:42:12 +02:00
"fmt"
2017-05-05 14:20:51 +02:00
"html/template"
2017-05-15 23:45:47 +02:00
"path/filepath"
2017-05-27 00:45:18 +02:00
"reflect"
2017-05-05 14:20:51 +02:00
"strconv"
"strings"
"time"
2017-05-27 00:45:18 +02:00
2017-05-26 01:48:14 +02:00
elastic "gopkg.in/olivere/elastic.v5"
2017-05-27 00:45:18 +02:00
"net/url"
2017-05-26 01:48:14 +02:00
"github.com/NyaaPantsu/nyaa/config"
"github.com/NyaaPantsu/nyaa/util"
"github.com/bradfitz/slice"
2017-05-05 14:20:51 +02:00
)
2017-05-20 01:10:16 +02:00
const (
2017-05-26 12:12:52 +02:00
// TorrentStatusNormal Int for Torrent status normal
TorrentStatusNormal = 1
// TorrentStatusRemake Int for Torrent status remake
TorrentStatusRemake = 2
// TorrentStatusTrusted Int for Torrent status trusted
2017-05-25 23:35:37 +02:00
TorrentStatusTrusted = 3
2017-05-26 12:12:52 +02:00
// TorrentStatusAPlus Int for Torrent status a+
TorrentStatusAPlus = 4
// TorrentStatusBlocked Int for Torrent status locked
2017-05-25 23:35:37 +02:00
TorrentStatusBlocked = 5
2017-05-20 01:10:16 +02:00
)
2017-05-26 12:12:52 +02:00
// Feed struct
2017-05-05 14:20:51 +02:00
type Feed struct {
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
ID int
2017-05-05 14:20:51 +02:00
Name string
Hash string
Magnet string
Timestamp string
}
2017-05-26 12:12:52 +02:00
// Torrent model
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
type Torrent struct {
2017-05-11 00:06:21 +02:00
ID uint ` gorm:"column:torrent_id;primary_key" `
Name string ` gorm:"column:torrent_name" `
Hash string ` gorm:"column:torrent_hash" `
Category int ` gorm:"column:category" `
SubCategory int ` gorm:"column:sub_category" `
Status int ` gorm:"column:status" `
2017-05-27 20:33:40 +02:00
Hidden bool ` gorm:"column:hidden" `
2017-05-11 00:06:21 +02:00
Date time . Time ` gorm:"column:date" `
UploaderID uint ` gorm:"column:uploader" `
Downloads int ` gorm:"column:downloads" `
Stardom int ` gorm:"column:stardom" `
Filesize int64 ` gorm:"column:filesize" `
Description string ` gorm:"column:description" `
WebsiteLink string ` gorm:"column:website_link" `
2017-05-27 00:45:18 +02:00
Trackers string ` gorm:"column:trackers" `
2017-05-10 10:47:06 +02:00
DeletedAt * time . Time
2017-05-08 19:26:29 +02:00
2017-05-20 13:45:15 +02:00
Uploader * User ` gorm:"AssociationForeignKey:UploaderID;ForeignKey:user_id" `
2017-05-10 13:32:45 +02:00
OldUploader string ` gorm:"-" ` // ???????
2017-05-09 01:56:57 +02:00
OldComments [ ] OldComment ` gorm:"ForeignKey:torrent_id" `
Comments [ ] Comment ` gorm:"ForeignKey:torrent_id" `
2017-05-10 19:29:35 +02:00
2017-05-11 00:06:21 +02:00
Seeders uint32 ` gorm:"column:seeders" `
Leechers uint32 ` gorm:"column:leechers" `
Completed uint32 ` gorm:"column:completed" `
LastScrape time . Time ` gorm:"column:last_scrape" `
2017-05-14 04:12:18 +02:00
FileList [ ] File ` gorm:"ForeignKey:torrent_id" `
2017-05-10 10:27:17 +02:00
}
2017-05-26 12:12:52 +02:00
// Size : Returns the total size of memory recursively allocated for this struct
2017-05-27 00:45:18 +02:00
// FIXME: Is it deprecated?
2017-05-10 10:47:06 +02:00
func ( t Torrent ) Size ( ) ( s int ) {
2017-05-27 00:45:18 +02:00
s = int ( reflect . TypeOf ( t ) . Size ( ) )
2017-05-10 10:27:17 +02:00
return
2017-05-10 11:08:02 +02:00
}
2017-05-26 12:12:52 +02:00
// TableName : Return the name of torrents table
2017-05-16 04:53:02 +02:00
func ( t Torrent ) TableName ( ) string {
2017-05-31 04:21:57 +02:00
return config . Conf . Models . TorrentsTableName
2017-05-20 01:10:16 +02:00
}
2017-05-26 12:12:52 +02:00
// Identifier : Return the identifier of a torrent
2017-05-22 00:22:42 +02:00
func ( t * Torrent ) Identifier ( ) string {
2017-05-24 09:11:13 +02:00
return "torrent_" + strconv . Itoa ( int ( t . ID ) )
2017-05-20 20:53:05 +02:00
}
2017-05-26 12:12:52 +02:00
// IsNormal : Return if a torrent status is normal
2017-05-20 01:10:16 +02:00
func ( t Torrent ) IsNormal ( ) bool {
return t . Status == TorrentStatusNormal
}
2017-05-26 12:12:52 +02:00
// IsRemake : Return if a torrent status is normal
2017-05-20 01:10:16 +02:00
func ( t Torrent ) IsRemake ( ) bool {
return t . Status == TorrentStatusRemake
}
2017-05-26 12:12:52 +02:00
// IsTrusted : Return if a torrent status is trusted
2017-05-20 01:10:16 +02:00
func ( t Torrent ) IsTrusted ( ) bool {
return t . Status == TorrentStatusTrusted
}
2017-05-26 12:12:52 +02:00
// IsAPlus : Return if a torrent status is a+
2017-05-20 01:10:16 +02:00
func ( t Torrent ) IsAPlus ( ) bool {
return t . Status == TorrentStatusAPlus
2017-05-16 04:53:02 +02:00
}
2017-05-26 12:12:52 +02:00
// IsBlocked : Return if a torrent status is locked
2017-05-25 02:19:05 +02:00
func ( t * Torrent ) IsBlocked ( ) bool {
return t . Status == TorrentStatusBlocked
}
2017-05-26 12:12:52 +02:00
// IsDeleted : Return if a torrent status is deleted
2017-05-25 02:19:05 +02:00
func ( t * Torrent ) IsDeleted ( ) bool {
return t . DeletedAt != nil
}
2017-05-27 00:45:18 +02:00
// AddToESIndex : Adds a torrent to Elastic Search
2017-05-26 01:48:14 +02:00
func ( t Torrent ) AddToESIndex ( client * elastic . Client ) error {
ctx := context . Background ( )
2017-05-27 00:45:18 +02:00
torrentJSON := t . ToJSON ( )
2017-05-26 01:48:14 +02:00
_ , err := client . Index ( ) .
2017-05-31 04:21:57 +02:00
Index ( config . Conf . Search . ElasticsearchIndex ) .
Type ( config . Conf . Search . ElasticsearchType ) .
2017-05-27 03:54:41 +02:00
Id ( strconv . FormatUint ( uint64 ( torrentJSON . ID ) , 10 ) ) .
2017-05-27 00:45:18 +02:00
BodyJson ( torrentJSON ) .
2017-05-26 01:48:14 +02:00
Refresh ( "true" ) .
Do ( ctx )
return err
}
2017-05-27 00:45:18 +02:00
// DeleteFromESIndex : Removes a torrent from Elastic Search
2017-05-26 01:48:14 +02:00
func ( t Torrent ) DeleteFromESIndex ( client * elastic . Client ) error {
ctx := context . Background ( )
_ , err := client . Delete ( ) .
2017-05-31 04:21:57 +02:00
Index ( config . Conf . Search . ElasticsearchIndex ) .
Type ( config . Conf . Search . ElasticsearchType ) .
2017-05-26 01:48:14 +02:00
Id ( strconv . FormatInt ( int64 ( t . ID ) , 10 ) ) .
Do ( ctx )
return err
}
2017-05-27 00:45:18 +02:00
// ParseTrackers : Takes an array of trackers, adds needed trackers and parse it to url string
func ( t * Torrent ) ParseTrackers ( trackers [ ] string ) {
v := url . Values { }
2017-05-31 04:21:57 +02:00
if len ( config . Conf . Torrents . Trackers . NeededTrackers ) > 0 { // if we have some needed trackers configured
2017-05-27 00:45:18 +02:00
if len ( trackers ) == 0 {
2017-05-31 04:21:57 +02:00
trackers = config . Conf . Torrents . Trackers . Default
2017-05-27 00:45:18 +02:00
} else {
2017-05-31 04:21:57 +02:00
for _ , id := range config . Conf . Torrents . Trackers . NeededTrackers {
2017-05-27 00:45:18 +02:00
found := false
for _ , tracker := range trackers {
2017-05-31 04:21:57 +02:00
if tracker == config . Conf . Torrents . Trackers . Default [ id ] {
2017-05-27 00:45:18 +02:00
found = true
break
}
}
if ! found {
2017-05-31 04:21:57 +02:00
trackers = append ( trackers , config . Conf . Torrents . Trackers . Default [ id ] )
2017-05-27 00:45:18 +02:00
}
}
}
}
v [ "tr" ] = trackers
t . Trackers = v . Encode ( )
}
// GetTrackersArray : Convert trackers string to Array
func ( t * Torrent ) GetTrackersArray ( ) ( trackers [ ] string ) {
v , _ := url . ParseQuery ( t . Trackers )
trackers = v [ "tr" ]
return
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
/ * We need a JSON object instead of a Gorm structure because magnet URLs are
not in the database and have to be generated dynamically * /
2017-05-05 14:20:51 +02:00
2017-05-26 12:12:52 +02:00
// APIResultJSON for torrents in json for api
type APIResultJSON struct {
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
Torrents [ ] TorrentJSON ` json:"torrents" `
QueryRecordCount int ` json:"queryRecordCount" `
TotalRecordCount int ` json:"totalRecordCount" `
2017-05-05 14:20:51 +02:00
}
2017-05-26 12:12:52 +02:00
// CommentJSON for comment model in json
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
type CommentJSON struct {
2017-05-24 09:11:13 +02:00
Username string ` json:"username" `
UserID int ` json:"user_id" `
UserAvatar string ` json:"user_avatar" `
Content template . HTML ` json:"content" `
Date time . Time ` json:"date" `
2017-05-08 04:06:11 +02:00
}
2017-05-26 12:12:52 +02:00
// FileJSON for file model in json
2017-05-14 04:12:18 +02:00
type FileJSON struct {
2017-05-14 15:19:19 +02:00
Path string ` json:"path" `
2017-05-21 14:34:32 +02:00
Filesize int64 ` json:"filesize" `
2017-05-14 04:12:18 +02:00
}
2017-05-26 12:12:52 +02:00
// TorrentJSON for torrent model in json for api
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
type TorrentJSON struct {
2017-05-27 03:54:41 +02:00
ID uint ` json:"id" `
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
Name string ` json:"name" `
Status int ` json:"status" `
Hash string ` json:"hash" `
Date string ` json:"date" `
2017-05-21 14:34:32 +02:00
Filesize int64 ` json:"filesize" `
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
Description template . HTML ` json:"description" `
Comments [ ] CommentJSON ` json:"comments" `
SubCategory string ` json:"sub_category" `
Category string ` json:"category" `
Downloads int ` json:"downloads" `
UploaderID uint ` json:"uploader_id" `
UploaderName template . HTML ` json:"uploader_name" `
2017-05-10 13:32:45 +02:00
OldUploader template . HTML ` json:"uploader_old" `
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
WebsiteLink template . URL ` json:"website_link" `
Magnet template . URL ` json:"magnet" `
TorrentLink template . URL ` json:"torrent" `
2017-05-10 19:29:35 +02:00
Seeders uint32 ` json:"seeders" `
Leechers uint32 ` json:"leechers" `
2017-05-11 01:31:10 +02:00
Completed uint32 ` json:"completed" `
2017-05-10 19:29:35 +02:00
LastScrape time . Time ` json:"last_scrape" `
2017-05-14 15:19:19 +02:00
FileList [ ] FileJSON ` json:"file_list" `
2017-05-05 14:20:51 +02:00
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
// ToJSON converts a model.Torrent to its equivalent JSON structure
func ( t * Torrent ) ToJSON ( ) TorrentJSON {
2017-05-27 00:45:18 +02:00
var trackers [ ] string
if t . Trackers == "" {
2017-05-31 04:21:57 +02:00
trackers = config . Conf . Torrents . Trackers . Default
2017-05-27 00:45:18 +02:00
} else {
trackers = t . GetTrackersArray ( )
}
magnet := util . InfoHashToMagnet ( strings . TrimSpace ( t . Hash ) , t . Name , trackers ... )
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
commentsJSON := make ( [ ] CommentJSON , 0 , len ( t . OldComments ) + len ( t . Comments ) )
2017-05-08 20:07:25 +02:00
for _ , c := range t . OldComments {
2017-05-11 13:15:52 +02:00
commentsJSON = append ( commentsJSON , CommentJSON { Username : c . Username , UserID : - 1 , Content : template . HTML ( c . Content ) , Date : c . Date . UTC ( ) } )
2017-05-08 20:07:25 +02:00
}
for _ , c := range t . Comments {
2017-05-17 07:58:40 +02:00
if c . User != nil {
2017-05-22 09:28:08 +02:00
commentsJSON = append ( commentsJSON , CommentJSON { Username : c . User . Username , UserID : int ( c . User . ID ) , Content : util . MarkdownToHTML ( c . Content ) , Date : c . CreatedAt . UTC ( ) , UserAvatar : c . User . MD5 } )
2017-05-17 00:05:16 +02:00
} else {
2017-05-17 07:58:40 +02:00
commentsJSON = append ( commentsJSON , CommentJSON { } )
2017-05-17 00:05:16 +02:00
}
2017-05-08 04:06:11 +02:00
}
2017-05-17 13:16:40 +02:00
// Sort comments by date
slice . Sort ( commentsJSON , func ( i , j int ) bool {
return commentsJSON [ i ] . Date . Before ( commentsJSON [ j ] . Date )
} )
2017-05-14 15:19:19 +02:00
fileListJSON := make ( [ ] FileJSON , 0 , len ( t . FileList ) )
for _ , f := range t . FileList {
2017-05-15 23:45:47 +02:00
fileListJSON = append ( fileListJSON , FileJSON {
2017-05-16 04:53:02 +02:00
Path : filepath . Join ( f . Path ( ) ... ) ,
2017-05-21 14:34:32 +02:00
Filesize : f . Filesize ,
2017-05-15 23:45:47 +02:00
} )
2017-05-14 15:19:19 +02:00
}
2017-05-17 13:16:40 +02:00
// Sort file list by lowercase filename
2017-05-20 01:10:16 +02:00
slice . Sort ( fileListJSON , func ( i , j int ) bool {
2017-05-17 13:16:40 +02:00
return strings . ToLower ( fileListJSON [ i ] . Path ) < strings . ToLower ( fileListJSON [ j ] . Path )
} )
2017-05-09 12:35:46 +02:00
uploader := ""
2017-05-27 20:33:40 +02:00
var uploaderID uint
2017-05-27 20:50:40 +02:00
if t . Hidden {
uploader = "れんちょん"
uploaderID = 0
} else if t . Uploader != nil {
2017-05-09 12:35:46 +02:00
uploader = t . Uploader . Username
2017-05-27 20:33:40 +02:00
uploaderID = t . UploaderID
2017-05-09 12:35:46 +02:00
}
2017-05-09 16:42:12 +02:00
torrentlink := ""
2017-05-31 04:21:57 +02:00
if t . ID <= config . Conf . Models . LastOldTorrentID && len ( config . Conf . Torrents . CacheLink ) > 0 {
2017-05-20 12:45:27 +02:00
if config . IsSukebei ( ) {
torrentlink = "" // torrent cache doesn't have sukebei torrents
} else {
2017-05-31 04:21:57 +02:00
torrentlink = fmt . Sprintf ( config . Conf . Torrents . CacheLink , t . Hash )
2017-05-20 12:45:27 +02:00
}
2017-05-31 04:21:57 +02:00
} else if t . ID > config . Conf . Models . LastOldTorrentID && len ( config . Conf . Torrents . StorageLink ) > 0 {
torrentlink = fmt . Sprintf ( config . Conf . Torrents . StorageLink , t . Hash )
2017-05-08 04:06:11 +02:00
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
res := TorrentJSON {
2017-05-27 03:54:41 +02:00
ID : t . ID ,
2017-05-09 12:35:46 +02:00
Name : t . Name ,
2017-05-05 14:20:51 +02:00
Status : t . Status ,
Hash : t . Hash ,
2017-05-11 00:06:21 +02:00
Date : t . Date . Format ( time . RFC3339 ) ,
2017-05-21 14:34:32 +02:00
Filesize : t . Filesize ,
2017-05-09 09:28:29 +02:00
Description : util . MarkdownToHTML ( t . Description ) ,
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
Comments : commentsJSON ,
SubCategory : strconv . Itoa ( t . SubCategory ) ,
2017-05-06 23:46:53 +02:00
Category : strconv . Itoa ( t . Category ) ,
2017-05-09 08:15:56 +02:00
Downloads : t . Downloads ,
2017-05-27 20:33:40 +02:00
UploaderID : uploaderID ,
2017-05-09 12:35:46 +02:00
UploaderName : util . SafeText ( uploader ) ,
2017-05-10 13:32:45 +02:00
OldUploader : util . SafeText ( t . OldUploader ) ,
2017-05-09 08:15:56 +02:00
WebsiteLink : util . Safe ( t . WebsiteLink ) ,
2017-05-10 21:48:08 +02:00
Magnet : template . URL ( magnet ) ,
2017-05-10 19:29:35 +02:00
TorrentLink : util . Safe ( torrentlink ) ,
Leechers : t . Leechers ,
Seeders : t . Seeders ,
2017-05-11 01:31:10 +02:00
Completed : t . Completed ,
2017-05-11 00:06:21 +02:00
LastScrape : t . LastScrape ,
2017-05-14 15:19:19 +02:00
FileList : fileListJSON ,
2017-05-10 19:29:35 +02:00
}
2017-05-05 14:20:51 +02:00
return res
}
/* Complete the functions when necessary... */
2017-05-09 01:56:57 +02:00
2017-05-26 12:12:52 +02:00
// TorrentsToJSON : Map Torrents to TorrentsToJSON without reallocations
2017-05-20 12:45:27 +02:00
func TorrentsToJSON ( t [ ] Torrent ) [ ] TorrentJSON {
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
json := make ( [ ] TorrentJSON , len ( t ) )
2017-05-09 01:56:57 +02:00
for i := range t {
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
json [ i ] = t [ i ] . ToJSON ( )
2017-05-09 01:56:57 +02:00
}
return json
}