2015-02-14 00:17:40 +01:00
|
|
|
window.onload = function () {
|
|
|
|
$('.dropdown li > a').click(function(e){
|
|
|
|
setDropdownButtonText(this.innerHTML, this.parentElement.parentElement.parentElement) //set the display-text to the selected value
|
|
|
|
fillDropdownByValue(this.innerHTML, $(".dropdown")["1"])
|
2015-02-21 03:01:47 +01:00
|
|
|
this.parentElement.parentElement.parentElement.classList.remove("open");
|
|
|
|
return false;
|
2015-02-14 00:17:40 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
customizeUploadButton()
|
|
|
|
}
|
|
|
|
|
|
|
|
function setDropdownButtonText(text, dropdownButton) {
|
|
|
|
var dropdownTextSpan = dropdownButton.getElementsByClassName("text")[0]
|
|
|
|
dropdownTextSpan.innerHTML = text
|
|
|
|
}
|
|
|
|
|
|
|
|
function sortCategories(categories) {
|
|
|
|
var sortedCategories = []
|
|
|
|
for(var key in categories) {
|
2015-02-15 03:48:39 +01:00
|
|
|
sortedCategories.push(categories[key])
|
2015-02-14 00:17:40 +01:00
|
|
|
}
|
|
|
|
sortedCategories.sort()
|
|
|
|
return sortedCategories
|
|
|
|
}
|
|
|
|
|
2015-02-21 03:01:47 +01:00
|
|
|
function fillDetectedFilelist(file) {
|
|
|
|
var reader = new FileReader();
|
|
|
|
fileList = document.querySelectorAll(".detectedFiles ul")[0]
|
|
|
|
fileList.innerHTML = ""
|
|
|
|
reader.onload = function() {
|
2015-02-21 04:29:47 +01:00
|
|
|
autodetectSuccess = false
|
2015-02-21 03:01:47 +01:00
|
|
|
b = new bencode()
|
2015-02-21 04:29:47 +01:00
|
|
|
try {
|
|
|
|
torrentObject = b.decode(reader.result)
|
|
|
|
} catch(err) {
|
|
|
|
|
2015-02-21 03:01:47 +01:00
|
|
|
}
|
|
|
|
|
2015-02-21 04:29:47 +01:00
|
|
|
if(torrentObject.info.name) {
|
|
|
|
document.querySelectorAll("input.name")[0].value = torrentObject.info.name
|
|
|
|
}
|
2015-02-21 03:01:47 +01:00
|
|
|
|
|
|
|
sizeGroup = document.querySelectorAll(".sizeGroup")[0]
|
2015-02-21 04:29:47 +01:00
|
|
|
if(torrentObject.info.pieces.length && torrentObject.info["piece length"]) {
|
2015-02-21 03:01:47 +01:00
|
|
|
sizeGroup.style.display = ""
|
2015-02-21 04:29:47 +01:00
|
|
|
size = (torrentObject.info.pieces.length / 20) * torrentObject.info["piece length"]
|
|
|
|
size = getNextUnit(size)
|
|
|
|
document.querySelectorAll(".detectedSize")[0].innerHTML = ((Math.round(size[0] * 100)) / 100) + " " + size[1]
|
|
|
|
autodetectSuccess = true
|
2015-02-21 03:01:47 +01:00
|
|
|
} else {
|
|
|
|
sizeGroup.style.display = "none"
|
|
|
|
}
|
|
|
|
|
|
|
|
trackerGroup = document.querySelectorAll(".trackerGroup")[0]
|
2015-02-21 04:29:47 +01:00
|
|
|
if(torrentObject.announce) {
|
2015-02-21 03:01:47 +01:00
|
|
|
trackerGroup.style.display = ""
|
|
|
|
document.querySelectorAll(".detectedTracker")[0].innerHTML = torrentObject.announce
|
2015-02-21 04:29:47 +01:00
|
|
|
autodetectSuccess = true
|
2015-02-21 03:01:47 +01:00
|
|
|
} else {
|
|
|
|
trackerGroup.style.display = "none"
|
|
|
|
}
|
2015-02-21 04:29:47 +01:00
|
|
|
|
|
|
|
filesGroup = document.querySelectorAll(".filesGroup")[0]
|
|
|
|
if(torrentObject.info.files.length > 0) {
|
|
|
|
autodetectSuccess = true
|
|
|
|
for(var fileIndex = 0; fileIndex < torrentObject.info.files.length; fileIndex++){
|
|
|
|
newListElement = document.createElement("li")
|
|
|
|
newListElement.innerHTML = torrentObject.info.files[fileIndex].path[0]
|
|
|
|
fileList.appendChild(newListElement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var detectInfosGroup = document.querySelectorAll(".detectedInfosGroup")[0]
|
|
|
|
if(autodetectSuccess) {
|
|
|
|
detectInfosGroup.style.display = "block"
|
|
|
|
} else {
|
|
|
|
detectInfosGroup.style.display = "none"
|
|
|
|
}
|
2015-02-21 03:01:47 +01:00
|
|
|
}
|
|
|
|
reader.readAsArrayBuffer(file)
|
|
|
|
}
|
|
|
|
|
2015-02-21 04:29:47 +01:00
|
|
|
function getNextUnit(bSize) {
|
|
|
|
units = ["B", "kB", "MB", "GB", "TB", "PB", "To damn high"]
|
|
|
|
iters = 0
|
|
|
|
size = bSize
|
|
|
|
do {
|
|
|
|
size /= 1024
|
|
|
|
iters++
|
|
|
|
} while(size >= 1)
|
|
|
|
size *= 1024
|
|
|
|
if(iters > units.length) { iters = units.length }
|
|
|
|
return [size, units[iters - 1]]
|
|
|
|
}
|
|
|
|
|
2015-02-15 03:48:39 +01:00
|
|
|
// Fill a defined dropdown with values.
|
|
|
|
// These values will be generated out of the categories.json
|
2015-02-14 00:17:40 +01:00
|
|
|
function fillDropdownByValue(value, dropdownToFill) {
|
|
|
|
dropdownToFill.getElementsByTagName("button")[0].disabled = false
|
|
|
|
dropdownToFill.getElementsByClassName("text")[0].innerHTML = "Subcategorie"
|
|
|
|
var dropdownToFillUl = dropdownToFill.getElementsByClassName("dropdown-menu")[0]
|
|
|
|
dropdownToFillUl.innerHTML = ""
|
2015-02-15 03:48:39 +01:00
|
|
|
valueDescriptor = getDescriptorByLocalString("english", value)
|
|
|
|
subcategories = global_categories[valueDescriptor]
|
|
|
|
subcategories = sortCategories(subcategories)
|
|
|
|
for(subcategoryIndex in subcategories) {
|
|
|
|
subcategoryLocalString = getLocalString("english", subcategories[subcategoryIndex])
|
|
|
|
var newEntry = document.createElement("li")
|
|
|
|
var newEntryLink = document.createElement("a")
|
|
|
|
newEntry.setAttribute("role", "presentation")
|
|
|
|
newEntryLink.setAttribute("role", "menuitem")
|
|
|
|
newEntryLink.tabIndex = -1
|
|
|
|
newEntryLink.href = "#"
|
|
|
|
newEntryLink.innerHTML = subcategoryLocalString
|
2015-02-21 03:01:47 +01:00
|
|
|
newEntryLink.onclick = function(e){
|
|
|
|
setDropdownButtonText(this.innerHTML, this.parentElement.parentElement.parentElement)
|
|
|
|
this.parentElement.parentElement.parentElement.classList.remove("open");
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-15 03:48:39 +01:00
|
|
|
newEntry.appendChild(newEntryLink)
|
|
|
|
dropdownToFillUl.appendChild(newEntry)
|
2015-02-14 00:17:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-15 03:48:39 +01:00
|
|
|
// Hides the default browser-upload-form and replaces it by an button
|
2015-02-14 00:17:40 +01:00
|
|
|
function customizeUploadButton() {
|
2015-02-15 05:43:17 +01:00
|
|
|
$("input.file").before('<button id="button-file" type="button" class="btn btn-default"><span class="text">Upload...</span><span class="glyphicon glyphicon-open-file" aria-hidden="true"></span></button>');
|
|
|
|
$("input.file").hide();
|
2015-02-14 00:17:40 +01:00
|
|
|
$('body').on('click', '#button-file', function() {
|
2015-02-15 05:43:17 +01:00
|
|
|
$("input.file").trigger('click');
|
2015-02-14 00:17:40 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-02-15 03:48:39 +01:00
|
|
|
// This sets the Uploadbutton to the filename of the uploaded file
|
2015-02-14 00:17:40 +01:00
|
|
|
function setButtonToFilename(event) {
|
2015-02-15 03:48:39 +01:00
|
|
|
$("input[name='torrentFile']").each(function() {
|
2015-02-14 00:17:40 +01:00
|
|
|
var fileName = $(this).val().split('/').pop().split('\\').pop();
|
|
|
|
targetInput = event["target"]
|
|
|
|
button = targetInput.previousSibling.getElementsByClassName("text")[0]
|
2015-02-15 03:48:39 +01:00
|
|
|
button.innerHTML = chunk(fileName, 40)
|
2015-02-21 03:01:47 +01:00
|
|
|
fillDetectedFilelist(this.files[0])
|
2015-02-14 00:17:40 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-02-15 03:48:39 +01:00
|
|
|
// Sets the text of a given dropdown-button to a given value
|
2015-02-14 00:17:40 +01:00
|
|
|
function setDropdownButtonText(text, dropdownButton) {
|
|
|
|
var dropdownTextSpan = dropdownButton.getElementsByClassName("text")[0]
|
2015-02-15 04:51:27 +01:00
|
|
|
dropdownHiddenValue = dropdownButton.getElementsByTagName("input")[0]
|
|
|
|
dropdownHiddenValue.setAttribute("value", getDescriptorByLocalString("english", text))
|
2015-02-14 00:17:40 +01:00
|
|
|
dropdownTextSpan.innerHTML = text
|
|
|
|
}
|
|
|
|
|
2015-02-15 05:43:17 +01:00
|
|
|
function validateForm() {
|
|
|
|
valid = true
|
|
|
|
file = document.querySelectorAll("input.file")[0]
|
|
|
|
category = document.querySelectorAll("input.category")[0]
|
|
|
|
subcategory = document.querySelectorAll("input.subcategory")[0]
|
|
|
|
torrentname = document.querySelectorAll("input.name")[0]
|
|
|
|
description = document.querySelectorAll("textarea.description")[0]
|
2015-02-17 23:03:19 +01:00
|
|
|
if(file.value.length <= 0) {
|
|
|
|
valid = false
|
|
|
|
file.parentElement.parentElement.classList.add("has-error")
|
|
|
|
file.parentElement.parentElement.classList.remove("has-success")
|
|
|
|
} else {
|
|
|
|
file.parentElement.parentElement.classList.add("has-success")
|
|
|
|
file.parentElement.parentElement.classList.remove("has-error")
|
|
|
|
}
|
|
|
|
|
|
|
|
if(category.value.length <= 0) {
|
|
|
|
valid = false
|
|
|
|
category.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.classList.add("has-error")
|
|
|
|
category.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.classList.remove("has-success")
|
|
|
|
} else {
|
|
|
|
category.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.classList.add("has-success")
|
|
|
|
category.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.classList.remove("has-error")
|
|
|
|
}
|
|
|
|
|
|
|
|
if(subcategory.value.length <= 0) {
|
|
|
|
valid = false
|
|
|
|
category.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.classList.add("has-error")
|
|
|
|
category.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.classList.remove("has-success")
|
|
|
|
} else {
|
|
|
|
category.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.classList.add("has-success")
|
|
|
|
category.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.classList.remove("has-error")
|
|
|
|
}
|
|
|
|
|
|
|
|
if(torrentname.value.length <= 0) {
|
|
|
|
valid = false
|
|
|
|
torrentname.parentElement.parentElement.classList.add("has-error")
|
|
|
|
torrentname.parentElement.parentElement.classList.remove("has-success")
|
|
|
|
} else {
|
|
|
|
torrentname.parentElement.parentElement.classList.add("has-success")
|
|
|
|
torrentname.parentElement.parentElement.classList.remove("has-error")
|
|
|
|
}
|
|
|
|
|
|
|
|
if(description.value.length <= 0) {
|
|
|
|
valid = false
|
|
|
|
description.parentElement.parentElement.classList.add("has-error")
|
|
|
|
description.parentElement.parentElement.classList.remove("has-success")
|
|
|
|
} else {
|
|
|
|
description.parentElement.parentElement.classList.add("has-success")
|
|
|
|
description.parentElement.parentElement.classList.remove("has-error")
|
|
|
|
}
|
2015-02-15 05:43:17 +01:00
|
|
|
|
2015-02-17 23:03:19 +01:00
|
|
|
return valid;
|
2015-02-15 05:43:17 +01:00
|
|
|
}
|
|
|
|
|
2015-02-15 03:48:39 +01:00
|
|
|
function chunk(string, n) {
|
|
|
|
var ret = "";
|
|
|
|
for(var i=0, len=string.length; i < len; i += n) {
|
|
|
|
if(i==0) {
|
|
|
|
ret = string.substr(i, n)
|
|
|
|
} else {
|
|
|
|
ret += "<br/>" + string.substr(i, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
};
|
2015-02-21 03:01:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
// Bencode Library
|
|
|
|
// Copyright 2014 Steven Goodwin
|
|
|
|
// Released under the GPL, version 2
|
|
|
|
|
|
|
|
// For format details, please see:
|
|
|
|
// http://en.wikipedia.org/wiki/Bencode
|
|
|
|
|
|
|
|
bencode = function() {
|
|
|
|
this.STATE_NULL = 0;
|
|
|
|
this.STATE_INTEGER = 1; // i-?[0-9]e
|
|
|
|
this.STATE_STRING_LENGTH = 2; // [0-9]+:\a+
|
|
|
|
this.STATE_STRING_CONTENT = 3; // [0-9]+:\a+
|
|
|
|
this.STATE_LIST = 4; // l<contents>e
|
|
|
|
this.STATE_DICTIONARY = 5; // d<contents>e
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse accepts an array of characters to process, and the index of the
|
|
|
|
// first character to parse.
|
|
|
|
// It returns an object containing the parsed result (in result.o), and the index
|
|
|
|
// of the next character to parse in result.idx
|
|
|
|
bencode.prototype.parse = function(dataArray, fromIndex) {
|
|
|
|
var length = dataArray.byteLength;
|
|
|
|
var idx = fromIndex;
|
|
|
|
var state = this.STATE_NULL;
|
|
|
|
|
|
|
|
// State data
|
|
|
|
var current = "";
|
|
|
|
var currentObject = null;;
|
|
|
|
// String-specific state data
|
|
|
|
var stringLength;
|
|
|
|
|
|
|
|
while(idx < length) {
|
|
|
|
var c = String.fromCharCode(dataArray[idx]) ;
|
|
|
|
|
|
|
|
switch(state) {
|
|
|
|
case this.STATE_NULL:
|
|
|
|
switch(c) {
|
|
|
|
case 'i':
|
|
|
|
state = this.STATE_INTEGER;
|
|
|
|
current = "";
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
|
|
|
state = this.STATE_STRING_LENGTH;
|
|
|
|
current = c;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
currentObject = new Array();
|
|
|
|
state = this.STATE_LIST;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd':
|
|
|
|
currentObject = new Object();
|
|
|
|
state = this.STATE_DICTIONARY;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
++idx;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case this.STATE_INTEGER:
|
|
|
|
switch(c) {
|
|
|
|
case '-': // we assume that negative numbers start with -
|
|
|
|
current = "-";
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
return { o : current, idx : idx+1 };
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
|
|
|
current += c;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++idx;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case this.STATE_STRING_LENGTH:
|
|
|
|
switch(c) {
|
|
|
|
case ':': // the separator between length and content
|
|
|
|
stringLength = parseInt(current, 10);
|
|
|
|
state = this.STATE_STRING_CONTENT;
|
|
|
|
current = ""; // We now parse the string content
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
|
|
|
current += c;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
++idx;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case this.STATE_STRING_CONTENT:
|
|
|
|
current += c;
|
|
|
|
if (--stringLength == 0) {
|
|
|
|
return { o : current, idx : idx+1 };
|
|
|
|
}
|
|
|
|
|
|
|
|
++idx;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case this.STATE_DICTIONARY:
|
|
|
|
|
|
|
|
if (c == 'e') {
|
|
|
|
return { o : currentObject, idx : idx+1 };
|
|
|
|
} else {
|
|
|
|
var objKey = this.parse(dataArray, idx);
|
|
|
|
var objValue = this.parse(dataArray, objKey.idx);
|
|
|
|
|
|
|
|
currentObject[objKey.o] = objValue.o;
|
|
|
|
idx = objValue.idx;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case this.STATE_LIST:
|
|
|
|
|
|
|
|
if (c == 'e') {
|
|
|
|
return { o : currentObject, idx : idx+1 };
|
|
|
|
} else {
|
|
|
|
var obj = this.parse(dataArray, idx);
|
|
|
|
|
|
|
|
currentObject.push(obj.o);
|
|
|
|
idx = obj.idx;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
bencode.prototype.decode = function(byteArray) {
|
|
|
|
var dataArray = new Uint8Array(byteArray);
|
|
|
|
var result = this.parse(dataArray, 0);
|
|
|
|
|
|
|
|
return result.o;
|
|
|
|
}
|