eeprom/public/js/eeprom-v1.0.js

211 lines
6 KiB
JavaScript
Raw Normal View History

2022-07-05 19:07:24 +00:00
var EEPROM = function(srcId, endpoint, authorization) {
var obj = {
srcId: parseInt(srcId),
endpoint: endpoint,
authorization: authorization,
};
obj.setEndpoint = function(endpoint) {
obj.endpoint = (endpoint || '').toString();
};
obj.setAuthorization = function(authorization) {
obj.authorization = (authorization || '').toString();
};
obj.deleteUpload = function(fileInfo) {
return new EEPROM.EEPROMDeleteTask(
obj.authorization,
fileInfo
);
};
obj.createUpload = function(file) {
return new EEPROM.EEPROMUploadTask(
obj.srcId,
obj.endpoint,
obj.authorization,
file
);
};
return obj;
};
EEPROM.ERR_GENERIC = 'generic';
EEPROM.ERR_INVALID = 'invalid';
EEPROM.ERR_AUTH = 'auth';
EEPROM.ERR_ACCESS = 'access';
EEPROM.ERR_DMCA = 'dmca';
EEPROM.ERR_GONE = 'gone';
EEPROM.ERR_SERVER = 'server';
EEPROM.ERR_SIZE = 'size';
EEPROM.EEPROMFile = function(fileInfo) {
var obj = {
id: (fileInfo.id || '').toString(),
url: (fileInfo.url || '').toString(),
urlf: (fileInfo.urlf || '').toString(),
thumb: (fileInfo.thumb || '').toString(),
name: (fileInfo.name || '').toString(),
type: (fileInfo.type || '').toString(),
size: parseInt(fileInfo.size || 0),
user: parseInt(fileInfo.user || 0),
hash: (fileInfo.hash || '').toString(),
created: (fileInfo.created || null),
accessed: (fileInfo.accessed || null),
expires: (fileInfo.expires || null),
deleted: (fileInfo.deleted || null),
dmca: (fileInfo.dmca || null),
};
obj.isImage = function() { return obj.type.indexOf('image/') === 0; };
obj.isAudio = function() { return obj.type.indexOf('audio/') === 0; };
obj.isVideo = function() { return obj.type.indexOf('video/') === 0; };
return obj;
};
EEPROM.EEPROMDeleteTask = function(authorization, fileInfo) {
var obj = {
authorization: authorization,
fileInfo: fileInfo,
onSuccess: undefined,
onFailure: undefined,
};
var xhr = obj.xhr = new XMLHttpRequest;
obj.xhr.addEventListener('readystatechange', function() {
if(xhr.readyState !== 4)
return;
if(xhr.status !== 204) {
obj.errorCode = EEPROM.ERR_GENERIC;
switch(xhr.status) {
case 401:
obj.errorCode = EEPROM.ERR_AUTH;
break;
case 403:
obj.errorCode = EEPROM.ERR_ACCESS;
break;
case 404:
case 410:
obj.errorCode = EEPROM.ERR_GONE;
break;
case 500:
case 503:
obj.errorCode = EEPROM.ERR_SERVER;
break;
}
if(obj.onFailure)
obj.onFailure(obj.errorCode);
return;
}
if(obj.onSuccess)
obj.onSuccess();
});
obj.start = function() {
xhr.open('DELETE', obj.fileInfo.urlf);
xhr.setRequestHeader('Authorization', obj.authorization);
xhr.send();
};
return obj;
};
EEPROM.EEPROMUploadTask = function(srcId, endpoint, authorization, file) {
var obj = {
aborted: false,
endpoint: endpoint,
authorization: authorization,
onComplete: undefined,
onFailure: undefined,
onProgress: undefined,
failureResponse: undefined,
};
var xhr = obj.xhr = new XMLHttpRequest,
fd = obj.formData = new FormData;
fd.append('src', srcId);
fd.append('file', file);
var reportUploadProgress = function(ev) {
if(obj.onProgress)
obj.onProgress({
loaded: ev.loaded,
total: ev.total,
progress: Math.ceil((ev.loaded / ev.total) * 100),
});
};
xhr.upload.addEventListener('loadstart', reportUploadProgress);
xhr.upload.addEventListener('progress', reportUploadProgress);
xhr.upload.addEventListener('load', reportUploadProgress);
xhr.addEventListener('readystatechange', function() {
if(this.readyState !== 4)
return;
if(this.status !== 201) {
obj.failureResponse = {
userAborted: obj.aborted,
error: EEPROM.ERR_GENERIC,
};
switch(this.status) {
case 400:
case 405:
obj.failureResponse.error = EEPROM.ERR_INVALID;
break;
case 401:
obj.failureResponse.error = EEPROM.ERR_AUTH;
break;
case 403:
obj.failureResponse.error = EEPROM.ERR_ACCESS;
break;
case 404:
case 410:
obj.failureResponse.error = EEPROM.ERR_GONE;
break;
case 451:
obj.failureResponse.error = EEPROM.ERR_DMCA;
break;
case 413:
obj.failureResponse.error = EEPROM.ERR_SIZE;
obj.failureResponse.maxSize = parseInt(this.getResponseHeader('X-EEPROM-Max-Size'));
break;
case 500:
case 503:
obj.failureResponse.error = EEPROM.ERR_SERVER;
break;
}
if(obj.onFailure)
obj.onFailure(obj.failureResponse);
return;
}
obj.fileInfo = new EEPROM.EEPROMFile(JSON.parse(this.responseText));
if(obj.onComplete)
obj.onComplete(obj.fileInfo);
});
obj.abort = function() {
obj.aborted = true;
xhr.abort();
};
obj.start = function() {
xhr.open('POST', obj.endpoint);
xhr.setRequestHeader('Authorization', obj.authorization);
xhr.send(fd);
};
return obj;
};