Make pre-filling work correctly
Cette révision appartient à :
Parent
1dd95f0388
révision
39cec46dce
4 fichiers modifiés avec 77 ajouts et 35 suppressions
31
public/js/uploadPage.js
Fichier normal
31
public/js/uploadPage.js
Fichier normal
|
@ -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");
|
||||
}
|
||||
})();
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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)}
|
||||
|
|
|
@ -6,13 +6,13 @@
|
|||
<form enctype="multipart/form-data" role="upload" method="POST">
|
||||
|
||||
<div class="form-group">
|
||||
<label for="torrent">Torrent file upload</label>
|
||||
<input type="file" name="torrent" id="torrent" accept=".torrent">
|
||||
<p class="help-block">Upload a torrent file to pre-fill information</p>
|
||||
<label for="name">Name</label>
|
||||
<input type="text" name="name" class="form-control" placeholder="File Name" value="{{.Name}}" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="name">FileName</label>
|
||||
<input type="text" name="name" class="form-control"placeholder="File Name" value="{{.Name}}" required>
|
||||
<label for="torrent">Torrent file</label>
|
||||
<input type="file" name="torrent" id="torrent" accept=".torrent">
|
||||
<p class="help-block">Uploading a torrent file allows pre-filling some fields, this is recommended.</p>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="Magnet">Magnet Link</label>
|
||||
|
@ -45,6 +45,7 @@
|
|||
|
||||
<div class="form-group">
|
||||
<label for="desc">Torrent Description</label>
|
||||
<p class="help-block">A limited set of HTML is allowed in the description, make sure to use <span style="font-family:monospace"><br/></span>.</p>
|
||||
<textarea name="desc" class="form-control" rows="10">{{.Description}}</textarea>
|
||||
</div>
|
||||
|
||||
|
@ -56,4 +57,7 @@
|
|||
</form>
|
||||
{{end}}
|
||||
</div>
|
||||
{{end}}
|
||||
{{end}}
|
||||
{{define "js_footer"}}
|
||||
<script type="text/javascript" charset="utf-8" src="{{.URL.Parse "/js/uploadPage.js"}}"></script>
|
||||
{{end}}
|
||||
|
|
Référencer dans un nouveau ticket