From 962ff1d104f930cdd53e0ea8927655e297b9fdfd Mon Sep 17 00:00:00 2001 From: kamil4 Date: Fri, 12 Feb 2021 23:15:03 -0600 Subject: [PATCH 1/4] Support upload_processing_limit --- scripts/main/lychee.js | 4 +++ scripts/main/upload.js | 65 ++++++++++++++++++++---------------------- 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/scripts/main/lychee.js b/scripts/main/lychee.js index 836feb2d..c0ee17e4 100644 --- a/scripts/main/lychee.js +++ b/scripts/main/lychee.js @@ -50,6 +50,8 @@ let lychee = { album_subtitle_type: "oldstyle", + upload_processing_limit: 4, + // this is device specific config, in this case default is Desktop. header_auto_hide: true, active_focus_on_page_load: false, @@ -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); + // leftMenu leftMenu.build(); leftMenu.bind(); diff --git a/scripts/main/upload.js b/scripts/main/upload.js index a1c6847e..bb56847c 100644 --- a/scripts/main/upload.js +++ b/scripts/main/upload.js @@ -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; @@ -71,7 +73,7 @@ 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"); @@ -79,7 +81,6 @@ upload.start = { xhr.onload = function () { let data = null; - let wait = false; let errorText = ""; const isNumber = (n) => !isNaN(parseFloat(n)) && isFinite(n); @@ -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 { @@ -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"); @@ -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"); @@ -145,7 +144,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"); @@ -153,21 +152,24 @@ upload.start = { 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) { @@ -178,49 +180,44 @@ 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++); } } }; xhr.setRequestHeader("X-XSRF-TOKEN", csrf.getCookie("XSRF-TOKEN")); xhr.send(formData); + currently_uploading = true; }; 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++); }); }, From 03d0e95369d08f7f2492df75a14d301c3df33b8a Mon Sep 17 00:00:00 2001 From: kamil4 Date: Fri, 12 Feb 2021 23:24:15 -0600 Subject: [PATCH 2/4] Make it prettier --- scripts/main/upload.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/scripts/main/upload.js b/scripts/main/upload.js index bb56847c..311c8ac1 100644 --- a/scripts/main/upload.js +++ b/scripts/main/upload.js @@ -160,9 +160,11 @@ upload.start = { processing_count--; // Upload next file - if (!currently_uploading && + if ( + !currently_uploading && (processing_count < lychee.upload_processing_limit || lychee.upload_processing_limit === 0) && - next_upload < files.length) { + next_upload < files.length + ) { process(next_upload++); } @@ -196,8 +198,7 @@ upload.start = { currently_uploading = false; // Upload next file - if ((processing_count < lychee.upload_processing_limit || lychee.upload_processing_limit === 0) && - next_upload < files.length) { + if ((processing_count < lychee.upload_processing_limit || lychee.upload_processing_limit === 0) && next_upload < files.length) { process(next_upload++); } } From 3ecd8300e0076491be5e6f43643e136e53477278 Mon Sep 17 00:00:00 2001 From: kamil4 Date: Fri, 12 Feb 2021 23:32:32 -0600 Subject: [PATCH 3/4] Fix smells (*I* liked it the way it was!) --- scripts/main/upload.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/scripts/main/upload.js b/scripts/main/upload.js index 311c8ac1..45c807af 100644 --- a/scripts/main/upload.js +++ b/scripts/main/upload.js @@ -165,7 +165,7 @@ upload.start = { (processing_count < lychee.upload_processing_limit || lychee.upload_processing_limit === 0) && next_upload < files.length ) { - process(next_upload++); + process(next_upload); } // Finish upload when all files are finished @@ -199,14 +199,16 @@ upload.start = { // Upload next file if ((processing_count < lychee.upload_processing_limit || lychee.upload_processing_limit === 0) && next_upload < files.length) { - process(next_upload++); + process(next_upload); } } }; + currently_uploading = true; + next_upload++; + xhr.setRequestHeader("X-XSRF-TOKEN", csrf.getCookie("XSRF-TOKEN")); xhr.send(formData); - currently_uploading = true; }; if (files.length <= 0) return false; @@ -218,7 +220,7 @@ upload.start = { upload.show(lychee.locale["UPLOAD_UPLOADING"], files, function () { // Upload first file - process(next_upload++); + process(next_upload); }); }, From 6a26422d155ebcdf993425ab5088bdeddf387a07 Mon Sep 17 00:00:00 2001 From: ildyria Date: Sat, 13 Feb 2021 13:20:09 +0100 Subject: [PATCH 4/4] fix NaN if backend is not updated --- scripts/main/lychee.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/main/lychee.js b/scripts/main/lychee.js index c0ee17e4..f4859b18 100644 --- a/scripts/main/lychee.js +++ b/scripts/main/lychee.js @@ -50,7 +50,7 @@ let lychee = { album_subtitle_type: "oldstyle", - upload_processing_limit: 4, + upload_processing_limit: 2, // this is device specific config, in this case default is Desktop. header_auto_hide: true, @@ -252,7 +252,7 @@ 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); + lychee.upload_processing_limit = parseInt(data.config.upload_processing_limit) || 4; // leftMenu leftMenu.build();