1
0
Fork 0
mirror of synced 2024-07-04 06:00:53 +12:00
appwrite/public/scripts/upload-modal.js

158 lines
6.1 KiB
JavaScript
Raw Normal View History

(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);
},
isOpen: true,
init() {
window.addEventListener('beforeunload', (event) => {
if(this.hasOngoingUploads()) {
let confirmationMessage = "There are incomplete uploads, are you sure you want to leave?";
event.returnValue = confirmationMessage;
return confirmationMessage;
}
});
},
2022-02-22 05:41:41 +13:00
cancelAll() {
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});
}
});
}
},
hasOngoingUploads() {
let ongoing = false;
this._files.some((file) => {
if(!file.completed && !file.failed) {
ongoing = true;
return;
}
});
return ongoing;
},
toggle() {
this.isOpen = !this.isOpen;
},
addFile(file) {
2022-02-22 06:12:13 +13:00
this._files.push(file);
},
updateFile(id, file) {
2022-02-22 06:12:13 +13:00
this._files = this._files.map((oldFile) => id == oldFile.id ? {...oldFile, ...file} : oldFile);
},
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);
},
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');
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-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
});
}
target.reset();
try {
const response = await sdk.storage.createFile(
2022-02-22 01:21:30 +13:00
bucketId,
fileId,
file,
2022-08-04 16:47:24 +12:00
permissions,
(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-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-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;
}
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
});
}
document.dispatchEvent(new CustomEvent('storage.createFile'));
}
}
2022-02-11 13:11:09 +13:00
});
});
2022-03-10 11:49:51 +13:00
})(window);