From 767c91bacd500ddd7de094877cd41f9faf5a59f6 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Sun, 7 May 2017 13:38:46 +0200 Subject: [PATCH] Make pre-filling work correctly --- public/js/uploadPage.js | 31 +++++++++++++++++++++ router/upload.go | 60 +++++++++++++++++++++++------------------ router/uploadHandler.go | 5 ++-- templates/upload.html | 16 ++++++----- 4 files changed, 77 insertions(+), 35 deletions(-) create mode 100644 public/js/uploadPage.js diff --git a/public/js/uploadPage.js b/public/js/uploadPage.js new file mode 100644 index 00000000..cc781a4f --- /dev/null +++ b/public/js/uploadPage.js @@ -0,0 +1,31 @@ +(function() { + var torrent = $("input[name=torrent]"), + magnet = $("input[name=magnet]"), + name = $("input[name=name]"); + + torrent.on("change", function() { + if (torrent.val() == "") { + enableField(magnet); + name.attr("required", ""); + } else { + disableField(magnet); + // .torrent file will allow autofilling name + name.removeAttr("required", ""); + } + }); + magnet.on("change", function() { + if (magnet.val() == "") + enableField(torrent); + else + disableField(torrent); + }); + + function enableField(e) { + e.attr("required", "") + .removeAttr("disabled"); + } + function disableField(e) { + e.attr("disabled", "") + .removeAttr("required"); + } +})(); diff --git a/router/upload.go b/router/upload.go index 44a2a80e..e29c3dc8 100644 --- a/router/upload.go +++ b/router/upload.go @@ -45,6 +45,10 @@ const UploadFormCategory = "c" // form value for description const UploadFormDescription = "desc" + +// 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") + // error indicating a torrent is private var ErrPrivateTorrent = errors.New("torrent is private") @@ -79,14 +83,6 @@ func (f *UploadForm) ExtractInfo(r *http.Request) error { f.Description = p.Sanitize(util.TrimWhitespaces(f.Description)) f.Magnet = util.TrimWhitespaces(f.Magnet) - if len(f.Name) == 0 { - return ErrInvalidTorrentName - } - - //if len(f.Description) == 0 { - // return ErrInvalidTorrentDescription - //} - catsSplit := strings.Split(f.Category, "_") // need this to prevent out of index panics if len(catsSplit) == 2 { @@ -105,13 +101,10 @@ func (f *UploadForm) ExtractInfo(r *http.Request) error { return ErrInvalidTorrentCategory } - if len(f.Magnet) == 0 { - // try parsing torrent file if provided if no magnet is specified - tfile, _, err := r.FormFile(UploadFormTorrent) - if err != nil { - return err - } + // first: parse torrent file (if any) to fill missing information + tfile, _, err := r.FormFile(UploadFormTorrent) + if err == nil { var torrent metainfo.TorrentFile // decode torrent err = bencode.NewDecoder(tfile).Decode(&torrent) @@ -119,23 +112,29 @@ func (f *UploadForm) ExtractInfo(r *http.Request) error { return metainfo.ErrInvalidTorrentFile } - // check if torrent is private + // check a few things if torrent.IsPrivate() { return ErrPrivateTorrent } - - // check trackers trackers := torrent.GetAllAnnounceURLS() if !CheckTrackers(trackers) { return ErrTrackerProblem } - // generate magnet + // Name + if len(f.Name) == 0 { + f.Name = torrent.TorrentName() + } + + // Magnet link: if a file is provided it should be empty + if len(f.Magnet) != 0 { + return ErrTorrentPlusMagnet + } binInfohash := torrent.Infohash() - f.Infohash = hex.EncodeToString(binInfohash[:]) + f.Infohash = strings.ToUpper(hex.EncodeToString(binInfohash[:])) f.Magnet = util.InfoHashToMagnet(f.Infohash, f.Name) - f.Infohash = strings.ToUpper(f.Infohash) } else { + // No torrent file provided magnetUrl, parseErr := url.Parse(f.Magnet) if parseErr != nil { return metainfo.ErrInvalidTorrentFile @@ -143,15 +142,24 @@ func (f *UploadForm) ExtractInfo(r *http.Request) error { exactTopic := magnetUrl.Query().Get("xt") if !strings.HasPrefix(exactTopic, "urn:btih:") { return metainfo.ErrInvalidTorrentFile - } else { - f.Infohash = strings.ToUpper(strings.TrimPrefix(exactTopic, "urn:btih:")) - matched, err := regexp.MatchString("^[0-9A-F]{40}$", f.Infohash) - if err != nil || !matched { - return metainfo.ErrInvalidTorrentFile - } + } + f.Infohash = strings.ToUpper(strings.TrimPrefix(exactTopic, "urn:btih:")) + matched, err := regexp.MatchString("^[0-9A-F]{40}$", f.Infohash) + if err != nil || !matched { + return metainfo.ErrInvalidTorrentFile } } + + // then actually check that we have everything we need + if len(f.Name) == 0 { + return ErrInvalidTorrentName + } + + //if len(f.Description) == 0 { + // return ErrInvalidTorrentDescription + //} + return nil } diff --git a/router/uploadHandler.go b/router/uploadHandler.go index 536c57a6..00008e31 100644 --- a/router/uploadHandler.go +++ b/router/uploadHandler.go @@ -24,6 +24,7 @@ func UploadHandler(w http.ResponseWriter, r *http.Request) { var uploadForm UploadForm if r.Method == "POST" { defer r.Body.Close() + // validation is done in ExtractInfo() err = uploadForm.ExtractInfo(r) if err == nil { if !captcha.Authenticate(uploadForm.Captcha) { @@ -32,7 +33,6 @@ func UploadHandler(w http.ResponseWriter, r *http.Request) { return } - //validate name + hash //add to db and redirect depending on result torrent := model.Torrents{ Name: uploadForm.Name, @@ -43,7 +43,7 @@ func UploadHandler(w http.ResponseWriter, r *http.Request) { Date: time.Now().Unix(), Description: uploadForm.Description, Comments: []byte{}} - fmt.Printf("%+v\n", torrent) + //fmt.Printf("%+v\n", torrent) db.ORM.Create(&torrent) fmt.Printf("%+v\n", torrent) url, err := Router.Get("view_torrent").URL("id", strconv.Itoa(torrent.Id)) @@ -51,7 +51,6 @@ func UploadHandler(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, url.String(), 302) } } - fmt.Printf("%+v\n", uploadForm) } else if r.Method == "GET" { uploadForm.CaptchaID = captcha.GetID(r.RemoteAddr) htv := UploadTemplateVariables{uploadForm, NewSearchForm(), Navigation{}, r.URL, mux.CurrentRoute(r)} diff --git a/templates/upload.html b/templates/upload.html index 0b26c15d..cbba56ac 100644 --- a/templates/upload.html +++ b/templates/upload.html @@ -6,13 +6,13 @@
- - -

Upload a torrent file to pre-fill information

+ +
- - + + +

Uploading a torrent file allows pre-filling some fields, this is recommended.

@@ -45,6 +45,7 @@
+

A limited set of HTML is allowed in the description, make sure to use <br/>.

@@ -56,4 +57,7 @@ {{end}}
- {{end}} +{{end}} +{{define "js_footer"}} + +{{end}}