diff --git a/router/modpanel.go b/router/modpanel.go index 525e0123..8bce22ed 100644 --- a/router/modpanel.go +++ b/router/modpanel.go @@ -9,19 +9,20 @@ 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" "github.com/ewhal/nyaa/service/comment" "github.com/ewhal/nyaa/service/report" "github.com/ewhal/nyaa/service/torrent" - "github.com/ewhal/nyaa/service/torrent/form" "github.com/ewhal/nyaa/service/user" form "github.com/ewhal/nyaa/service/user/form" "github.com/ewhal/nyaa/service/user/permission" "github.com/ewhal/nyaa/util/languages" "github.com/ewhal/nyaa/util/log" - "github.com/ewhal/nyaa/util/modelHelper" "github.com/ewhal/nyaa/util/search" "github.com/gorilla/mux" ) @@ -191,9 +192,18 @@ func TorrentEditModPanel(w http.ResponseWriter, r *http.Request) { id := r.URL.Query().Get("id") torrent, _ := torrentService.GetTorrentById(id) languages.SetTranslationFromRequest(panelTorrentEd, r, "en-us") - htv := PanelTorrentEdVbs{torrent, NewSearchForm(), currentUser} + + torrentJson := torrent.ToJSON() + 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()} err := panelTorrentEd.ExecuteTemplate(w, "admin_index.html", htv) log.CheckError(err) + } else { http.Error(w, "admins only", http.StatusForbidden) } @@ -202,31 +212,42 @@ func TorrentEditModPanel(w http.ResponseWriter, r *http.Request) { func TorrentPostEditModPanel(w http.ResponseWriter, r *http.Request) { currentUser := GetUser(r) if userPermission.HasAdmin(currentUser) { - b := torrentform.PanelPost{} - err := form.NewErrors() - infos := form.NewInfos() - modelHelper.BindValueForm(&b, r) - err = modelHelper.ValidateForm(&b, err) - id := r.URL.Query().Get("id") - torrent, _ := torrentService.GetTorrentById(id) - if torrent.ID > 0 { - modelHelper.AssignValue(&torrent, &b) - if len(err) == 0 { - _, errorT := torrentService.UpdateTorrent(torrent) - if errorT != nil { - err["errors"] = append(err["errors"], errorT.Error()) - } - if len(err) == 0 { - infos["infos"] = append(infos["infos"], "torrent_updated") - } - } + if config.UploadsDisabled { + http.Error(w, "Error uploads are disabled", http.StatusInternalServerError) + return + } + var uploadForm UploadForm + id := r.URL.Query().Get("id") + err := form.NewErrors() + infos := form.NewInfos() + torrent, _ := torrentService.GetTorrentById(id) + if (torrent.ID > 0) { + // validation is done in ExtractInfo() + errUp := uploadForm.ExtractInfo(r) + if errUp != nil { + err["errors"] = append(err["errors"], "Failed to upload!") } + 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{torrent, NewSearchForm(), currentUser} + htv := PanelTorrentEdVbs{uploadForm, NewSearchForm(), currentUser, err, infos} _ = panelTorrentEd.ExecuteTemplate(w, "admin_index.html", htv) } else { http.Error(w, "admins only", http.StatusForbidden) } + } func CommentDeleteModPanel(w http.ResponseWriter, r *http.Request) { diff --git a/router/templateVariables.go b/router/templateVariables.go index 20d852f5..c3cc2010 100644 --- a/router/templateVariables.go +++ b/router/templateVariables.go @@ -148,9 +148,11 @@ type PanelCommentListVbs struct { URL *url.URL // For parsing Url in templates } type PanelTorrentEdVbs struct { - Torrent model.Torrent + Upload UploadForm Search SearchForm User *model.User + FormInfos map[string][]string + FormErrors map[string][]string } type PanelTorrentReportListVbs struct { diff --git a/router/upload.go b/router/upload.go index 9557ab8e..7d075da0 100644 --- a/router/upload.go +++ b/router/upload.go @@ -30,6 +30,7 @@ type UploadForm struct { Category string Remake bool Description string + Status int captcha.Captcha Infohash string @@ -48,6 +49,7 @@ const UploadFormMagnet = "magnet" const UploadFormCategory = "c" const UploadFormRemake = "remake" const UploadFormDescription = "desc" +const UploadFormStatus = "status" // error indicating that you can't send both a magnet link and torrent var ErrTorrentPlusMagnet = errors.New("upload either a torrent file or magnet link, not both") @@ -77,6 +79,7 @@ func (f *UploadForm) ExtractInfo(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)) f.Magnet = r.FormValue(UploadFormMagnet) f.Remake = r.FormValue(UploadFormRemake) == "on" f.Captcha = captcha.Extract(r) diff --git a/templates/admin/paneltorrentedit.html b/templates/admin/paneltorrentedit.html index 478c5e9d..c85015e6 100644 --- a/templates/admin/paneltorrentedit.html +++ b/templates/admin/paneltorrentedit.html @@ -1,5 +1,65 @@ {{define "content"}} + {{with .Upload}} +
+{{end}} {{end}} \ No newline at end of file diff --git a/templates/admin/userlist.html b/templates/admin/userlist.html index c3a29d9d..46297a44 100644 --- a/templates/admin/userlist.html +++ b/templates/admin/userlist.html @@ -5,7 +5,7 @@