I'm using https://www.npmjs.com/package/@sleiss/ngx-awesome-uploader and I cannot find a way to rename the file before uploading.
I've checked the methods, and there seems to be none.
I can rename the file in the adapter, and it uploads with the new name, but on the file list it's still with the old name. So when I try to delete it, it looks for the file with the old name.
Is there a way to rename the file and make it available to the list of files?
Thank you!
I think I got it. Given this adapter...
import { AngularFireStorage } from "@angular/fire/storage";
import { FilePickerAdapter, FilePreviewModel, UploadResponse, UploadStatus } from "ngx-awesome-uploader";
import { from, Observable, of } from "rxjs";
import { catchError, map } from "rxjs/operators";
import { Uuid } from "../externalLibraries/uuid/uuid";
export class FirebaseStorageFilePickerAdapter extends FilePickerAdapter {
constructor(
private firebaseStorage: AngularFireStorage
) {
super();
}
uploadFile(fileItem: FilePreviewModel): Observable<UploadResponse> {
const file = fileItem.file;
const path = `public/${Uuid.generateUUID()}.${fileItem.fileName.split('.')[1]}`;
const ref = this.firebaseStorage.ref(path);
return from(this.firebaseStorage.upload(path, file, {
contentType: fileItem.file.type
})).pipe(
map((response) => {
let returnedResponse: UploadResponse;
switch (response.state.toLowerCase()) {
case 'canceled':
case 'error':
case 'paused':
returnedResponse = {
status: UploadStatus.ERROR,
};
break;
case 'success':
returnedResponse = {
status: UploadStatus.UPLOADED,
body: response.metadata,
progress: 100
}
break;
default:
returnedResponse = {
status: UploadStatus.IN_PROGRESS,
body: response.metadata,
progress: (response.bytesTransferred / response.totalBytes) * 100
}
break;
}
return returnedResponse;
}),
catchError((err, caught) => {
console.error(err)
return of({ status: UploadStatus.ERROR })
})
);
}
removeFile(fileItem: FilePreviewModel): Observable<any> {
return this.firebaseStorage.ref(fileItem.uploadResponse.fullPath).delete();
}
}
My problem was to use public/${fileItem.filename} when trying to delete the file and it didn't exist. However, the new name comes in the uploadResponse object so now I get the fullPath from fileItem.uploadResponse.fullPath