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/utils/filelist/filelist.go

70 lignes
1,7 Kio
Go
Brut Vue normale Historique

package filelist
import (
"strings"
2017-06-29 13:15:23 +02:00
"github.com/NyaaPantsu/nyaa/models"
"github.com/bradfitz/slice"
)
// FileListFolder struct
type FileListFolder struct {
Folders []*FileListFolder
Files []models.File
FolderName string
}
// FileListToFolder convert a filelist to filelistfolder
func FileListToFolder(fileList []models.File, folderName string) (out *FileListFolder) {
out = &FileListFolder{
Folders: make([]*FileListFolder, 0),
Files: make([]models.File, 0),
FolderName: folderName,
}
pathsToFolders := make(map[string][]models.File)
for _, file := range fileList {
pathArray := file.Path()
if len(pathArray) > 1 {
pathStrippedFile := models.File{
ID: file.ID,
TorrentID: file.TorrentID,
BencodedPath: "",
Filesize: file.Filesize,
}
pathStrippedFile.SetPath(pathArray[1:])
pathsToFolders[pathArray[0]] = append(pathsToFolders[pathArray[0]], pathStrippedFile)
} else {
out.Files = append(out.Files, file)
}
}
for folderName, folderFiles := range pathsToFolders {
out.Folders = append(out.Folders, FileListToFolder(folderFiles, folderName))
}
// Do some sorting
slice.Sort(out.Folders, func(i, j int) bool {
return strings.ToLower(out.Folders[i].FolderName) < strings.ToLower(out.Folders[j].FolderName)
})
slice.Sort(out.Files, func(i, j int) bool {
return strings.ToLower(out.Files[i].Filename()) < strings.ToLower(out.Files[j].Filename())
})
return
}
// TotalSize : gives the total size of filelistfolder
func (f *FileListFolder) TotalSize() (out int64) {
out = 0
for _, folder := range f.Folders {
out += folder.TotalSize()
}
for _, file := range f.Files {
out += file.Filesize
}
return
}