Rewrote EEPROM handling.

This commit is contained in:
flash 2024-02-02 21:02:29 +00:00
parent 1c813ad97c
commit 07b4906034
6 changed files with 74 additions and 104 deletions

View file

@ -1,3 +1,5 @@
#include common.js
#include mszauth.js
#include txtrigs.js #include txtrigs.js
#include audio/context.js #include audio/context.js
#include controls/views.js #include controls/views.js
@ -146,12 +148,18 @@ const MamiContext = function(targetBody) {
const txtTriggers = new MamiTextTriggers; const txtTriggers = new MamiTextTriggers;
pub.getTextTriggers = function() { return txtTriggers; }; pub.getTextTriggers = function() { return txtTriggers; };
let eeprom = null; let eeprom;
pub.hasEEPROM = function() { return eeprom !== null }; pub.hasEEPROM = () => eeprom !== undefined;
pub.getEEPROM = function() { return eeprom; }; pub.getEEPROM = () => eeprom;
pub.createEEPROM = function(url, getToken) { pub.createEEPROM = function() {
// new EEPROM api should take a callback to get auth info instead of a string if(eeprom !== undefined)
eeprom = new EEPROM(1, url, getToken()); return;
eeprom = new EEPROM(
'1',
futami.get('eeprom2'),
MamiMisuzuAuth.getLine
);
}; };
return pub; return pub;

View file

@ -11,15 +11,12 @@ MamiEEPROM.init = (function() {
if(initialised) if(initialised)
return new Promise(resolve => resolve()); return new Promise(resolve => resolve());
// cuts off "/uploads", this is little disgusting
const src = futami.get('eeprom').slice(0, -8) + '/eeprom.js';
const script = $e({ const script = $e({
tag: 'script', tag: 'script',
attrs: { attrs: {
charset: 'utf-8', charset: 'utf-8',
type: 'text/javascript', type: 'text/javascript',
src: src, src: `${futami.get('eeprom2')}/scripts/eepromv1a.js`,
onload: () => { onload: () => {
initialised = true; initialised = true;
resolve(); resolve();

View file

@ -320,10 +320,7 @@ const Umi = { UI: {} };
lo.setMessage('Loading EEPROM...'); lo.setMessage('Loading EEPROM...');
try { try {
await MamiEEPROM.init(); await MamiEEPROM.init();
ctx.createEEPROM( ctx.createEEPROM();
futami.get('eeprom'),
function() { return 'Misuzu ' + MamiMisuzuAuth.getAuthToken(); }
);
} catch(ex) { } catch(ex) {
console.error(ex); console.error(ex);
} }
@ -451,93 +448,55 @@ const Umi = { UI: {} };
if(ctx.hasEEPROM()) { if(ctx.hasEEPROM()) {
Umi.UI.Menus.Add('uploads', 'Upload History', !FUTAMI_DEBUG); Umi.UI.Menus.Add('uploads', 'Upload History', !FUTAMI_DEBUG);
const doUpload = function(file) { const doUpload = async file => {
const uploadEntry = Umi.UI.Uploads.create(file.name), const uploadEntry = Umi.UI.Uploads.create(file.name);
uploadTask = ctx.getEEPROM().createUpload(file); const uploadTask = ctx.getEEPROM().create(file);
uploadTask.onProgress = function(progressInfo) { uploadTask.onProgress(prog => uploadEntry.setProgress(prog.progress));
uploadEntry.setProgress(progressInfo.total, progressInfo.loaded); uploadEntry.addOption('Cancel', () => uploadTask.abort());
};
uploadTask.onFailure = function(errorInfo) { try {
if(!errorInfo.userAborted) { const fileInfo = await uploadTask.start();
let errorText = 'Was unable to upload file.';
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 _t = ['bytes', 'KB', 'MB', 'GB', 'TB'],
_i = parseInt(Math.floor(Math.log(errorInfo.maxSize) / Math.log(1024))),
_s = Math.round(errorInfo.maxSize / Math.pow(1024, _i), 2);
errorText = 'Upload may not be larger than %1 %2.'.replace('%1', _s).replace('%2', _t[_i]);
}
break;
}
alert(errorText);
}
uploadEntry.remove();
};
uploadTask.onComplete = function(fileInfo) {
uploadEntry.hideOptions(); uploadEntry.hideOptions();
uploadEntry.clearOptions(); uploadEntry.clearOptions();
uploadEntry.removeProgress(); uploadEntry.removeProgress();
uploadEntry.addOption('Open', fileInfo.url); uploadEntry.addOption('Open', fileInfo.url);
uploadEntry.addOption('Insert', function() { uploadEntry.addOption('Insert', () => Umi.UI.Markup.InsertRaw(insertText, ''));
Umi.UI.Markup.InsertRaw(insertText, ''); uploadEntry.addOption('Delete', () => {
}); ctx.getEEPROM().delete(fileInfo)
uploadEntry.addOption('Delete', function() { .then(() => uploadEntry.remove())
ctx.getEEPROM().deleteUpload(fileInfo).start(); .catch(ex => {
uploadEntry.remove(); console.error(ex);
alert(ex);
});
}); });
let insertText = location.protocol + fileInfo.url; let insertText;
if(fileInfo.isImage()) { if(fileInfo.isImage()) {
insertText = '[img]' + fileInfo.url + '[/img]'; insertText = `[img]${fileInfo.url}[/img]`;
uploadEntry.setThumbnail(fileInfo.thumb); uploadEntry.setThumbnail(fileInfo.thumb);
} else if(fileInfo.isAudio() || fileInfo.type === 'application/x-font-gdos') { } else if(fileInfo.isAudio()) {
insertText = '[audio]' + fileInfo.url + '[/audio]'; insertText = `[audio]${fileInfo.url}[/audio]`;
uploadEntry.setThumbnail(fileInfo.thumb); uploadEntry.setThumbnail(fileInfo.thumb);
} else if(fileInfo.isVideo()) { } else if(fileInfo.isVideo()) {
insertText = '[video]' + fileInfo.url + '[/video]'; insertText = `[video]${fileInfo.url}[/video]`;
uploadEntry.setThumbnail(fileInfo.thumb); uploadEntry.setThumbnail(fileInfo.thumb);
} } else
insertText = location.protocol + fileInfo.url;
if(settings.get('eepromAutoInsert')) if(settings.get('eepromAutoInsert'))
Umi.UI.Markup.InsertRaw(insertText, ''); Umi.UI.Markup.InsertRaw(insertText, '');
}; } catch(ex) {
if(!ex.aborted) {
console.error(ex);
alert(ex);
}
uploadEntry.addOption('Cancel', function() { uploadEntry.remove();
uploadTask.abort(); }
});
uploadTask.start();
}; };
const uploadForm = $e({ const uploadForm = $e({
@ -545,42 +504,39 @@ const Umi = { UI: {} };
attrs: { attrs: {
type: 'file', type: 'file',
multiple: true, multiple: true,
style: { style: { display: 'none' },
display: 'none', onchange: ev => {
}, for(const file of ev.target.files)
onchange: function(ev) { doUpload(file);
for(let i = 0; i < ev.target.files.length; ++i)
doUpload(ev.target.files[i]);
}, },
}, },
}); });
document.body.appendChild(uploadForm); document.body.appendChild(uploadForm);
Umi.UI.InputMenus.AddButton('upload', 'Upload', function() { Umi.UI.InputMenus.AddButton('upload', 'Upload', () => uploadForm.click());
uploadForm.click();
});
Umi.UI.Elements.MessageInput.onpaste = function(ev) { Umi.UI.Elements.MessageInput.onpaste = ev => {
if(ev.clipboardData && ev.clipboardData.files.length > 0) if(ev.clipboardData && ev.clipboardData.files.length > 0)
for(const file of ev.clipboardData.files) for(const file of ev.clipboardData.files)
doUpload(file); doUpload(file);
}; };
document.body.ondragenter = function(ev) { document.body.ondragenter = ev => {
ev.preventDefault(); ev.preventDefault();
ev.stopPropagation(); ev.stopPropagation();
}; };
document.body.ondragover = function(ev) { document.body.ondragover = ev => {
ev.preventDefault(); ev.preventDefault();
ev.stopPropagation(); ev.stopPropagation();
}; };
document.body.ondragleave = function(ev) { document.body.ondragleave = ev => {
ev.preventDefault(); ev.preventDefault();
ev.stopPropagation(); ev.stopPropagation();
}; };
document.body.ondrop = function(ev) { document.body.ondrop = ev => {
ev.preventDefault(); ev.preventDefault();
ev.stopPropagation(); ev.stopPropagation();
if(ev.dataTransfer && ev.dataTransfer.files.length > 0) if(ev.dataTransfer && ev.dataTransfer.files.length > 0)
for(const file of ev.dataTransfer.files) { for(const file of ev.dataTransfer.files) {
if(file.name.slice(-5) === '.mami' if(file.name.slice(-5) === '.mami'

View file

@ -1,16 +1,18 @@
#include common.js #include common.js
#include utility.js #include utility.js
const MamiMisuzuAuth = (function() { const MamiMisuzuAuth = (() => {
let userId = null, let userId = null;
authToken = null; let authMethod = 'Misuzu';
let authToken = null;
return { return {
getUserId: function() { return userId; }, getUserId: () => userId,
getAuthToken: function() { return authToken; }, getAuthToken: () => authToken,
getInfo: function() { getLine: () => `${authMethod} ${authToken}`,
getInfo: () => {
return { return {
method: 'Misuzu', method: authMethod,
token: authToken, token: authToken,
}; };
}, },

View file

@ -126,8 +126,8 @@ Umi.UI.Uploads = (function() {
removeProgress: function() { removeProgress: function() {
$r(prog); $r(prog);
}, },
setProgress: function(total, loaded) { setProgress: function(progress) {
prog.value = Math.ceil((loaded / total) * 100); prog.value = Math.ceil(progress * 100);
}, },
}; };
}, },

View file

@ -248,7 +248,14 @@ const $x = (function() {
ev: ev, ev: ev,
}); });
xhr.onabort = ev => reject({
abort: true,
xhr: xhr,
ev: ev,
});
xhr.onerror = ev => reject({ xhr.onerror = ev => reject({
abort: false,
xhr: xhr, xhr: xhr,
ev: ev, ev: ev,
}); });