2018-01-17 01:29:35 +01:00
|
|
|
var uploader = null;
|
|
|
|
var finishedUploads = new Array();
|
|
|
|
var totalUploads = 0;
|
|
|
|
var UploadProgressBar = /** @class */ (function () {
|
|
|
|
function UploadProgressBar(file) {
|
|
|
|
this.file = file;
|
2018-01-18 23:33:44 +01:00
|
|
|
this.name = file.name;
|
2018-01-17 01:29:35 +01:00
|
|
|
this.queueNum = totalUploads;
|
|
|
|
totalUploads++;
|
|
|
|
this.uploadDiv = document.createElement("a");
|
2018-01-18 23:33:44 +01:00
|
|
|
this.uploadDiv.setAttribute("class", "file_button");
|
2018-01-17 01:29:35 +01:00
|
|
|
this.uploadDiv.innerText = "Queued\n" + this.file.name;
|
|
|
|
this.uploadDivJQ = $(this.uploadDiv);
|
2018-07-09 12:12:32 +02:00
|
|
|
$("#uploads_queue").append(this.uploadDivJQ.hide().fadeIn('slow').css("display", ""));
|
2018-01-17 01:29:35 +01:00
|
|
|
}
|
|
|
|
UploadProgressBar.prototype.onProgress = function (progress) {
|
|
|
|
this.uploadDiv.innerText = "Uploading... " + Math.round(progress * 1000) / 10 + "%\n" + this.file.name;
|
|
|
|
this.uploadDiv.setAttribute('style', 'background: linear-gradient('
|
|
|
|
+ 'to right, '
|
|
|
|
+ '#111 0%, '
|
|
|
|
+ 'var(--highlight_color) ' + ((progress * 100)) + '%, '
|
|
|
|
+ '#111 ' + ((progress * 100) + 1) + '%)');
|
|
|
|
};
|
|
|
|
UploadProgressBar.prototype.onFinished = function (id) {
|
|
|
|
finishedUploads[this.queueNum] = id;
|
|
|
|
this.uploadDiv.setAttribute('style', 'background: #111');
|
|
|
|
this.uploadDiv.setAttribute('href', '/u/' + id);
|
|
|
|
this.uploadDiv.setAttribute("target", "_blank");
|
2018-06-25 23:05:18 +02:00
|
|
|
this.uploadDivJQ.html('<img src="' + apiEndpoint + '/file/' + id + '/thumbnail" alt="' + this.file.name + '"/>'
|
2018-01-17 01:29:35 +01:00
|
|
|
+ this.file.name + '<br/>'
|
|
|
|
+ '<span style="color: var(--highlight_color);">' + window.location.hostname + '/u/' + id + '</span>');
|
|
|
|
};
|
2018-01-17 01:41:58 +01:00
|
|
|
UploadProgressBar.prototype.onFailure = function (response, error) {
|
|
|
|
this.uploadDiv.setAttribute('style', 'background: #821C40');
|
|
|
|
this.uploadDivJQ.html(this.file.name + '<br/>'
|
2018-08-06 23:33:03 +02:00
|
|
|
+ 'Upload failed after three tries!<br/>'
|
|
|
|
+ "Message: " + error);
|
2018-01-17 01:41:58 +01:00
|
|
|
};
|
2018-01-17 01:29:35 +01:00
|
|
|
return UploadProgressBar;
|
|
|
|
}());
|
|
|
|
function handleUploads(files) {
|
|
|
|
if (uploader === null) {
|
|
|
|
uploader = new UploadManager();
|
|
|
|
$("#uploads_queue").animate({ "height": "340px" }, { "duration": 2000, queue: false });
|
|
|
|
}
|
|
|
|
for (var i = 0; i < files.length; i++) {
|
|
|
|
uploader.uploadFile(new UploadProgressBar(files.item(i)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Form upload handlers
|
|
|
|
*/
|
|
|
|
// Relay click event to hidden file field
|
|
|
|
$("#select_file_button").click(function () { $("#file_input_field").click(); });
|
|
|
|
$("#file_input_field").change(function (evt) {
|
|
|
|
handleUploads(evt.target.files);
|
|
|
|
// This resets the file input field
|
|
|
|
// http://stackoverflow.com/questions/1043957/clearing-input-type-file-using-jquery
|
|
|
|
$('#file_name').html("");
|
|
|
|
$("#file_upload_button").css("visibility", "hidden");
|
|
|
|
$("#file_input_field").wrap("<form>").closest("form").get(0).reset();
|
|
|
|
$("#file_input_field").unwrap();
|
|
|
|
});
|
|
|
|
/*
|
|
|
|
* Drag 'n Drop upload handlers
|
|
|
|
*/
|
|
|
|
$(document).on('dragover', function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
$(document).on('dragenter', function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
document.addEventListener('drop', function (e) {
|
|
|
|
if (e.dataTransfer && e.dataTransfer.files.length > 0) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
handleUploads(e.dataTransfer.files);
|
|
|
|
}
|
|
|
|
});
|
2018-07-11 22:46:44 +02:00
|
|
|
// Style selector
|
|
|
|
$("input[name=style]").change(function (evt) {
|
|
|
|
Cookie.write("style", evt.target.id.substring(6));
|
|
|
|
location.reload();
|
|
|
|
});
|
2018-01-17 01:29:35 +01:00
|
|
|
var Cookie;
|
|
|
|
(function (Cookie) {
|
|
|
|
function read(name) {
|
|
|
|
var result = new RegExp('(?:^|; )' + encodeURIComponent(name) + '=([^;]*)').exec(document.cookie);
|
|
|
|
return result ? result[1] : null;
|
|
|
|
}
|
|
|
|
Cookie.read = read;
|
|
|
|
function write(name, value, days) {
|
|
|
|
if (!days) {
|
|
|
|
days = 365 * 20;
|
|
|
|
}
|
|
|
|
var date = new Date();
|
|
|
|
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
|
|
|
|
var expires = "; expires=" + date.toUTCString();
|
|
|
|
document.cookie = name + "=" + value + expires + "; path=/";
|
|
|
|
}
|
|
|
|
Cookie.write = write;
|
|
|
|
function remove(name) {
|
|
|
|
write(name, "", -1);
|
|
|
|
}
|
|
|
|
Cookie.remove = remove;
|
|
|
|
})(Cookie || (Cookie = {}));
|
|
|
|
var UploadManager = /** @class */ (function () {
|
|
|
|
function UploadManager() {
|
|
|
|
this.uploadQueue = new Array();
|
|
|
|
this.uploadThreads = new Array();
|
2018-01-17 11:19:16 +01:00
|
|
|
this.maxThreads = 3;
|
2018-01-17 01:29:35 +01:00
|
|
|
}
|
|
|
|
UploadManager.prototype.uploadFile = function (file) {
|
|
|
|
console.debug("Adding upload to queue");
|
|
|
|
this.uploadQueue.push(file);
|
|
|
|
if (this.uploadThreads.length < this.maxThreads) {
|
|
|
|
console.debug("Starting upload thread");
|
2018-01-17 11:13:15 +01:00
|
|
|
var thread_1 = new UploadWorker(this);
|
|
|
|
this.uploadThreads.push(thread_1);
|
|
|
|
setTimeout(function () { thread_1.start(); }, 0); // Start a new upload thread
|
2018-01-17 01:29:35 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (var i = 0; i < this.uploadThreads.length; i++) {
|
2018-01-17 11:19:16 +01:00
|
|
|
this.uploadThreads[i].start();
|
2018-01-17 01:29:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
UploadManager.prototype.grabFile = function () {
|
|
|
|
if (this.uploadQueue.length > 0) {
|
|
|
|
return this.uploadQueue.shift();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return UploadManager;
|
|
|
|
}());
|
|
|
|
var UploadWorker = /** @class */ (function () {
|
|
|
|
function UploadWorker(manager) {
|
|
|
|
this.tries = 0;
|
|
|
|
this.uploading = false;
|
|
|
|
this.manager = manager;
|
|
|
|
}
|
|
|
|
UploadWorker.prototype.start = function () {
|
2018-01-17 11:02:12 +01:00
|
|
|
if (!this.uploading) {
|
|
|
|
this.newFile();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
UploadWorker.prototype.newFile = function () {
|
2018-01-17 01:29:35 +01:00
|
|
|
var file = this.manager.grabFile();
|
|
|
|
if (file === undefined) {
|
|
|
|
this.uploading = false;
|
|
|
|
console.debug("No files left in queue");
|
|
|
|
return; // Stop the thread
|
|
|
|
}
|
|
|
|
this.uploading = true;
|
|
|
|
this.tries = 0;
|
|
|
|
this.upload(file);
|
|
|
|
};
|
|
|
|
UploadWorker.prototype.upload = function (file) {
|
2018-01-18 23:33:44 +01:00
|
|
|
console.debug("Starting upload of " + file.name);
|
2018-01-17 01:29:35 +01:00
|
|
|
var formData = new FormData();
|
2018-01-18 23:33:44 +01:00
|
|
|
formData.append("name", file.name);
|
2018-08-06 23:33:03 +02:00
|
|
|
formData.append('file', file.file);
|
2018-01-17 01:29:35 +01:00
|
|
|
var that = this; // jquery changes the definiton of "this"
|
|
|
|
$.ajax({
|
2018-01-29 22:00:13 +01:00
|
|
|
type: 'POST',
|
2018-06-25 23:05:18 +02:00
|
|
|
url: apiEndpoint + "/file",
|
2018-01-17 01:29:35 +01:00
|
|
|
data: formData,
|
2018-08-06 23:33:03 +02:00
|
|
|
timeout: 21600000,
|
2018-01-17 01:29:35 +01:00
|
|
|
cache: false,
|
2018-01-29 22:00:13 +01:00
|
|
|
async: true,
|
2018-01-17 01:29:35 +01:00
|
|
|
crossDomain: false,
|
|
|
|
contentType: false,
|
|
|
|
processData: false,
|
|
|
|
xhr: function () {
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.upload.addEventListener("progress", function (evt) {
|
|
|
|
if (evt.lengthComputable) {
|
|
|
|
file.onProgress(evt.loaded / evt.total);
|
|
|
|
}
|
|
|
|
}, false);
|
|
|
|
return xhr;
|
|
|
|
},
|
|
|
|
success: function (data) {
|
|
|
|
file.onFinished(data.id);
|
|
|
|
that.setHistoryCookie(data.id);
|
2018-01-17 11:02:12 +01:00
|
|
|
console.log("Done: " + data.id);
|
|
|
|
that.newFile(); // Continue uploading on this thread
|
2018-01-17 01:29:35 +01:00
|
|
|
},
|
|
|
|
error: function (xhr, status, error) {
|
2018-01-17 11:02:12 +01:00
|
|
|
console.log("status: " + status + " error: " + error);
|
2018-01-17 01:29:35 +01:00
|
|
|
if (that.tries === 3) {
|
2018-01-17 01:41:58 +01:00
|
|
|
file.onFailure(status, error);
|
2018-01-17 11:13:15 +01:00
|
|
|
setTimeout(function () { that.newFile(); }, 2000); // Try to continue
|
2018-01-17 01:29:35 +01:00
|
|
|
return; // Upload failed
|
|
|
|
}
|
|
|
|
// Try again
|
|
|
|
that.tries++;
|
2018-01-17 11:13:15 +01:00
|
|
|
setTimeout(function () { that.upload(file); }, that.tries * 3000);
|
2018-01-17 01:29:35 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
UploadWorker.prototype.setHistoryCookie = function (id) {
|
|
|
|
var uc = Cookie.read("pduploads");
|
|
|
|
// First upload in this browser
|
|
|
|
if (uc === null) {
|
|
|
|
Cookie.write("pduploads", id + ".", undefined);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (uc.length > 2000) {
|
|
|
|
// Cookie is becoming too long, drop the oldest two files
|
|
|
|
uc = uc.substring(uc.indexOf(".") + 1).substring(uc.indexOf(".") + 1);
|
|
|
|
}
|
|
|
|
Cookie.write("pduploads", uc + id + ".", undefined);
|
|
|
|
};
|
|
|
|
return UploadWorker;
|
|
|
|
}());
|