Switched to integrated EEPROM client.
This commit is contained in:
parent
77bfc10475
commit
2a191c5b19
3 changed files with 266 additions and 216 deletions
|
@ -1,37 +1,95 @@
|
|||
#include utility.js
|
||||
#include xhr.js
|
||||
|
||||
const MszEEPROM = (() => {
|
||||
let eepromScript;
|
||||
const MszEEPROM = function(appId, endPoint) {
|
||||
if(typeof appId !== 'string')
|
||||
throw 'appId must be a string';
|
||||
if(typeof endPoint !== 'string')
|
||||
throw 'endPoint must be a string';
|
||||
|
||||
return {
|
||||
init: () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
if(eepromScript !== undefined) {
|
||||
resolve(false);
|
||||
return;
|
||||
}
|
||||
create: fileInput => {
|
||||
if(!(fileInput instanceof File))
|
||||
throw 'fileInput must be an instance of window.File';
|
||||
|
||||
if(typeof peepPath !== 'string') {
|
||||
reject();
|
||||
return;
|
||||
}
|
||||
let userAborted = false;
|
||||
let abortHandler;
|
||||
let progressHandler;
|
||||
|
||||
const scriptElem = $e({
|
||||
tag: 'script',
|
||||
attrs: {
|
||||
src: `${peepPath}/scripts/eepromv1a.js`,
|
||||
charset: 'utf-8',
|
||||
type: 'text/javascript',
|
||||
onerror: () => reject(),
|
||||
onload: () => {
|
||||
eepromScript = scriptElem;
|
||||
resolve(true);
|
||||
},
|
||||
},
|
||||
const reportProgress = ev => {
|
||||
if(progressHandler !== undefined)
|
||||
progressHandler({
|
||||
loaded: ev.loaded,
|
||||
total: ev.total,
|
||||
progress: ev.total <= 0 ? 0 : ev.loaded / ev.total,
|
||||
});
|
||||
};
|
||||
|
||||
document.body.appendChild(scriptElem);
|
||||
});
|
||||
return {
|
||||
abort: () => {
|
||||
userAborted = true;
|
||||
if(typeof abortHandler === 'function')
|
||||
abortHandler();
|
||||
},
|
||||
onProgress: handler => {
|
||||
if(typeof handler !== 'function')
|
||||
throw 'handler must be a function';
|
||||
progressHandler = handler;
|
||||
},
|
||||
start: async () => {
|
||||
if(userAborted)
|
||||
throw 'File upload was cancelled by the user, it cannot be restarted.';
|
||||
|
||||
try {
|
||||
const formData = new FormData;
|
||||
formData.append('src', appId);
|
||||
formData.append('file', fileInput);
|
||||
|
||||
const { status, body } = await $x.post(`${endPoint}/uploads`, {
|
||||
type: 'json',
|
||||
authed: true,
|
||||
upload: reportProgress,
|
||||
abort: handler => abortHandler = handler,
|
||||
}, formData);
|
||||
if(body === null)
|
||||
throw "The upload server didn't return the metadata for some reason.";
|
||||
|
||||
if(status !== 201)
|
||||
throw body.english ?? body.error ?? `Upload failed with status code ${status}`;
|
||||
|
||||
body.isImage = () => body.type.startsWith('image/');
|
||||
body.isVideo = () => body.type.startsWith('video/');
|
||||
body.isAudio = () => body.type.startsWith('audio/');
|
||||
body.isMedia = () => body.isImage() || body.isAudio() || body.isVideo();
|
||||
|
||||
return Object.freeze(body);
|
||||
} catch(ex) {
|
||||
if(userAborted)
|
||||
throw '';
|
||||
|
||||
console.error(ex);
|
||||
throw ex;
|
||||
}
|
||||
},
|
||||
};
|
||||
})();
|
||||
},
|
||||
|
||||
delete: async fileInfo => {
|
||||
if(typeof fileInfo !== 'object')
|
||||
throw 'fileInfo must be an object';
|
||||
if(typeof fileInfo.urlf !== 'string')
|
||||
throw 'fileInfo.urlf must be a string';
|
||||
|
||||
const { status, body } = await $x.delete(fileInfo.urlf, {
|
||||
type: 'json',
|
||||
authed: true,
|
||||
});
|
||||
|
||||
if(status !== 204) {
|
||||
if(body === null)
|
||||
throw `Delete failed with status code ${status}`;
|
||||
|
||||
throw body.english ?? body.error ?? `Delete failed with status code ${status}`;
|
||||
}
|
||||
},
|
||||
};
|
||||
};
|
||||
|
|
|
@ -20,10 +20,7 @@ const MszForumEditor = function(form) {
|
|||
let lastPostText = '',
|
||||
lastPostParser;
|
||||
|
||||
MszEEPROM.init()
|
||||
.catch(() => console.error('Failed to initialise EEPROM'))
|
||||
.then(() => {
|
||||
const eepromClient = new EEPROM(peepApp, peepPath);
|
||||
const eepromClient = new MszEEPROM(peepApp, peepPath);
|
||||
const eepromHistory = <div class="eeprom-widget-history-items"/>;
|
||||
|
||||
const eepromHandleFileUpload = async file => {
|
||||
|
@ -163,7 +160,6 @@ const MszForumEditor = function(form) {
|
|||
eepromHandleFileUpload(file);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// hack: don't prompt user when hitting submit, really need to make this not stupid.
|
||||
buttonsElem.firstChild.addEventListener('click', () => MszForumEditorAllowClose = true);
|
||||
|
|
|
@ -64,10 +64,7 @@ const MszMessagesReply = function(element) {
|
|||
|
||||
// this implementation is godawful but it'll do for now lol
|
||||
// need to make it easier to share the forum's implementation
|
||||
MszEEPROM.init()
|
||||
.catch(() => console.error('Failed to initialise EEPROM'))
|
||||
.then(() => {
|
||||
const eepromClient = new EEPROM(peepApp, peepPath);
|
||||
const eepromClient = new MszEEPROM(peepApp, peepPath);
|
||||
const eepromHandleFileUpload = async file => {
|
||||
const uploadTask = eepromClient.create(file);
|
||||
|
||||
|
@ -134,7 +131,6 @@ const MszMessagesReply = function(element) {
|
|||
eepromHandleFileUpload(file);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return {
|
||||
getElement: () => element,
|
||||
|
|
Reference in a new issue