-
Notifications
You must be signed in to change notification settings - Fork 150
/
upload.go
219 lines (190 loc) · 5.49 KB
/
upload.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
package upload
import (
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"os"
"reflect"
"strings"
"github.com/NyaaPantsu/nyaa/config"
"github.com/NyaaPantsu/nyaa/models"
"github.com/NyaaPantsu/nyaa/utils/sanitize"
"github.com/NyaaPantsu/nyaa/utils/search/structs"
"github.com/NyaaPantsu/nyaa/utils/validator/torrent"
"github.com/gin-gonic/gin"
)
// form names
const uploadFormTorrent = "torrent"
type torrentsQuery struct {
Category int `json:"category"`
SubCategory int `json:"sub_category"`
Status int `json:"status"`
Uploader int `json:"uploader"`
Downloads int `json:"downloads"`
}
// TorrentsRequest struct
type TorrentsRequest struct {
Query torrentsQuery `json:"search"`
Page int `json:"page"`
MaxPerPage int `json:"limit"`
}
// APIResultJSON for torrents in json for api
type APIResultJSON struct {
Torrents []models.TorrentJSON `json:"torrents"`
QueryRecordCount int `json:"queryRecordCount"`
TotalRecordCount int `json:"totalRecordCount"`
}
// ToParams : Convert a torrentsrequest to searchparams
func (r *TorrentsRequest) ToParams() structs.WhereParams {
res := structs.WhereParams{}
conditions := ""
v := reflect.ValueOf(r.Query)
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
if field.Interface() != reflect.Zero(field.Type()).Interface() {
if i != 0 {
conditions += " AND "
}
conditions += v.Type().Field(i).Tag.Get("json") + " = ?"
res.Params = append(res.Params, field.Interface())
}
}
res.Conditions = conditions
return res
}
// ExtractEditInfo : takes an http request and computes all fields for this form
func ExtractEditInfo(c *gin.Context, r *torrentValidator.TorrentRequest) error {
err := ExtractBasicValue(c, r)
if err != nil {
return err
}
err = r.ValidateName()
if err != nil {
return err
}
err = r.ExtractCategory()
if err != nil {
return err
}
err = r.ExtractLanguage()
return err
}
// ExtractBasicValue : takes an http request and computes all basic fields for this form
func ExtractBasicValue(c *gin.Context, r *torrentValidator.TorrentRequest) error {
c.Bind(r)
// trim whitespace
r.Name = strings.TrimSpace(r.Name)
r.Description = sanitize.Sanitize(strings.TrimSpace(r.Description), "default")
r.WebsiteLink = strings.TrimSpace(r.WebsiteLink)
r.Magnet = strings.TrimSpace(r.Magnet)
if len(r.Languages) == 0 { // Shouldn't have to do that since c.Bind actually bind arrays, but better off adding it in case gin doesn't do his work
r.Languages = c.PostFormArray("languages")
}
// then actually check that we have everything we need
err := r.ValidateDescription()
if err != nil {
return err
}
err = r.ValidateWebsiteLink()
return err
}
// ExtractInfo : takes an http request and computes all fields for this form
func ExtractInfo(c *gin.Context, r *torrentValidator.TorrentRequest) error {
err := ExtractBasicValue(c, r)
if err != nil {
return err
}
err = r.ExtractCategory()
if err != nil {
return err
}
err = r.ExtractLanguage()
if err != nil {
return err
}
tfile, err := r.ValidateMultipartUpload(c, uploadFormTorrent)
if err != nil {
return err
}
// We check name only here, reason: we can try to retrieve them from the torrent file
err = r.ValidateName()
if err != nil {
return err
}
// after data has been checked & extracted, write it to disk
if len(config.Get().Torrents.FileStorage) > 0 && r.Filesize > 0 {
err := writeTorrentToDisk(tfile, r.Infohash+".torrent", &r.Filepath)
if err != nil {
return err
}
} else {
r.Filepath = ""
}
return nil
}
// UpdateTorrent : Update torrent model
//rewrite with reflect ?
func UpdateTorrent(r *torrentValidator.UpdateRequest, t *models.Torrent, currentUser *models.User) *models.Torrent {
if r.Update.Name != "" {
t.Name = r.Update.Name
}
if r.Update.Infohash != "" {
t.Hash = r.Update.Infohash
}
if r.Update.CategoryID != 0 {
t.Category = r.Update.CategoryID
}
if r.Update.SubCategoryID != 0 {
t.SubCategory = r.Update.SubCategoryID
}
if r.Update.Description != "" {
t.Description = r.Update.Description
}
if r.Update.WebsiteLink != "" {
t.WebsiteLink = r.Update.WebsiteLink
}
t.Languages = r.Update.Languages
status := models.TorrentStatusNormal
if r.Update.Remake { // overrides trusted
status = models.TorrentStatusRemake
} else if currentUser.IsTrusted() {
status = models.TorrentStatusTrusted
}
t.Status = status
t.Hidden = r.Update.Hidden
return t
}
// UpdateUnscopeTorrent : Update a torrent model without scoping
func UpdateUnscopeTorrent(r *torrentValidator.UpdateRequest, t *models.Torrent, currentUser *models.User) *models.Torrent {
t = UpdateTorrent(r, t, currentUser)
t.Status = r.Update.Status
return t
}
func writeTorrentToDisk(file multipart.File, name string, fullpath *string) error {
_, seekErr := file.Seek(0, io.SeekStart)
if seekErr != nil {
return seekErr
}
b, err := ioutil.ReadAll(file)
if err != nil {
return err
}
*fullpath = fmt.Sprintf("%s%c%s", config.Get().Torrents.FileStorage, os.PathSeparator, name)
return ioutil.WriteFile(*fullpath, b, 0644)
}
// NewTorrentRequest : creates a new torrent request struc with some default value
func NewTorrentRequest(params ...string) *torrentValidator.TorrentRequest {
torrentRequest := &torrentValidator.TorrentRequest{}
if len(params) > 1 {
torrentRequest.Category = params[0]
} else {
torrentRequest.Category = "3_12"
}
if len(params) > 2 {
torrentRequest.Description = params[1]
} else {
torrentRequest.Description = "Description"
}
return torrentRequest
}