Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/model/torrent.go

233 lignes
6,9 Kio
Go
Brut Vue normale Historique

package model
import (
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/config"
"github.com/NyaaPantsu/nyaa/util"
"github.com/bradfitz/slice"
"fmt"
"html/template"
"path/filepath"
"strconv"
"strings"
"time"
)
const (
TorrentStatusNormal = 1
TorrentStatusRemake = 2
TorrentStatusTrusted = 3
TorrentStatusAPlus = 4
)
type Feed struct {
ID int
Name string
Hash string
Magnet string
Timestamp string
}
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"`
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"`
DeletedAt *time.Time
2017-05-08 19:26:29 +02:00
2017-05-10 16:43:50 +02:00
Uploader *User `gorm:"ForeignKey:uploader"`
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"`
FileList []File `gorm:"ForeignKey:torrent_id"`
2017-05-10 10:27:17 +02:00
}
// Returns the total size of memory recursively allocated for this struct
2017-05-10 13:09:23 +02:00
// FIXME: doesn't go have sizeof or something nicer for this?
func (t Torrent) Size() (s int) {
s += 8 + // ints
2*3 + // time.Time
2 + // pointers
4*2 + // string pointers
2017-05-10 10:27:17 +02:00
// string array sizes
len(t.Name) + len(t.Hash) + len(t.Description) + len(t.WebsiteLink) +
2*2 // array pointers
2017-05-10 10:27:17 +02:00
s *= 8 // Assume 64 bit OS
if t.Uploader != nil {
s += t.Uploader.Size()
}
2017-05-10 10:27:17 +02:00
for _, c := range t.OldComments {
s += c.Size()
}
for _, c := range t.Comments {
s += c.Size()
}
2017-05-08 19:26:29 +02:00
2017-05-10 10:27:17 +02:00
return
}
func (t Torrent) TableName() string {
return config.TorrentsTableName
}
func (t Torrent) IsNormal() bool {
return t.Status == TorrentStatusNormal
}
func (t Torrent) IsRemake() bool {
return t.Status == TorrentStatusRemake
}
func (t Torrent) IsTrusted() bool {
return t.Status == TorrentStatusTrusted
}
func (t Torrent) IsAPlus() bool {
return t.Status == TorrentStatusAPlus
}
/* We need a JSON object instead of a Gorm structure because magnet URLs are
not in the database and have to be generated dynamically */
type ApiResultJSON struct {
Torrents []TorrentJSON `json:"torrents"`
QueryRecordCount int `json:"queryRecordCount"`
TotalRecordCount int `json:"totalRecordCount"`
}
type CommentJSON struct {
2017-05-08 04:06:11 +02:00
Username string `json:"username"`
2017-05-10 23:53:30 +02:00
UserID int `json:"user_id"`
2017-05-08 20:07:25 +02:00
Content template.HTML `json:"content"`
Date time.Time `json:"date"`
2017-05-08 04:06:11 +02:00
}
type FileJSON struct {
Path string `json:"path"`
Filesize string `json:"filesize"`
}
type TorrentJSON struct {
ID string `json:"id"`
Name string `json:"name"`
Status int `json:"status"`
Hash string `json:"hash"`
Date string `json:"date"`
Filesize string `json:"filesize"`
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"`
OldUploader template.HTML `json:"uploader_old"`
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"`
Completed uint32 `json:"completed"`
2017-05-10 19:29:35 +02:00
LastScrape time.Time `json:"last_scrape"`
FileList []FileJSON `json:"file_list"`
}
// ToJSON converts a model.Torrent to its equivalent JSON structure
func (t *Torrent) ToJSON() TorrentJSON {
2017-05-07 03:10:35 +02:00
magnet := util.InfoHashToMagnet(strings.TrimSpace(t.Hash), t.Name, config.Trackers...)
commentsJSON := make([]CommentJSON, 0, len(t.OldComments)+len(t.Comments))
2017-05-08 20:07:25 +02:00
for _, c := range t.OldComments {
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 {
commentsJSON = append(commentsJSON, CommentJSON{Username: c.User.Username, UserID: int(c.User.ID), Content: util.MarkdownToHTML(c.Content), Date: c.CreatedAt.UTC()})
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
}
// Sort comments by date
slice.Sort(commentsJSON, func(i, j int) bool {
return commentsJSON[i].Date.Before(commentsJSON[j].Date)
})
fileListJSON := make([]FileJSON, 0, len(t.FileList))
for _, f := range t.FileList {
fileListJSON = append(fileListJSON, FileJSON{
Path: filepath.Join(f.Path()...),
Filesize: util.FormatFilesize2(f.Filesize),
})
}
// Sort file list by lowercase filename
slice.Sort(fileListJSON, func(i, j int) bool {
return strings.ToLower(fileListJSON[i].Path) < strings.ToLower(fileListJSON[j].Path)
})
uploader := ""
if t.Uploader != nil {
uploader = t.Uploader.Username
}
torrentlink := ""
if t.ID <= config.LastOldTorrentID && len(config.TorrentCacheLink) > 0 {
torrentlink = fmt.Sprintf(config.TorrentCacheLink, t.Hash)
} else if t.ID > config.LastOldTorrentID && len(config.TorrentStorageLink) > 0 {
// TODO: Fix as part of configuration changes (fix what?)
torrentlink = fmt.Sprintf(config.TorrentStorageLink, t.Hash)
2017-05-08 04:06:11 +02:00
}
res := TorrentJSON{
ID: strconv.FormatUint(uint64(t.ID), 10),
Name: t.Name,
Status: t.Status,
Hash: t.Hash,
2017-05-11 00:06:21 +02:00
Date: t.Date.Format(time.RFC3339),
2017-05-07 13:51:59 +02:00
Filesize: util.FormatFilesize2(t.Filesize),
Description: util.MarkdownToHTML(t.Description),
Comments: commentsJSON,
SubCategory: strconv.Itoa(t.SubCategory),
Category: strconv.Itoa(t.Category),
2017-05-09 08:15:56 +02:00
Downloads: t.Downloads,
UploaderID: t.UploaderID,
UploaderName: util.SafeText(uploader),
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,
Completed: t.Completed,
2017-05-11 00:06:21 +02:00
LastScrape: t.LastScrape,
FileList: fileListJSON,
2017-05-10 19:29:35 +02:00
}
return res
}
/* Complete the functions when necessary... */
2017-05-09 01:56:57 +02:00
// Map Torrents to TorrentsToJSON without reallocations
func TorrentsToJSON(t []Torrent) []TorrentJSON { // TODO: Convert to singular version
json := make([]TorrentJSON, len(t))
2017-05-09 01:56:57 +02:00
for i := range t {
json[i] = t[i].ToJSON()
2017-05-09 01:56:57 +02:00
}
return json
}