Skip to content
This repository has been archived by the owner on Dec 25, 2023. It is now read-only.

Support upload_processing_limit #246

Merged
merged 4 commits into from
Feb 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions scripts/main/lychee.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ let lychee = {

album_subtitle_type: "oldstyle",

upload_processing_limit: 2,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change it to 2? And why only in this least important place?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was testing. :'D


// this is device specific config, in this case default is Desktop.
header_auto_hide: true,
active_focus_on_page_load: false,
Expand Down Expand Up @@ -250,6 +252,8 @@ lychee.init = function () {
lychee.admin = !lychee.api_V2;
lychee.nsfw_visible_saved = lychee.nsfw_visible;

lychee.upload_processing_limit = parseInt(data.config.upload_processing_limit) || 4;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong. This prevents the value of 0 from being used. I don't think we can use this || syntax for numerals that can be 0.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hummmm indeed, I did not think about this. The problem is that if you send null to parseInt you get NaN which obviously crash all the upload checks afterwards.


// leftMenu
leftMenu.build();
leftMenu.bind();
Expand Down
68 changes: 34 additions & 34 deletions scripts/main/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ upload.start = {
let albumID = album.getID();
let error = false;
let warning = false;
let processing_count = 0;
let next_upload = 0;
let currently_uploading = false;

const process = function (_files, file) {
const process = function (file_num) {
let formData = new FormData();
let xhr = new XMLHttpRequest();
let pre_progress = 0;
let progress = 0;
let next_file_started = false;

const finish = function () {
window.onbeforeunload = null;
Expand Down Expand Up @@ -71,15 +73,14 @@ upload.start = {

formData.append("function", "Photo::add");
formData.append("albumID", albumID);
formData.append(0, file);
formData.append(0, files[file_num]);

var api_url = api.get_url("Photo::add");

xhr.open("POST", api_url);

xhr.onload = function () {
let data = null;
let wait = false;
let errorText = "";

const isNumber = (n) => !isNaN(parseFloat(n)) && isFinite(n);
Expand All @@ -100,12 +101,10 @@ upload.start = {
data = "";
}

file.ready = true;

// Set status
if (xhr.status === 200 && isNumber(data)) {
// Success
$(".basicModal .rows .row:nth-child(" + (file.num + 1) + ") .status")
$(".basicModal .rows .row:nth-child(" + (file_num + 1) + ") .status")
.html(lychee.locale["UPLOAD_FINISHED"])
.addClass("success");
} else {
Expand All @@ -123,7 +122,7 @@ upload.start = {
error = true;

// Error Status
$(".basicModal .rows .row:nth-child(" + (file.num + 1) + ") .status")
$(".basicModal .rows .row:nth-child(" + (file_num + 1) + ") .status")
.html(lychee.locale["UPLOAD_FAILED"])
.addClass("error");

Expand All @@ -134,7 +133,7 @@ upload.start = {
warning = true;

// Warning Status
$(".basicModal .rows .row:nth-child(" + (file.num + 1) + ") .status")
$(".basicModal .rows .row:nth-child(" + (file_num + 1) + ") .status")
.html(lychee.locale["UPLOAD_SKIPPED"])
.addClass("warning");

Expand All @@ -145,29 +144,34 @@ upload.start = {
error = true;

// Error Status
$(".basicModal .rows .row:nth-child(" + (file.num + 1) + ") .status")
$(".basicModal .rows .row:nth-child(" + (file_num + 1) + ") .status")
.html(lychee.locale["UPLOAD_FAILED"])
.addClass("error");

// Throw error
lychee.error(lychee.locale["UPLOAD_ERROR_UNKNOWN"], xhr, data);
}

$(".basicModal .rows .row:nth-child(" + (file.num + 1) + ") p.notice")
$(".basicModal .rows .row:nth-child(" + (file_num + 1) + ") p.notice")
.html(errorText)
.show();
}

// Check if there are file which are not finished
for (let i = 0; i < _files.length; i++) {
if (_files[i].ready === false) {
wait = true;
break;
}
processing_count--;

// Upload next file
if (
!currently_uploading &&
(processing_count < lychee.upload_processing_limit || lychee.upload_processing_limit === 0) &&
next_upload < files.length
) {
process(next_upload);
}

// Finish upload when all files are finished
if (wait === false) finish();
if (!currently_uploading && processing_count === 0) {
finish();
}
};

xhr.upload.onprogress = function (e) {
Expand All @@ -178,49 +182,45 @@ upload.start = {

// Set progress when progress has changed
if (progress > pre_progress) {
$(".basicModal .rows .row:nth-child(" + (file.num + 1) + ") .status").html(progress + "%");
$(".basicModal .rows .row:nth-child(" + (file_num + 1) + ") .status").html(progress + "%");
pre_progress = progress;
}

if (progress >= 100 && next_file_started === false) {
if (progress >= 100) {
// Scroll to the uploading file
let scrollPos = 0;
if (file.num + 1 > 4) scrollPos = (file.num + 1 - 4) * 40;
if (file_num + 1 > 4) scrollPos = (file_num + 1 - 4) * 40;
$(".basicModal .rows").scrollTop(scrollPos);

// Set status to processing
$(".basicModal .rows .row:nth-child(" + (file.num + 1) + ") .status").html(lychee.locale["UPLOAD_PROCESSING"]);
$(".basicModal .rows .row:nth-child(" + (file_num + 1) + ") .status").html(lychee.locale["UPLOAD_PROCESSING"]);
processing_count++;
currently_uploading = false;

// Upload next file
if (file.next != null) {
process(files, file.next);
next_file_started = true;
if ((processing_count < lychee.upload_processing_limit || lychee.upload_processing_limit === 0) && next_upload < files.length) {
process(next_upload);
}
}
};

currently_uploading = true;
next_upload++;

xhr.setRequestHeader("X-XSRF-TOKEN", csrf.getCookie("XSRF-TOKEN"));
xhr.send(formData);
};

if (files.length <= 0) return false;
if (albumID === false || visible.albums() === true) albumID = 0;

for (let i = 0; i < files.length; i++) {
files[i].num = i;
files[i].ready = false;

if (i < files.length - 1) files[i].next = files[i + 1];
else files[i].next = null;
}

window.onbeforeunload = function () {
return lychee.locale["UPLOAD_IN_PROGRESS"];
};

upload.show(lychee.locale["UPLOAD_UPLOADING"], files, function () {
// Upload first file
process(files, files[0]);
process(next_upload);
});
},

Expand Down