Also read file lists from metadata, fix Length == 0
Cette révision appartient à :
Parent
a1dc2d3587
révision
80034bb52b
4 fichiers modifiés avec 66 ajouts et 4 suppressions
|
@ -58,7 +58,7 @@ func GormInit(conf *config.Config, logger Logger) (*gorm.DB, error) {
|
|||
if db.Error != nil {
|
||||
return db, db.Error
|
||||
}
|
||||
db.AutoMigrate(&model.Torrent{}, &model.TorrentReport{})
|
||||
db.AutoMigrate(&model.Torrent{}, &model.TorrentReport{}, &model.File{})
|
||||
if db.Error != nil {
|
||||
return db, db.Error
|
||||
}
|
||||
|
|
16
model/file.go
Fichier normal
16
model/file.go
Fichier normal
|
@ -0,0 +1,16 @@
|
|||
package model
|
||||
|
||||
type File struct {
|
||||
ID uint `gorm:"column:file_id;primary_key"`
|
||||
TorrentID uint `gorm:"column:torrent_id"`
|
||||
Path string `gorm:"column:path"`
|
||||
Filesize int64 `gorm:"column:filesize"`
|
||||
|
||||
Torrent *Torrent `gorm:"AssociationForeignKey:TorrentID;ForeignKey:torrent_id"`
|
||||
}
|
||||
|
||||
// Returns the total size of memory allocated for this struct
|
||||
func (f File) Size() int {
|
||||
return (1 + len(f.Path) + 2) * 8;
|
||||
}
|
||||
|
|
@ -44,6 +44,7 @@ type Torrent struct {
|
|||
Leechers uint32 `gorm:"column:leechers"`
|
||||
Completed uint32 `gorm:"column:completed"`
|
||||
LastScrape time.Time `gorm:"column:last_scrape"`
|
||||
FileList []File `gorm:"ForeignKey:torrent_id"`
|
||||
}
|
||||
|
||||
// Returns the total size of memory recursively allocated for this struct
|
||||
|
@ -88,6 +89,11 @@ type CommentJSON struct {
|
|||
Date time.Time `json:"date"`
|
||||
}
|
||||
|
||||
type FileJSON struct {
|
||||
Path string `json:"path"`
|
||||
Length int64 `json:"length"`
|
||||
}
|
||||
|
||||
type TorrentJSON struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
|
@ -110,6 +116,7 @@ type TorrentJSON struct {
|
|||
Leechers uint32 `json:"leechers"`
|
||||
Completed uint32 `json:"completed"`
|
||||
LastScrape time.Time `json:"last_scrape"`
|
||||
FileList []File `json:"file_list"`
|
||||
}
|
||||
|
||||
// ToJSON converts a model.Torrent to its equivalent JSON structure
|
||||
|
@ -155,6 +162,7 @@ func (t *Torrent) ToJSON() TorrentJSON {
|
|||
Seeders: t.Seeders,
|
||||
Completed: t.Completed,
|
||||
LastScrape: t.LastScrape,
|
||||
FileList: t.FileList,
|
||||
}
|
||||
|
||||
return res
|
||||
|
|
|
@ -2,7 +2,9 @@ package filesizeFetcher;
|
|||
|
||||
import (
|
||||
"github.com/anacrolix/torrent"
|
||||
"github.com/anacrolix/torrent/metainfo"
|
||||
"github.com/ewhal/nyaa/config"
|
||||
"github.com/ewhal/nyaa/db"
|
||||
"github.com/ewhal/nyaa/model"
|
||||
"github.com/ewhal/nyaa/util/log"
|
||||
serviceBase "github.com/ewhal/nyaa/service"
|
||||
|
@ -79,21 +81,57 @@ func (fetcher *FilesizeFetcher) removeFromQueue(op *FetchOperation) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func updateFileList(dbEntry model.Torrent, info *metainfo.Info) error {
|
||||
log.Infof("TID %d has %d files.", dbEntry.ID, len(info.Files))
|
||||
for _, file := range info.Files {
|
||||
path := file.DisplayPath(info)
|
||||
fileExists := false
|
||||
for _, existingFile := range dbEntry.FileList {
|
||||
if existingFile.Path == path {
|
||||
fileExists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !fileExists {
|
||||
log.Infof("Adding file %s to filelist of TID %d", path, dbEntry.ID)
|
||||
dbFile := model.File{
|
||||
TorrentID: dbEntry.ID,
|
||||
Path: path,
|
||||
Filesize: file.Length,
|
||||
}
|
||||
|
||||
err := db.ORM.Create(&dbFile).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (fetcher *FilesizeFetcher) gotResult(r Result) {
|
||||
updatedSuccessfully := false
|
||||
if r.err != nil {
|
||||
log.Infof("Failed to get torrent filesize (TID: %d), err %v", r.operation.torrent.ID, r.err)
|
||||
} else if r.info.Length == 0 {
|
||||
} else if r.info.TotalLength() == 0 {
|
||||
log.Infof("Got length 0 for torrent TID: %d. Possible bug?", r.operation.torrent.ID)
|
||||
} else {
|
||||
log.Infof("Got length %d for torrent TID: %d. Updating.", r.info.Length, r.operation.torrent.ID)
|
||||
r.operation.torrent.Filesize = r.info.Length
|
||||
log.Infof("Got length %d for torrent TID: %d. Updating.", r.info.TotalLength(), r.operation.torrent.ID)
|
||||
r.operation.torrent.Filesize = r.info.TotalLength()
|
||||
_, err := torrentService.UpdateTorrent(r.operation.torrent)
|
||||
if err != nil {
|
||||
log.Infof("Failed to update torrent TID: %d with new filesize", r.operation.torrent.ID)
|
||||
} else {
|
||||
updatedSuccessfully = true
|
||||
}
|
||||
|
||||
// Also update the File list with FileInfo, I guess.
|
||||
err = updateFileList(r.operation.torrent, r.info)
|
||||
if err != nil {
|
||||
log.Infof("Failed to update file list of TID %d", r.operation.torrent.ID)
|
||||
}
|
||||
}
|
||||
|
||||
if !updatedSuccessfully {
|
||||
|
|
Référencer dans un nouveau ticket