From fe77f1616ce72de225ed0ad6320996d7796b233c Mon Sep 17 00:00:00 2001 From: flashwave Date: Fri, 2 Feb 2024 21:42:40 +0000 Subject: [PATCH] Updated to new EEPROM script. --- assets/misuzu.js/ext/eeprom.js | 2 +- assets/misuzu.js/forum/editor.jsx | 83 ++++++++++------------------- assets/misuzu.js/messages/reply.jsx | 25 +++++---- assets/misuzu.js/msgbox.jsx | 7 ++- 4 files changed, 48 insertions(+), 69 deletions(-) diff --git a/assets/misuzu.js/ext/eeprom.js b/assets/misuzu.js/ext/eeprom.js index 5eda181..1755429 100644 --- a/assets/misuzu.js/ext/eeprom.js +++ b/assets/misuzu.js/ext/eeprom.js @@ -19,7 +19,7 @@ const MszEEPROM = (() => { const scriptElem = $e({ tag: 'script', attrs: { - src: `${peepPath}/eeprom.js`, + src: `${peepPath}/scripts/eepromv1a.js`, charset: 'utf-8', type: 'text/javascript', onerror: () => reject(), diff --git a/assets/misuzu.js/forum/editor.jsx b/assets/misuzu.js/forum/editor.jsx index cc0608c..7b9a145 100644 --- a/assets/misuzu.js/forum/editor.jsx +++ b/assets/misuzu.js/forum/editor.jsx @@ -22,10 +22,10 @@ const MszForumEditor = function(form) { MszEEPROM.init() .catch(() => console.error('Failed to initialise EEPROM')) .then(() => { - const eepromClient = new EEPROM(peepApp, `${peepPath}/uploads`, ''); + const eepromClient = new EEPROM(peepApp, peepPath); const eepromHistory =
; - const eepromHandleFileUpload = file => { + const eepromHandleFileUpload = async file => { const uploadElemNameValue =
{file.name}
; const uploadElemName = {uploadElemNameValue}; const uploadElemProgressText =
Please wait...
; @@ -46,62 +46,22 @@ const MszForumEditor = function(form) { eepromHistory.appendChild(uploadElem); const explodeUploadElem = () => $r(uploadElem); - const uploadTask = eepromClient.createUpload(file); + const uploadTask = eepromClient.create(file); - uploadTask.onProgress = function(progressInfo) { - const progressValue = `${progressInfo.progress}%`; - uploadElemProgressBarValue.style.width = progressValue; - uploadElemProgressText.textContent = `${progressValue} (${progressInfo.total - progressInfo.loaded} bytes remaining)`; - }; + uploadTask.onProgress(prog => { + const progress = `}%`; + uploadElemProgressBarValue.style.width = `${Math.ceil(prog.progress * 100)}%`; + uploadElemProgressText.textContent = `${prog.progress.toLocaleString(undefined, { style: 'percent' })} (${prog.total - prog.loaded} bytes remaining)`; + }); - uploadTask.onFailure = function(errorInfo) { - if(!errorInfo.userAborted) { - let errorText = 'Was unable to upload file.'; + try { + const fileInfo = await uploadTask.start(); - switch(errorInfo.error) { - case EEPROM.ERR_INVALID: - errorText = 'Upload request was invalid.'; - break; - case EEPROM.ERR_AUTH: - errorText = 'Upload authentication failed, refresh and try again.'; - break; - case EEPROM.ERR_ACCESS: - errorText = "You're not allowed to upload files."; - break; - case EEPROM.ERR_GONE: - errorText = 'Upload client has a configuration error or the server is gone.'; - break; - case EEPROM.ERR_DMCA: - errorText = 'This file has been uploaded before and was removed for copyright reasons, you cannot upload this file.'; - break; - case EEPROM.ERR_SERVER: - errorText = 'Upload server returned a critical error, try again later.'; - break; - case EEPROM.ERR_SIZE: - if(errorInfo.maxSize < 1) - errorText = 'Selected file is too large.'; - else { - const types = ['bytes', 'KB', 'MB', 'GB', 'TB'], - typeIndex = parseInt(Math.floor(Math.log(errorInfo.maxSize) / Math.log(1024))), - number = Math.round(errorInfo.maxSize / Math.pow(1024, _i), 2); - - errorText = `Upload may not be larger than ${number} ${types[typeIndex]}.`; - } - break; - } - - uploadElem.classList.add('eeprom-widget-file-fail'); - uploadElemProgressText.textContent = errorText; - MszShowMessageBox(errorText, 'Upload Error'); - } - }; - - uploadTask.onComplete = function(fileInfo) { uploadElem.classList.add('eeprom-widget-file-done'); uploadElemName.href = fileInfo.url; uploadElemProgressText.textContent = ''; - const insertTheLinkIntoTheBoxEx2 = function() { + const insertTheLinkIntoTheBoxEx2 = () => { const parserMode = parseInt(parserElem.value); let insertText = location.protocol + fileInfo.url; @@ -124,14 +84,27 @@ const MszForumEditor = function(form) { uploadElemProgressText.appendChild( insertTheLinkIntoTheBoxEx2()}>Insert); uploadElemProgressText.appendChild($t(' ')); uploadElemProgressText.appendChild( { - eepromClient.deleteUpload(fileInfo).start(); - explodeUploadElem(); + eepromClient.delete(fileInfo) + .then(() => explodeUploadElem()) + .catch(ex => { + console.error(ex); + MszShowMessageBox(ex, 'Upload Error'); + }); }}>Delete); insertTheLinkIntoTheBoxEx2(); - }; + } catch(ex) { + let errorText = 'Upload aborted.'; - uploadTask.start(); + if(!ex.aborted) { + console.error(ex); + errorText = ex.toString(); + } + + uploadElem.classList.add('eeprom-widget-file-fail'); + uploadElemProgressText.textContent = errorText; + await MszShowMessageBox(errorText, 'Upload Error'); + } }; const eepromFormInput = console.error('Failed to initialise EEPROM')) .then(() => { - const eepromClient = new EEPROM(peepApp, `${peepPath}/uploads`, ''); - const eepromHandleFileUpload = file => { - const uploadTask = eepromClient.createUpload(file); + const eepromClient = new EEPROM(peepApp, peepPath); + const eepromHandleFileUpload = async file => { + const uploadTask = eepromClient.create(file); - uploadTask.onFailure = errorInfo => { - if(!errorInfo.userAborted) - MszShowMessageBox('Was unable to upload file.', 'Upload Error'); - }; - - uploadTask.onComplete = fileInfo => { + try { + const fileInfo = await uploadTask.start(); const parserMode = parseInt(parserSelect.value); let insertText = location.protocol + fileInfo.url; @@ -94,9 +90,16 @@ const MszMessagesReply = function(element) { $insertTags(bodyElem, insertText, ''); bodyElem.value = bodyElem.value.trim(); - }; + } catch(ex) { + let errorText = 'Upload aborted.'; - uploadTask.start(); + if(!ex.aborted) { + console.error(ex); + errorText = ex.toString(); + } + + await MszShowMessageBox(errorText, 'Upload Error'); + } }; bodyElem.addEventListener('paste', ev => { diff --git a/assets/misuzu.js/msgbox.jsx b/assets/misuzu.js/msgbox.jsx index 023fb93..0051eef 100644 --- a/assets/misuzu.js/msgbox.jsx +++ b/assets/misuzu.js/msgbox.jsx @@ -12,8 +12,11 @@ const MszShowConfirmBox = async (text, title, target) => { }; const MszShowMessageBox = (text, title, buttons, target) => { - if(typeof text !== 'string') - throw 'text must be a string'; + if(typeof text !== 'string') { + if(text !== undefined && text !== null && typeof text.toString === 'function') + text = text.toString(); + else throw 'text must be a string'; + } if(!(target instanceof Element)) target = document.body;