Updated to new EEPROM script.

This commit is contained in:
flash 2024-02-02 21:42:40 +00:00
parent eb81ed7a82
commit fe77f1616c
4 changed files with 48 additions and 69 deletions

View file

@ -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(),

View file

@ -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 = <div class="eeprom-widget-history-items"/>;
const eepromHandleFileUpload = file => {
const eepromHandleFileUpload = async file => {
const uploadElemNameValue = <div class="eeprom-widget-file-name-value" title={file.name}>{file.name}</div>;
const uploadElemName = <a class="eeprom-widget-file-name" target="_blank">{uploadElemNameValue}</a>;
const uploadElemProgressText = <div class="eeprom-widget-file-progress">Please wait...</div>;
@ -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(<a href="javascript:void(0)" onclick={() => insertTheLinkIntoTheBoxEx2()}>Insert</a>);
uploadElemProgressText.appendChild($t(' '));
uploadElemProgressText.appendChild(<a href="javascript:void(0)" onclick={() => {
eepromClient.deleteUpload(fileInfo).start();
explodeUploadElem();
eepromClient.delete(fileInfo)
.then(() => explodeUploadElem())
.catch(ex => {
console.error(ex);
MszShowMessageBox(ex, 'Upload Error');
});
}}>Delete</a>);
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 = <input type="file" multiple={true} class="eeprom-widget-form-input"

View file

@ -67,16 +67,12 @@ const MszMessagesReply = function(element) {
MszEEPROM.init()
.catch(() => 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 => {

View file

@ -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;