workspace-ionic-app/src/app/model/actions/upload-file.ts

75 lines
2.9 KiB
TypeScript
Raw Normal View History

2023-03-16 16:15:10 +01:00
import { LoadingController, ModalOptions } from "@ionic/angular";
2023-03-15 09:26:11 +01:00
import { StoragehubService } from "src/app/storagehub.service";
import { WsViewerComponent } from "src/app/ws-viewer/ws-viewer.component";
import { WSItem } from "../ws-item";
import { Filesystem } from "@capacitor/filesystem";
import { UploaderInfoService } from "src/app/uploader-info.service";
export class UploadFile {
2023-03-16 16:15:10 +01:00
getModalOptions(storagehub: StoragehubService, uploaderInfo: UploaderInfoService, loading: LoadingController, data: any, reload: Function, notify: Function, onSelectionFinished: Function): ModalOptions {
2023-03-15 09:26:11 +01:00
return {
component: WsViewerComponent,
componentProps: {
finishLabel: "Upload here",
title: "Select destination folder",
notClickableIds: [],
notSelectableIds: [],
onSelected: (destinationItem: WSItem) => {
const itemId = destinationItem.item.id;
2023-03-15 17:16:06 +01:00
2023-03-16 16:15:10 +01:00
this.showLoading(loading);
2023-03-15 09:26:11 +01:00
uploaderInfo.uploadStarted(itemId, data.title);
this.actionHandler(destinationItem, data, storagehub).then( obs => obs.subscribe({
next: () => {},
2023-03-15 17:16:06 +01:00
error: err => {
uploaderInfo.uploadFinished(itemId, data.title);
notify(`error uploading file ${data.title} to workspace`);
},
2023-03-15 09:26:11 +01:00
complete: () => {
notify(`uploaded file ${data.title}`);
uploaderInfo.uploadFinished(itemId, data.title),
2023-03-16 16:15:10 +01:00
loading.dismiss();
2023-03-15 09:26:11 +01:00
reload(itemId);
2023-03-15 17:16:06 +01:00
onSelectionFinished();
2023-03-15 09:26:11 +01:00
},
}
));
}
}
};
}
2023-03-16 16:15:10 +01:00
async showLoading(loadingCtrl: LoadingController) {
const loading = await loadingCtrl.create({
message: 'Uploading...',
duration: 100000,
spinner: 'circles',
});
loading.present();
return loading;
}
2023-03-15 09:26:11 +01:00
async actionHandler(destinationItem: WSItem, result: any, storagehub: StoragehubService) {
const pathDecodedWebPath = decodeURIComponent(result.webPath);
const file = await fetch(pathDecodedWebPath)
.then(res => res.blob()) // Gets the response and returns it as a blob
.then(blob => { return new File([blob], result.title)});
console.log("before uploading file ");
return storagehub.uploadFile(destinationItem.item.id, result.title, file);
}
getName(): string {
return "UploadFile";
}
getActionType(): string | undefined {
return undefined;
}
}