-
-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathtorrents.go
220 lines (202 loc) · 5.05 KB
/
torrents.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package model
import (
"net/url"
"sort"
"strconv"
"strings"
"time"
"github.com/bitmagnet-io/bitmagnet/internal/lexer"
"github.com/facette/natsort"
"gorm.io/gorm"
)
func (t *Torrent) AfterFind(tx *gorm.DB) error {
if t.Files != nil {
sort.Slice(t.Files, func(i, j int) bool {
return t.Files[i].Path < t.Files[j].Path
})
}
if t.Tags != nil {
sort.Slice(t.Tags, func(i, j int) bool {
return natsort.Compare(t.Tags[i].Name, t.Tags[j].Name)
})
}
return nil
}
// Seeders returns the highest number of seeders from all sources
// todo: Add up bloom filters
func (t Torrent) Seeders() NullUint {
seeders := NullUint{}
for _, source := range t.Sources {
if source.Seeders.Valid {
seeders.Valid = true
if source.Seeders.Uint > seeders.Uint {
seeders.Uint = source.Seeders.Uint
}
}
}
return seeders
}
// Leechers returns the highest number of leechers from all sources
func (t Torrent) Leechers() NullUint {
leechers := NullUint{}
for _, source := range t.Sources {
if source.Leechers.Valid {
leechers.Valid = true
if source.Leechers.Uint > leechers.Uint {
leechers.Uint = source.Leechers.Uint
}
}
}
return leechers
}
var cutoff = time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
func (t Torrent) PublishedAt() time.Time {
publishedAt := t.CreatedAt
for _, source := range t.Sources {
dt := source.CreatedAt
if source.PublishedAt.Valid && source.PublishedAt.Time.After(cutoff) {
dt = source.PublishedAt.Time
}
if dt.Before(publishedAt) {
publishedAt = dt
}
}
return publishedAt
}
func (t Torrent) MagnetUri() string {
return "magnet:?xt=urn:btih:" + t.InfoHash.String() +
"&dn=" + url.QueryEscape(t.Name) +
"&xl=" + strconv.FormatUint(uint64(t.Size), 10)
}
func (t Torrent) PermaLink() string {
return "/webui/torrents/permalink/" + t.InfoHash.String()
}
// HasFilesInfo returns true if we know about the files in this torrent.
func (t Torrent) HasFilesInfo() bool {
return t.FilesStatus == FilesStatusSingle || t.FilesStatus == FilesStatusMulti || len(t.Files) > 0
}
func (t Torrent) SingleFile() bool {
return t.FilesStatus == FilesStatusSingle
}
func (t Torrent) BaseName() string {
baseName := t.Name
if t.Extension.Valid {
baseName = baseName[:len(baseName)-len(t.Extension.String)-1]
}
return baseName
}
func (t Torrent) FileExtensions() []string {
switch t.FilesStatus {
case FilesStatusSingle:
exts := make([]string, 0, 1)
ext := FileExtensionFromPath(t.Name)
if ext.Valid {
exts = append(exts, ext.String)
}
return exts
default:
exts := make([]string, 0, len(t.Files))
extMap := make(map[string]struct{})
for _, file := range t.Files {
ext := FileExtensionFromPath(file.Path)
if ext.Valid {
if _, ok := extMap[ext.String]; !ok {
extMap[ext.String] = struct{}{}
exts = append(exts, ext.String)
}
}
}
return exts
}
}
func (t Torrent) FileType() NullFileType {
if t.Extension.Valid {
return FileTypeFromExtension(t.Extension.String)
}
return NullFileType{}
}
func (t Torrent) FileTypes() []FileType {
exts := t.FileExtensions()
typesMap := make(map[FileType]struct{})
types := make([]FileType, 0, len(exts))
for _, ext := range exts {
if ft := FileTypeFromExtension(ext); ft.Valid {
if _, ok := typesMap[ft.FileType]; !ok {
typesMap[ft.FileType] = struct{}{}
types = append(types, ft.FileType)
}
}
}
return types
}
func (t Torrent) HasFileType(fts ...FileType) NullBool {
for _, thisFt := range t.FileTypes() {
for _, ft := range fts {
if ft == thisFt {
return NewNullBool(true)
}
}
}
return NewNullBool(false)
}
func (t Torrent) TagNames() []string {
tagNames := make([]string, 0, len(t.Tags))
for _, tag := range t.Tags {
tagNames = append(tagNames, tag.Name)
}
return tagNames
}
// fileSearchStrings returns a list of strings extracted from file paths, for inclusion in the text search vector.
// To reduce duplication, common prefixes and suffixes are deduplicated.
func (t Torrent) fileSearchStrings() []string {
firstPass := make([]string, 0, len(t.Files))
var prevPath string
outer:
for _, f := range t.Files {
i := 0
for {
if i >= len(f.Path) {
continue outer
}
if i >= len(prevPath) || prevPath[i] != f.Path[i] {
break
}
i++
}
for {
if i == 0 || !lexer.IsWordChar(rune(f.Path[i])) {
break
}
i--
}
firstPass = append(firstPass, f.Path[i:])
prevPath = f.Path
}
searchStrings := make([]string, 0, len(firstPass))
for i := range firstPass {
longestSuffixLength := 0
for j := 0; j < i; j++ {
l := 0
for {
if l >= len(firstPass[i]) || l >= len(firstPass[j]) || firstPass[i][len(firstPass[i])-l-1] != firstPass[j][len(firstPass[j])-l-1] {
break
}
l++
}
if l > longestSuffixLength {
longestSuffixLength = l
}
}
for {
if longestSuffixLength == 0 || !lexer.IsWordChar(rune(firstPass[i][len(firstPass[i])-longestSuffixLength])) {
break
}
longestSuffixLength--
}
str := strings.TrimSpace(firstPass[i][:len(firstPass[i])-longestSuffixLength])
if str != "" {
searchStrings = append(searchStrings, str)
}
}
return searchStrings
}