ami/src/ami.js/eeprom.js

163 lines
6.4 KiB
JavaScript

var AmiEEPROM = function(endPoint, getAuthLine) {
if(typeof endPoint !== 'string')
throw 'endPoint must be a string';
if(typeof getAuthLine !== 'function')
throw 'getAuthLine must be a function';
// when the pools rewrite happen, retrieve this from futami common
const appId = '1';
return {
create: fileInput => {
if(!(fileInput instanceof File))
throw 'fileInput must be an instance of window.File';
let userAborted = false;
let abortHandler;
let progressHandler;
const reportProgress = ev => {
if(progressHandler !== undefined)
progressHandler({
loaded: ev.loaded,
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.indexOf('image/') === 0;
body.isVideo = () => body.type.indexOf('video/') === 0;
body.isAudio = () => body.type.indexOf('audio/') === 0;
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);
}
},
};
};