2022-02-16 19:41:13 +13:00
|
|
|
(function(window){
|
|
|
|
document.addEventListener('alpine:init', () => {
|
|
|
|
Alpine.store('uploader', {
|
2022-02-22 06:12:13 +13:00
|
|
|
_files: [],
|
|
|
|
files() {
|
|
|
|
return (this._files ?? []).filter((file) => !file.cancelled);
|
|
|
|
},
|
2022-02-16 19:41:13 +13:00
|
|
|
isOpen: true,
|
|
|
|
init() {
|
|
|
|
window.addEventListener('beforeunload', (event) => {
|
2022-02-28 23:10:06 +13:00
|
|
|
if(this.hasOngoingUploads()) {
|
|
|
|
let confirmationMessage = "There are incomplete uploads, are you sure you want to leave?";
|
|
|
|
event.returnValue = confirmationMessage;
|
|
|
|
return confirmationMessage;
|
|
|
|
}
|
2022-02-16 19:41:13 +13:00
|
|
|
});
|
|
|
|
},
|
2022-02-22 05:41:41 +13:00
|
|
|
cancelAll() {
|
2022-02-28 23:10:06 +13:00
|
|
|
if(this.hasOngoingUploads() ? confirm("Are you sure? This will cancel and remove any ongoing uploads?") : true){
|
2022-02-22 06:12:13 +13:00
|
|
|
this._files.forEach(file => {
|
2022-02-22 05:41:41 +13:00
|
|
|
if(file.completed || file.failed) {
|
|
|
|
this.removeFile(file.id);
|
|
|
|
} else {
|
|
|
|
this.updateFile(file.id, {cancelled: true});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2022-02-28 23:10:06 +13:00
|
|
|
hasOngoingUploads() {
|
|
|
|
let ongoing = false;
|
|
|
|
this._files.some((file) => {
|
|
|
|
if(!file.completed && !file.failed) {
|
|
|
|
ongoing = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return ongoing;
|
|
|
|
},
|
2022-02-16 19:41:13 +13:00
|
|
|
toggle() {
|
|
|
|
this.isOpen = !this.isOpen;
|
|
|
|
},
|
|
|
|
addFile(file) {
|
2022-02-22 06:12:13 +13:00
|
|
|
this._files.push(file);
|
2022-02-16 19:41:13 +13:00
|
|
|
},
|
|
|
|
updateFile(id, file) {
|
2022-02-22 06:12:13 +13:00
|
|
|
this._files = this._files.map((oldFile) => id == oldFile.id ? {...oldFile, ...file} : oldFile);
|
2022-02-16 19:41:13 +13:00
|
|
|
},
|
|
|
|
removeFile(id) {
|
2022-02-21 01:21:53 +13:00
|
|
|
const file = this.getFile(id) ?? {};
|
|
|
|
if(file.completed || file.failed) {
|
2022-02-22 06:12:13 +13:00
|
|
|
this._files = this._files.filter((file) => file.id !== id);
|
2022-02-21 01:21:53 +13:00
|
|
|
} else {
|
2022-02-21 20:26:40 +13:00
|
|
|
if(confirm("Are you sure you want to cancel the upload?")) {
|
|
|
|
this.updateFile(id, {cancelled: true});
|
|
|
|
}
|
2022-02-21 01:21:53 +13:00
|
|
|
}
|
|
|
|
},
|
|
|
|
getFile(id) {
|
2022-02-22 06:12:13 +13:00
|
|
|
return this._files.find((file) => file.id === id);
|
2022-02-16 19:41:13 +13:00
|
|
|
},
|
|
|
|
async uploadFile(target) {
|
|
|
|
const formData = new FormData(target);
|
|
|
|
const sdk = window.ls.container.get('sdk');
|
2022-02-22 01:21:30 +13:00
|
|
|
const bucketId = formData.get('bucketId');
|
2022-02-16 19:41:13 +13:00
|
|
|
const file = formData.get('file');
|
|
|
|
const fileId = formData.get('fileId');
|
2022-02-21 01:21:53 +13:00
|
|
|
let id = fileId === 'unique()' ? performance.now() : fileId;
|
2022-02-21 21:12:30 +13:00
|
|
|
if(!file || !fileId) {
|
2022-02-21 20:26:40 +13:00
|
|
|
return;
|
|
|
|
}
|
2022-08-04 16:47:24 +12:00
|
|
|
let permissions = formData.get('permissions');
|
|
|
|
if(permissions) {
|
2022-08-13 21:07:17 +12:00
|
|
|
permissions = permissions.split(',');
|
2022-02-16 19:41:13 +13:00
|
|
|
}
|
2022-02-11 13:11:09 +13:00
|
|
|
|
2022-02-21 20:26:40 +13:00
|
|
|
if(this.getFile(id)) {
|
|
|
|
this.updateFile(id, {
|
|
|
|
name: file.name,
|
|
|
|
completed: false,
|
|
|
|
failed: false,
|
|
|
|
cancelled: false,
|
2022-02-22 20:37:12 +13:00
|
|
|
error: "",
|
2022-02-21 20:26:40 +13:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.addFile({
|
|
|
|
id: id,
|
|
|
|
name: file.name,
|
|
|
|
progress: 0,
|
|
|
|
completed: false,
|
|
|
|
failed: false,
|
|
|
|
cancelled: false,
|
2022-02-22 20:37:12 +13:00
|
|
|
error: "",
|
2022-02-21 20:26:40 +13:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-16 19:41:13 +13:00
|
|
|
target.reset();
|
|
|
|
try {
|
|
|
|
const response = await sdk.storage.createFile(
|
2022-02-22 01:21:30 +13:00
|
|
|
bucketId,
|
2022-02-16 19:41:13 +13:00
|
|
|
fileId,
|
|
|
|
file,
|
2022-08-04 16:47:24 +12:00
|
|
|
permissions,
|
2022-02-16 19:41:13 +13:00
|
|
|
(progress) => {
|
|
|
|
this.updateFile(id, {
|
2022-02-20 02:01:52 +13:00
|
|
|
id: progress.$id,
|
|
|
|
progress: Math.round(progress.progress),
|
2022-02-22 20:37:12 +13:00
|
|
|
error: "",
|
2022-02-16 19:41:13 +13:00
|
|
|
});
|
2022-02-21 01:21:53 +13:00
|
|
|
id = progress.$id;
|
|
|
|
|
|
|
|
const file = this.getFile(id) ?? {};
|
|
|
|
if(file.cancelled === true) {
|
2022-02-22 01:21:30 +13:00
|
|
|
throw 'USER_CANCELLED';
|
2022-02-21 01:21:53 +13:00
|
|
|
}
|
2022-02-16 19:41:13 +13:00
|
|
|
});
|
2022-02-22 01:21:30 +13:00
|
|
|
const existingFile = this.getFile(id) ?? {};
|
|
|
|
if(existingFile.cancelled) {
|
|
|
|
this.updateFile(id,{
|
|
|
|
id: response.$id,
|
|
|
|
name: response.name,
|
|
|
|
failed: false,
|
|
|
|
});
|
|
|
|
id = response.$id;
|
|
|
|
throw 'USER_CANCELLED'
|
|
|
|
} else {
|
|
|
|
this.updateFile(id,{
|
|
|
|
id: response.$id,
|
|
|
|
name: response.name,
|
|
|
|
progress: 100,
|
|
|
|
completed: true,
|
|
|
|
failed: false,
|
|
|
|
});
|
|
|
|
id = response.$id;
|
|
|
|
}
|
2022-02-16 19:41:13 +13:00
|
|
|
document.dispatchEvent(new CustomEvent('storage.createFile'));
|
|
|
|
} catch(error) {
|
2022-02-22 01:21:30 +13:00
|
|
|
if(error === 'USER_CANCELLED') {
|
|
|
|
await sdk.storage.deleteFile(bucketId, id);
|
|
|
|
this.updateFile(id, {
|
|
|
|
cancelled: false,
|
|
|
|
failed: true,
|
|
|
|
});
|
|
|
|
this.removeFile(id);
|
|
|
|
} else {
|
|
|
|
this.updateFile(id, {
|
|
|
|
id: id,
|
|
|
|
failed: true,
|
2022-02-22 20:37:12 +13:00
|
|
|
error: error.message ?? error
|
2022-02-22 01:21:30 +13:00
|
|
|
});
|
|
|
|
}
|
2022-02-16 19:41:13 +13:00
|
|
|
document.dispatchEvent(new CustomEvent('storage.createFile'));
|
|
|
|
}
|
|
|
|
}
|
2022-02-11 13:11:09 +13:00
|
|
|
|
2022-02-16 19:41:13 +13:00
|
|
|
});
|
|
|
|
});
|
2022-03-10 11:49:51 +13:00
|
|
|
})(window);
|