remove typescript

This commit is contained in:
2020-01-20 12:43:43 +01:00
parent 47b8a4c900
commit 129d6915d2
32 changed files with 751 additions and 15929 deletions

View File

@@ -0,0 +1,164 @@
class UploadManager {
constructor(apiAddress, uploadsFinished) {let um = this;
um.apiAddress = apiAddress;
// Callback function for when the queue is empty
um.uploadsFinished = uploadsFinished;
// Counts the total number of upload jobs
um.jobCounter = 0;
// Queue of files to be uploaded. Format:
// {
// jobID: number,
// file: Blob,
// name: string,
// onProgress: function,
// onFinished: function,
// onFailure: function,
// tries: number
// }
um.uploadQueue = [];
// Here we put successful jobs. The array should be sorted by job ID.
// Format:
// { jobID: number, fileID: string, fileName: string }
um.uploadLog = [];
// Max number of uploading threads at once
um.maxWorkers = 3;
// Threads which are currently uploading
um.activeWorkers = 0;
// Total number of jobs accepted
um.jobCounter = 0;
}
finishedUploads() {let um = this;
um.uploadLog.sort(function(a, b) {
return a.jobID - b.jobID;
})
return um.uploadLog;
}
addFile(
file, // Blob
name, // string
onProgress, // func (progress: number)
onFinished, // func (id: string)
onFailure // func (errorID: string, errorMessage: string)
) {let um = this;
um.uploadQueue.push({
jobID: um.jobCounter,
file: file,
name: name,
onProgress: onProgress,
onFinished: onFinished,
onFailure: onFailure,
tries: 0
});
// Increment the job counter
um.jobCounter++
if (um.activeWorkers < um.maxWorkers) {
// Run the upload function
um.startUpload();
}
}
startUpload() {let um = this;
if (um.uploadQueue.length === 0) {
return; // Nothing to upload
}
if (um.activeWorkers < um.maxWorkers) {
um.activeWorkers++;
um.uploadThread();
}
}
finishUpload() {let um = this;
um.activeWorkers--;
if (
um.uploadQueue.length === 0 &&
um.activeWorkers === 0 &&
typeof(um.uploadsFinished) === "function"
) {
um.uploadsFinished();
return;
}
// Run the upload function for the next file
um.startUpload();
}
uploadThread() {let um = this;
let job = um.uploadQueue.shift(); // Get the first element of the array
console.debug("Starting upload of " + job.name);
let form = new FormData();
form.append("name", job.name);
form.append('file', job.file);
let xhr = new XMLHttpRequest();
xhr.open("POST", um.apiAddress + "/file", true);
xhr.timeout = 21600000; // 6 hours, to account for slow connections
// Report progress updates back to the caller
xhr.upload.addEventListener("progress", function (evt) {
if (evt.lengthComputable && typeof(job.onProgress) === "function") {
job.onProgress(evt.loaded / evt.total);
}
});
xhr.onreadystatechange = function () {
// readystate 4 means the upload is done
if (xhr.readyState !== 4) { return; }
if (xhr.status >= 100 && xhr.status < 400) {
// Request is a success
let resp = JSON.parse(xhr.response);
addUploadHistory(resp.id)
// Log the successful job
um.uploadLog.push({
jobID: job.jobID,
fileID: resp.id,
fileName: job.name
});
if (typeof(job.onFinished) === "function") {
job.onFinished(resp.id);
}
} else if (xhr.status >= 400) {
// Request failed
console.log("Upload error. status: " + xhr.status + " response: " + xhr.response);
let resp = JSON.parse(xhr.response);
if (job.tries === 3) { // Upload failed
job.onFailure(resp.value, resp.message);
} else { // Try again
job.tries++;
um.uploadQueue.push(job);
}
} else {
// Request did not arrive
if (job.tries === 3) { // Upload failed
if (typeof(job.onFailure) === "function") {
job.onFailure(xhr.response, xhr.response);
}
} else { // Try again
job.tries++;
um.uploadQueue.push(job);
}
}
// Finish the upload job
um.finishUpload();
};
xhr.send(form);
}
}

View File

@@ -0,0 +1,68 @@
function renderFileButton(apiURL, id, title, subtitle) {
let btn = document.createElement("a");
btn.classList = "file_button";
btn.href = "/u/"+id;
btn.target = "_blank";
let thumbnail = document.createElement("img");
thumbnail.src = apiURL+"/file/"+id+"/thumbnail?width=80&height=80";
thumbnail.alt = title;
let titleSpan = document.createElement("span");
titleSpan.classList = "file_button_title";
titleSpan.innerText = title;
let br = document.createElement("br");
let subtitleSpan = document.createElement("span");
subtitleSpan.classList = "file_button_subtitle";
subtitleSpan.innerText = subtitle;
btn.appendChild(thumbnail);
btn.appendChild(titleSpan);
btn.appendChild(br);
btn.appendChild(subtitleSpan);
return btn;
}
function renderListButton(apiURL, id, title, subtitle) {
let btn = document.createElement("a");
btn.classList = "file_button";
btn.href = "/l/"+id;
btn.target = "_blank";
let thumbnail = document.createElement("img");
thumbnail.src = apiURL+"/list/"+id+"/thumbnail?width=80&height=80";
thumbnail.alt = title;
let titleSpan = document.createElement("span");
titleSpan.classList = "file_button_title";
titleSpan.innerText = title;
let br = document.createElement("br");
let subtitleSpan = document.createElement("span");
subtitleSpan.classList = "file_button_subtitle";
subtitleSpan.innerText = subtitle;
btn.appendChild(thumbnail);
btn.appendChild(titleSpan);
btn.appendChild(br);
btn.appendChild(subtitleSpan);
return btn;
}
function addUploadHistory(fileID) {
// Make sure the user is not logged in, for privacy. This keeps the
// files uploaded while logged in and anonymously uploaded files
// separated
if (document.cookie.includes("pd_auth_key")) { return; }
let uploads = localStorage.getItem("uploaded_files");
if (uploads === null) { uploads = ""; }
// Check if there are not too many values stored
if (uploads.length > 3600) {
// 3600 characters is enough to store 400 file IDs. If we exceed that
// number we'll drop the last two items
uploads = uploads.substring(
uploads.indexOf(",") + 1
).substring(
uploads.indexOf(",") + 1
);
}
// Save the new ID
localStorage.setItem("uploaded_files", fileID + "," + uploads);
}