Replaced EEPROM implementation with a directly integrated one.
This commit is contained in:
parent
8f89e4603d
commit
f095828f64
3 changed files with 194 additions and 104 deletions
|
@ -1,33 +1,163 @@
|
||||||
var AmiEEPROM = function() {
|
var AmiEEPROM = function(endPoint, getAuthLine) {
|
||||||
//
|
if(typeof endPoint !== 'string')
|
||||||
};
|
throw 'endPoint must be a string';
|
||||||
AmiEEPROM.init = (function() {
|
if(typeof getAuthLine !== 'function')
|
||||||
var initialised = false;
|
throw 'getAuthLine must be a function';
|
||||||
|
|
||||||
return function(callback) {
|
|
||||||
if(initialised) {
|
|
||||||
if(callback)
|
|
||||||
callback(true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
initialised = true;
|
|
||||||
|
|
||||||
// cuts off "/uploads", this is little disgusting
|
// when the pools rewrite happen, retrieve this from futami common
|
||||||
var eepromScript = futami.get('eeprom').slice(0, -8) + '/eeprom.js';
|
const appId = '1';
|
||||||
|
|
||||||
var script = document.createElement('script');
|
return {
|
||||||
script.onload = function() {
|
create: fileInput => {
|
||||||
if(callback)
|
if(!(fileInput instanceof File))
|
||||||
callback(true);
|
throw 'fileInput must be an instance of window.File';
|
||||||
};
|
|
||||||
script.onerror = function() {
|
let userAborted = false;
|
||||||
console.error('Failed to load EEPROM script!');
|
let abortHandler;
|
||||||
if(callback)
|
let progressHandler;
|
||||||
callback(false);
|
|
||||||
};
|
const reportProgress = ev => {
|
||||||
script.charset = 'utf-8';
|
if(progressHandler !== undefined)
|
||||||
script.type = 'text/javascript';
|
progressHandler({
|
||||||
script.src = eepromScript;
|
loaded: ev.loaded,
|
||||||
document.body.appendChild(script);
|
total: ev.total,
|
||||||
|
progress: ev.total <= 0 ? 0 : ev.loaded / ev.total,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
abort: () => {
|
||||||
|
userAborted = true;
|
||||||
|
if(typeof abortHandler === 'function')
|
||||||
|
abortHandler();
|
||||||
|
},
|
||||||
|
onProgress: handler => {
|
||||||
|
if(typeof handler !== 'function')
|
||||||
|
throw 'handler must be a function';
|
||||||
|
progressHandler = handler;
|
||||||
|
},
|
||||||
|
start: (success, error) => {
|
||||||
|
const throwError = ex => {
|
||||||
|
if(typeof error === 'function')
|
||||||
|
error(ex);
|
||||||
|
else
|
||||||
|
console.error(ex);
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
if(typeof success !== 'function')
|
||||||
|
throw 'success must be a callback function';
|
||||||
|
|
||||||
|
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 xhr = new XMLHttpRequest;
|
||||||
|
abortHandler = () => { xhr.abort(); };
|
||||||
|
xhr.upload.onloadstart = reportProgress;
|
||||||
|
xhr.upload.onprogress = reportProgress;
|
||||||
|
xhr.upload.onloadend = reportProgress;
|
||||||
|
xhr.addEventListener('readystatechange', () => {
|
||||||
|
if(xhr.readyState !== XMLHttpRequest.DONE)
|
||||||
|
return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const body = (() => {
|
||||||
|
try {
|
||||||
|
return JSON.parse(xhr.responseText);
|
||||||
|
} catch(ex) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
|
// user cancel
|
||||||
|
if(xhr.status === 0)
|
||||||
|
throw '';
|
||||||
|
|
||||||
|
if(body === null)
|
||||||
|
throw "The upload server didn't return the metadata for some reason.";
|
||||||
|
|
||||||
|
if(xhr.status !== 201)
|
||||||
|
throw body.english ?? body.error ?? `Upload failed with status code ${xhr.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();
|
||||||
|
|
||||||
|
success(Object.freeze(body));
|
||||||
|
} catch(ex) {
|
||||||
|
throwError(ex);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
xhr.open('POST', `${endPoint}/uploads`);
|
||||||
|
xhr.setRequestHeader('Authorization', getAuthLine());
|
||||||
|
xhr.send(formData);
|
||||||
|
} catch(ex) {
|
||||||
|
if(userAborted)
|
||||||
|
throw '';
|
||||||
|
|
||||||
|
console.error(ex);
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
|
} catch(ex) {
|
||||||
|
throwError(ex);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
delete: (fileInfo, success, error) => {
|
||||||
|
const throwError = ex => {
|
||||||
|
if(typeof error === 'function')
|
||||||
|
error(ex);
|
||||||
|
else
|
||||||
|
console.error(ex);
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
if(typeof fileInfo !== 'object')
|
||||||
|
throw 'fileInfo must be an object';
|
||||||
|
if(typeof fileInfo.urlf !== 'string')
|
||||||
|
throw 'fileInfo.urlf must be a string';
|
||||||
|
|
||||||
|
const xhr = new XMLHttpRequest;
|
||||||
|
xhr.addEventListener('readystatechange', () => {
|
||||||
|
if(xhr.readyState !== XMLHttpRequest.DONE)
|
||||||
|
return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const body = (() => {
|
||||||
|
try {
|
||||||
|
return JSON.parse(xhr.responseText);
|
||||||
|
} catch(ex) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
|
if(xhr.status !== 204) {
|
||||||
|
if(body === null)
|
||||||
|
throw `Delete failed with status code ${xhr.status}`;
|
||||||
|
|
||||||
|
throw body.english ?? body.error ?? `Delete failed with status code ${xhr.status}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(typeof success === 'function')
|
||||||
|
success(body);
|
||||||
|
} catch(ex) {
|
||||||
|
throwError(ex);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
xhr.open('DELETE', fileInfo.urlf);
|
||||||
|
xhr.setRequestHeader('Authorization', getAuthLine());
|
||||||
|
xhr.send();
|
||||||
|
} catch(ex) {
|
||||||
|
throwError(ex);
|
||||||
|
}
|
||||||
|
},
|
||||||
};
|
};
|
||||||
})();
|
};
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
#include ts_20_ui.js
|
#include ts_20_ui.js
|
||||||
#include ts_10_user.js
|
#include ts_10_user.js
|
||||||
#include eeprom.js
|
|
||||||
#include z_eepromv1.js
|
#include z_eepromv1.js
|
||||||
#include utility.js
|
#include utility.js
|
||||||
#include sidebars/channelssb.js
|
#include sidebars/channelssb.js
|
||||||
|
@ -596,13 +595,7 @@ var Chat = (function () {
|
||||||
|
|
||||||
UI.RenderLanguage();
|
UI.RenderLanguage();
|
||||||
|
|
||||||
AmiEEPROM.init(function(success) {
|
eepromInitialise(auth);
|
||||||
if(!success)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// set up old eeprom code
|
|
||||||
eepromInitialise(auth);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
Chat.InsertBBCode = function (tag, arg) {
|
Chat.InsertBBCode = function (tag, arg) {
|
||||||
if (arg === void 0) { arg = null; }
|
if (arg === void 0) { arg = null; }
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#include common.js
|
#include common.js
|
||||||
|
#include eeprom.js
|
||||||
#include utility.js
|
#include utility.js
|
||||||
|
|
||||||
var eepromAppl = 1,
|
var eepromAppl = 1,
|
||||||
|
@ -7,7 +8,7 @@ var eepromAppl = 1,
|
||||||
eepromSupportVideoEmbed = true;
|
eepromSupportVideoEmbed = true;
|
||||||
var eepromInitialise = function(auth) {
|
var eepromInitialise = function(auth) {
|
||||||
window.eepromHistory = {};
|
window.eepromHistory = {};
|
||||||
window.eepromClient = new EEPROM(eepromAppl, futami.get('eeprom'), auth.getHttpAuth());
|
window.eepromClient = new AmiEEPROM(futami.get('eeprom2'), auth.getHttpAuth);
|
||||||
|
|
||||||
window.eepromUploadsTable = $i('uploadHistory');
|
window.eepromUploadsTable = $i('uploadHistory');
|
||||||
$i('uploadSelector').onchange = function() {
|
$i('uploadSelector').onchange = function() {
|
||||||
|
@ -86,53 +87,11 @@ var eepromFileUpload = function(file) {
|
||||||
prog.appendChild(progBar);
|
prog.appendChild(progBar);
|
||||||
uploadEntry.appendChild(prog);
|
uploadEntry.appendChild(prog);
|
||||||
|
|
||||||
var uploadTask = window.eepromClient.createUpload(file);
|
var uploadTask = window.eepromClient.create(file);
|
||||||
|
|
||||||
uploadTask.onProgress = function(progressInfo) {
|
uploadTask.onProgress(function(progressInfo) {
|
||||||
progBar.style.width = progressInfo.progress.toString() + '%';
|
progBar.style.width = `${progressInfo.progress * 100}%`;
|
||||||
};
|
});
|
||||||
|
|
||||||
uploadTask.onFailure = function(errorInfo) {
|
|
||||||
if(!errorInfo.userAborted) {
|
|
||||||
var 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 {
|
|
||||||
var _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);
|
|
||||||
}
|
|
||||||
|
|
||||||
uploadHistory.deleteRow(uploadEntryWrap.rowIndex);
|
|
||||||
};
|
|
||||||
|
|
||||||
name.onclick = function(ev) {
|
name.onclick = function(ev) {
|
||||||
if(ev.shiftKey) {
|
if(ev.shiftKey) {
|
||||||
|
@ -140,30 +99,38 @@ var eepromFileUpload = function(file) {
|
||||||
var fileInfo = window.eepromHistory[name.getAttribute('data-eeprom-id') || ''];
|
var fileInfo = window.eepromHistory[name.getAttribute('data-eeprom-id') || ''];
|
||||||
|
|
||||||
if(fileInfo) {
|
if(fileInfo) {
|
||||||
window.eepromClient.deleteUpload(fileInfo).start();
|
window.eepromClient.delete(
|
||||||
uploadHistory.deleteRow(uploadEntryWrap.rowIndex);
|
fileInfo,
|
||||||
|
() => { uploadHistory.deleteRow(uploadEntryWrap.rowIndex); },
|
||||||
|
window.alert
|
||||||
|
);
|
||||||
} else uploadTask.abort();
|
} else uploadTask.abort();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
uploadTask.onComplete = function(fileInfo) {
|
uploadTask.start(
|
||||||
window.eepromHistory[fileInfo.id] = fileInfo;
|
fileInfo => {
|
||||||
name.style.textDecoration = null;
|
window.eepromHistory[fileInfo.id] = fileInfo;
|
||||||
name.setAttribute('data-eeprom-id', fileInfo.id);
|
name.style.textDecoration = null;
|
||||||
name.href = fileInfo.url;
|
name.setAttribute('data-eeprom-id', fileInfo.id);
|
||||||
uploadEntry.removeChild(prog);
|
name.href = fileInfo.url;
|
||||||
|
uploadEntry.removeChild(prog);
|
||||||
|
|
||||||
var insertText = location.protocol + fileInfo.url;
|
var insertText = location.protocol + fileInfo.url;
|
||||||
|
|
||||||
if(eepromSupportImageEmbed && fileInfo.type.indexOf('image/') === 0)
|
if(eepromSupportImageEmbed && fileInfo.isImage())
|
||||||
insertText = '[img]' + fileInfo.url + '[/img]';
|
insertText = '[img]' + fileInfo.url + '[/img]';
|
||||||
else if(eepromSupportAudioEmbed && (fileInfo.type === 'application/x-font-gdos' || fileInfo.type.indexOf('audio/') === 0))
|
else if(eepromSupportAudioEmbed && fileInfo.isAudio())
|
||||||
insertText = '[audio]' + fileInfo.url + '[/audio]';
|
insertText = '[audio]' + fileInfo.url + '[/audio]';
|
||||||
else if(eepromSupportVideoEmbed && fileInfo.type.indexOf('video/') === 0)
|
else if(eepromSupportVideoEmbed && fileInfo.isVideo())
|
||||||
insertText = '[video]' + fileInfo.url + '[/video]';
|
insertText = '[video]' + fileInfo.url + '[/video]';
|
||||||
|
|
||||||
ami.chat.inputBox.insert(insertText);
|
ami.chat.inputBox.insert(insertText);
|
||||||
};
|
},
|
||||||
|
ex => {
|
||||||
uploadTask.start();
|
if(ex !== '')
|
||||||
|
alert(ex);
|
||||||
|
uploadHistory.deleteRow(uploadEntryWrap.rowIndex);
|
||||||
|
},
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue