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 = (() => {
|
const MszEEPROM = function(appId, endPoint) {
|
||||||
let eepromScript;
|
if(typeof appId !== 'string')
|
||||||
|
throw 'appId must be a string';
|
||||||
|
if(typeof endPoint !== 'string')
|
||||||
|
throw 'endPoint must be a string';
|
||||||
|
|
||||||
return {
|
return {
|
||||||
init: () => {
|
create: fileInput => {
|
||||||
return new Promise((resolve, reject) => {
|
if(!(fileInput instanceof File))
|
||||||
if(eepromScript !== undefined) {
|
throw 'fileInput must be an instance of window.File';
|
||||||
resolve(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(typeof peepPath !== 'string') {
|
let userAborted = false;
|
||||||
reject();
|
let abortHandler;
|
||||||
return;
|
let progressHandler;
|
||||||
}
|
|
||||||
|
|
||||||
const scriptElem = $e({
|
const reportProgress = ev => {
|
||||||
tag: 'script',
|
if(progressHandler !== undefined)
|
||||||
attrs: {
|
progressHandler({
|
||||||
src: `${peepPath}/scripts/eepromv1a.js`,
|
loaded: ev.loaded,
|
||||||
charset: 'utf-8',
|
total: ev.total,
|
||||||
type: 'text/javascript',
|
progress: ev.total <= 0 ? 0 : ev.loaded / ev.total,
|
||||||
onerror: () => reject(),
|
|
||||||
onload: () => {
|
|
||||||
eepromScript = scriptElem;
|
|
||||||
resolve(true);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
|
||||||
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 = '',
|
let lastPostText = '',
|
||||||
lastPostParser;
|
lastPostParser;
|
||||||
|
|
||||||
MszEEPROM.init()
|
const eepromClient = new MszEEPROM(peepApp, peepPath);
|
||||||
.catch(() => console.error('Failed to initialise EEPROM'))
|
|
||||||
.then(() => {
|
|
||||||
const eepromClient = new EEPROM(peepApp, peepPath);
|
|
||||||
const eepromHistory = <div class="eeprom-widget-history-items"/>;
|
const eepromHistory = <div class="eeprom-widget-history-items"/>;
|
||||||
|
|
||||||
const eepromHandleFileUpload = async file => {
|
const eepromHandleFileUpload = async file => {
|
||||||
|
@ -163,7 +160,6 @@ const MszForumEditor = function(form) {
|
||||||
eepromHandleFileUpload(file);
|
eepromHandleFileUpload(file);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
// hack: don't prompt user when hitting submit, really need to make this not stupid.
|
// hack: don't prompt user when hitting submit, really need to make this not stupid.
|
||||||
buttonsElem.firstChild.addEventListener('click', () => MszForumEditorAllowClose = true);
|
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
|
// this implementation is godawful but it'll do for now lol
|
||||||
// need to make it easier to share the forum's implementation
|
// need to make it easier to share the forum's implementation
|
||||||
MszEEPROM.init()
|
const eepromClient = new MszEEPROM(peepApp, peepPath);
|
||||||
.catch(() => console.error('Failed to initialise EEPROM'))
|
|
||||||
.then(() => {
|
|
||||||
const eepromClient = new EEPROM(peepApp, peepPath);
|
|
||||||
const eepromHandleFileUpload = async file => {
|
const eepromHandleFileUpload = async file => {
|
||||||
const uploadTask = eepromClient.create(file);
|
const uploadTask = eepromClient.create(file);
|
||||||
|
|
||||||
|
@ -134,7 +131,6 @@ const MszMessagesReply = function(element) {
|
||||||
eepromHandleFileUpload(file);
|
eepromHandleFileUpload(file);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
getElement: () => element,
|
getElement: () => element,
|
||||||
|
|
Reference in a new issue