diff --git a/src/mami.js/context.js b/src/mami.js/context.js index 0d9120d..d6e5520 100644 --- a/src/mami.js/context.js +++ b/src/mami.js/context.js @@ -1,3 +1,5 @@ +#include common.js +#include mszauth.js #include txtrigs.js #include audio/context.js #include controls/views.js @@ -146,12 +148,18 @@ const MamiContext = function(targetBody) { const txtTriggers = new MamiTextTriggers; pub.getTextTriggers = function() { return txtTriggers; }; - let eeprom = null; - pub.hasEEPROM = function() { return eeprom !== null }; - pub.getEEPROM = function() { return eeprom; }; - pub.createEEPROM = function(url, getToken) { - // new EEPROM api should take a callback to get auth info instead of a string - eeprom = new EEPROM(1, url, getToken()); + let eeprom; + pub.hasEEPROM = () => eeprom !== undefined; + pub.getEEPROM = () => eeprom; + pub.createEEPROM = function() { + if(eeprom !== undefined) + return; + + eeprom = new EEPROM( + '1', + futami.get('eeprom2'), + MamiMisuzuAuth.getLine + ); }; return pub; diff --git a/src/mami.js/eeprom/eeprom.js b/src/mami.js/eeprom/eeprom.js index 8b0b2d8..72d5356 100644 --- a/src/mami.js/eeprom/eeprom.js +++ b/src/mami.js/eeprom/eeprom.js @@ -11,15 +11,12 @@ MamiEEPROM.init = (function() { if(initialised) 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({ tag: 'script', attrs: { charset: 'utf-8', type: 'text/javascript', - src: src, + src: `${futami.get('eeprom2')}/scripts/eepromv1a.js`, onload: () => { initialised = true; resolve(); diff --git a/src/mami.js/main.js b/src/mami.js/main.js index 37e972b..63456fa 100644 --- a/src/mami.js/main.js +++ b/src/mami.js/main.js @@ -320,10 +320,7 @@ const Umi = { UI: {} }; lo.setMessage('Loading EEPROM...'); try { await MamiEEPROM.init(); - ctx.createEEPROM( - futami.get('eeprom'), - function() { return 'Misuzu ' + MamiMisuzuAuth.getAuthToken(); } - ); + ctx.createEEPROM(); } catch(ex) { console.error(ex); } @@ -451,93 +448,55 @@ const Umi = { UI: {} }; if(ctx.hasEEPROM()) { Umi.UI.Menus.Add('uploads', 'Upload History', !FUTAMI_DEBUG); - const doUpload = function(file) { - const uploadEntry = Umi.UI.Uploads.create(file.name), - uploadTask = ctx.getEEPROM().createUpload(file); + const doUpload = async file => { + const uploadEntry = Umi.UI.Uploads.create(file.name); + const uploadTask = ctx.getEEPROM().create(file); - uploadTask.onProgress = function(progressInfo) { - uploadEntry.setProgress(progressInfo.total, progressInfo.loaded); - }; + uploadTask.onProgress(prog => uploadEntry.setProgress(prog.progress)); + uploadEntry.addOption('Cancel', () => uploadTask.abort()); - 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 _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.clearOptions(); uploadEntry.removeProgress(); uploadEntry.addOption('Open', fileInfo.url); - uploadEntry.addOption('Insert', function() { - Umi.UI.Markup.InsertRaw(insertText, ''); - }); - uploadEntry.addOption('Delete', function() { - ctx.getEEPROM().deleteUpload(fileInfo).start(); - uploadEntry.remove(); + uploadEntry.addOption('Insert', () => Umi.UI.Markup.InsertRaw(insertText, '')); + uploadEntry.addOption('Delete', () => { + ctx.getEEPROM().delete(fileInfo) + .then(() => uploadEntry.remove()) + .catch(ex => { + console.error(ex); + alert(ex); + }); }); - let insertText = location.protocol + fileInfo.url; + let insertText; if(fileInfo.isImage()) { - insertText = '[img]' + fileInfo.url + '[/img]'; + insertText = `[img]${fileInfo.url}[/img]`; uploadEntry.setThumbnail(fileInfo.thumb); - } else if(fileInfo.isAudio() || fileInfo.type === 'application/x-font-gdos') { - insertText = '[audio]' + fileInfo.url + '[/audio]'; + } else if(fileInfo.isAudio()) { + insertText = `[audio]${fileInfo.url}[/audio]`; uploadEntry.setThumbnail(fileInfo.thumb); } else if(fileInfo.isVideo()) { - insertText = '[video]' + fileInfo.url + '[/video]'; + insertText = `[video]${fileInfo.url}[/video]`; uploadEntry.setThumbnail(fileInfo.thumb); - } - + } else + insertText = location.protocol + fileInfo.url; if(settings.get('eepromAutoInsert')) Umi.UI.Markup.InsertRaw(insertText, ''); - }; + } catch(ex) { + if(!ex.aborted) { + console.error(ex); + alert(ex); + } - uploadEntry.addOption('Cancel', function() { - uploadTask.abort(); - }); - - uploadTask.start(); + uploadEntry.remove(); + } }; const uploadForm = $e({ @@ -545,42 +504,39 @@ const Umi = { UI: {} }; attrs: { type: 'file', multiple: true, - style: { - display: 'none', - }, - onchange: function(ev) { - for(let i = 0; i < ev.target.files.length; ++i) - doUpload(ev.target.files[i]); + style: { display: 'none' }, + onchange: ev => { + for(const file of ev.target.files) + doUpload(file); }, }, }); document.body.appendChild(uploadForm); - Umi.UI.InputMenus.AddButton('upload', 'Upload', function() { - uploadForm.click(); - }); + Umi.UI.InputMenus.AddButton('upload', 'Upload', () => uploadForm.click()); - Umi.UI.Elements.MessageInput.onpaste = function(ev) { + Umi.UI.Elements.MessageInput.onpaste = ev => { if(ev.clipboardData && ev.clipboardData.files.length > 0) for(const file of ev.clipboardData.files) doUpload(file); }; - document.body.ondragenter = function(ev) { + document.body.ondragenter = ev => { ev.preventDefault(); ev.stopPropagation(); }; - document.body.ondragover = function(ev) { + document.body.ondragover = ev => { ev.preventDefault(); ev.stopPropagation(); }; - document.body.ondragleave = function(ev) { + document.body.ondragleave = ev => { ev.preventDefault(); ev.stopPropagation(); }; - document.body.ondrop = function(ev) { + document.body.ondrop = ev => { ev.preventDefault(); ev.stopPropagation(); + if(ev.dataTransfer && ev.dataTransfer.files.length > 0) for(const file of ev.dataTransfer.files) { if(file.name.slice(-5) === '.mami' diff --git a/src/mami.js/mszauth.js b/src/mami.js/mszauth.js index 9861231..e945dc5 100644 --- a/src/mami.js/mszauth.js +++ b/src/mami.js/mszauth.js @@ -1,16 +1,18 @@ #include common.js #include utility.js -const MamiMisuzuAuth = (function() { - let userId = null, - authToken = null; +const MamiMisuzuAuth = (() => { + let userId = null; + let authMethod = 'Misuzu'; + let authToken = null; return { - getUserId: function() { return userId; }, - getAuthToken: function() { return authToken; }, - getInfo: function() { + getUserId: () => userId, + getAuthToken: () => authToken, + getLine: () => `${authMethod} ${authToken}`, + getInfo: () => { return { - method: 'Misuzu', + method: authMethod, token: authToken, }; }, diff --git a/src/mami.js/ui/uploads.js b/src/mami.js/ui/uploads.js index 2c79f7e..f488715 100644 --- a/src/mami.js/ui/uploads.js +++ b/src/mami.js/ui/uploads.js @@ -126,8 +126,8 @@ Umi.UI.Uploads = (function() { removeProgress: function() { $r(prog); }, - setProgress: function(total, loaded) { - prog.value = Math.ceil((loaded / total) * 100); + setProgress: function(progress) { + prog.value = Math.ceil(progress * 100); }, }; }, diff --git a/src/mami.js/utility.js b/src/mami.js/utility.js index 393581f..0f2142d 100644 --- a/src/mami.js/utility.js +++ b/src/mami.js/utility.js @@ -248,7 +248,14 @@ const $x = (function() { ev: ev, }); + xhr.onabort = ev => reject({ + abort: true, + xhr: xhr, + ev: ev, + }); + xhr.onerror = ev => reject({ + abort: false, xhr: xhr, ev: ev, });