Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

Fix modpanel torrent edit (#328)

* Fix modpanel torrent edits breaking torrents

* Fix hash not being monospace anymore

* Fix modpanel torrent edit strings

* Fix modpanel torrent edit creating a new user (wtf GORM)
Cette révision appartient à :
sfan5 2017-05-11 01:46:50 +02:00 révisé par Austin
Parent 70795078df
révision 7bc395068d
6 fichiers modifiés avec 91 ajouts et 39 suppressions

Voir le fichier

@ -1,5 +1,3 @@
// hurry mod panel to get it faaaaaaaaaaaast
package router
import (
@ -9,9 +7,7 @@ import (
"net/http"
"path/filepath"
"strconv"
"time"
"github.com/ewhal/nyaa/config"
"github.com/ewhal/nyaa/db"
"github.com/ewhal/nyaa/model"
"github.com/ewhal/nyaa/service"
@ -186,6 +182,7 @@ func CommentsListPanel(w http.ResponseWriter, r *http.Request) {
}
}
func TorrentEditModPanel(w http.ResponseWriter, r *http.Request) {
currentUser := GetUser(r)
if userPermission.HasAdmin(currentUser) {
@ -197,7 +194,6 @@ func TorrentEditModPanel(w http.ResponseWriter, r *http.Request) {
uploadForm := NewUploadForm()
uploadForm.Name = torrentJson.Name
uploadForm.Category = torrentJson.Category + "_" + torrentJson.SubCategory
uploadForm.Magnet = string(torrentJson.Magnet)
uploadForm.Status = torrentJson.Status
uploadForm.Description = string(torrentJson.Description)
htv := PanelTorrentEdVbs{uploadForm, NewSearchForm(), currentUser, form.NewErrors(), form.NewInfos(), r.URL}
@ -209,11 +205,11 @@ func TorrentEditModPanel(w http.ResponseWriter, r *http.Request) {
}
}
func TorrentPostEditModPanel(w http.ResponseWriter, r *http.Request) {
currentUser := GetUser(r)
if userPermission.HasAdmin(currentUser) {
if config.UploadsDisabled {
http.Error(w, "Error uploads are disabled", http.StatusInternalServerError)
if !userPermission.HasAdmin(currentUser) {
http.Error(w, "admins only", http.StatusForbidden)
return
}
var uploadForm UploadForm
@ -222,32 +218,25 @@ func TorrentPostEditModPanel(w http.ResponseWriter, r *http.Request) {
infos := form.NewInfos()
torrent, _ := torrentService.GetTorrentById(id)
if (torrent.ID > 0) {
// validation is done in ExtractInfo()
errUp := uploadForm.ExtractInfo(r)
errUp := uploadForm.ExtractEditInfo(r)
if errUp != nil {
err["errors"] = append(err["errors"], "Failed to upload!")
err["errors"] = append(err["errors"], "Failed to update torrent!")
}
if (len(err) == 0) {
// update some (but not all!) values
torrent.Name = uploadForm.Name
torrent.Category = uploadForm.CategoryID
torrent.SubCategory = uploadForm.SubCategoryID
torrent.Status = uploadForm.Status
torrent.Description = uploadForm.Description
torrent.Uploader = nil // GORM will create a new user otherwise (wtf?!)
db.ORM.Save(&torrent)
infos["infos"] = append(infos["infos"], "Torrent details updated.")
}
if (len(err) > 0) {
//add to db and redirect depending on result
torrent.Name= uploadForm.Name
torrent.Category= uploadForm.CategoryID
torrent.SubCategory=uploadForm.SubCategoryID
torrent.Status= uploadForm.Status
torrent.Hash= uploadForm.Infohash
torrent.Date= time.Now()
torrent.Filesize= uploadForm.Filesize
torrent.Description= uploadForm.Description
db.ORM.Save(&torrent)
infos["infos"] = append(infos["infos"], "Torrent details updated!")
}
}
languages.SetTranslationFromRequest(panelTorrentEd, r, "en-us")
htv := PanelTorrentEdVbs{uploadForm, NewSearchForm(), currentUser, err, infos, r.URL}
_ = panelTorrentEd.ExecuteTemplate(w, "admin_index.html", htv)
} else {
http.Error(w, "admins only", http.StatusForbidden)
}
languages.SetTranslationFromRequest(panelTorrentEd, r, "en-us")
htv := PanelTorrentEdVbs{uploadForm, NewSearchForm(), currentUser, err, infos, r.URL}
_ = panelTorrentEd.ExecuteTemplate(w, "admin_index.html", htv)
}
func CommentDeleteModPanel(w http.ResponseWriter, r *http.Request) {

Voir le fichier

@ -193,6 +193,36 @@ func (f *UploadForm) ExtractInfo(r *http.Request) error {
return nil
}
func (f *UploadForm) ExtractEditInfo(r *http.Request) error {
f.Name = r.FormValue(UploadFormName)
f.Category = r.FormValue(UploadFormCategory)
f.Description = r.FormValue(UploadFormDescription)
f.Status, _ = strconv.Atoi(r.FormValue(UploadFormStatus))
// trim whitespace
f.Name = util.TrimWhitespaces(f.Name)
f.Description = p.Sanitize(util.TrimWhitespaces(f.Description))
catsSplit := strings.Split(f.Category, "_")
// need this to prevent out of index panics
if len(catsSplit) == 2 {
CatID, err := strconv.Atoi(catsSplit[0])
if err != nil {
return ErrInvalidTorrentCategory
}
SubCatID, err := strconv.Atoi(catsSplit[1])
if err != nil {
return ErrInvalidTorrentCategory
}
f.CategoryID = CatID
f.SubCategoryID = SubCatID
} else {
return ErrInvalidTorrentCategory
}
return nil
}
func WriteTorrentToDisk(file multipart.File, name string, fullpath *string) error {
_, seekErr := file.Seek(0, io.SeekStart)
if seekErr != nil {

Voir le fichier

@ -12,6 +12,7 @@
<label for="name">{{T "name"}}</label>
<input type="text" name="name" class="form-control" placeholder="File Name" value="{{.Name}}" required>
</div>
<!--
<div class="form-group">
<label for="torrent">{{T "torrent_file"}}</label>
<input type="file" name="torrent" id="torrent" accept=".torrent">
@ -21,7 +22,7 @@
<label for="magnet">{{T "magnet_link"}}</label>
<input type="text" name="magnet" class="form-control"
style="width:60rem" placeholder="{{T "magnet_link"}}" value="{{.Magnet}}">
</div>
</div>-->
<div class="form-group">
<label for="c">{{T "category"}}</label>
<select name="c" class="form-control input-sm">
@ -47,11 +48,11 @@
<div class="form-group">
<label for="Status">{{T "torrent_status"}}</label>
<select name="status" class="form-control input-sm">
<option value="0" {{if eq .Status 0}}selected{{end}}>{{T "mark_as_hidden"}}</option>
<option value="1" {{if eq .Status 1}}selected{{end}}>{{T "mark_as_normal"}}</option>
<option value="2" {{if eq .Status 2}}selected{{end}}>{{T "mark_as_remake"}}</option>
<option value="3" {{if eq .Status 3}}selected{{end}}>{{T "mark_as_trusted"}}</option>
<option value="4" {{if eq .Status 4}}selected{{end}}>{{T "mark_as_aplus"}}</option>
<option value="0" {{if eq .Status 0}}selected{{end}}>{{T "torrent_status_hidden"}}</option>
<option value="1" {{if eq .Status 1}}selected{{end}}>{{T "torrent_status_normal"}}</option>
<option value="2" {{if eq .Status 2}}selected{{end}}>{{T "torrent_status_remake"}}</option>
<option value="3" {{if eq .Status 3}}selected{{end}}>{{T "trusted"}}</option>
<option value="4" {{if eq .Status 4}}selected{{end}}>A+</option>
</select>
</div>
@ -63,4 +64,4 @@
<button type="submit" class="btn btn-success">{{T "save_changes"}}</button>
</form>
{{end}}
{{end}}
{{end}}

Voir le fichier

@ -41,7 +41,7 @@
</div>
<div class="col-md-4">
<h4>{{T "hash"}}</h4>
<p>{{.Hash}}</p>
<p class="torrent-hash">{{.Hash}}</p>
<hr>
<h4>{{T "date"}}</h4>
<p class="date-full">{{.Date}}</p>

Voir le fichier

@ -574,5 +574,21 @@
{
"id": "email_changed",
"translation": "E-Mail erfolgreich geändert! Bitte bestätige die Änderung mit dem Bestätigungs-Link (an %s gesendet)"
},
{
"id": "torrent_status",
"translation": "Torrent-Status"
},
{
"id": "torrent_status_hidden",
"translation": "Versteckt"
},
{
"id": "torrent_status_normal",
"translation": "Normal"
},
{
"id": "torrent_status_remake",
"translation": "Remake"
}
]

Voir le fichier

@ -582,5 +582,21 @@
{
"id": "email_changed",
"translation": "Email changed successfully! You will have, however, to confirm it by clicking to the link sent to: %s"
},
{
"id": "torrent_status",
"translation": "Torrent status"
},
{
"id": "torrent_status_hidden",
"translation": "Hidden"
},
{
"id": "torrent_status_normal",
"translation": "Normal"
},
{
"id": "torrent_status_remake",
"translation": "Remake"
}
]