2021-03-25 16:20:38 +01:00
|
|
|
import { ApiService } from '../abstractions/api.service';
|
|
|
|
import { FileUploadService as FileUploadServiceAbstraction } from '../abstractions/fileUpload.service';
|
|
|
|
import { LogService } from '../abstractions/log.service';
|
|
|
|
|
|
|
|
import { FileUploadType } from '../enums/fileUploadType';
|
|
|
|
|
2021-04-21 02:16:19 +02:00
|
|
|
import { EncArrayBuffer } from '../models/domain/encArrayBuffer';
|
|
|
|
import { EncString } from '../models/domain/encString';
|
2021-04-14 17:47:10 +02:00
|
|
|
|
2021-03-26 22:57:07 +01:00
|
|
|
import { AttachmentUploadDataResponse } from '../models/response/attachmentUploadDataResponse';
|
2021-03-25 16:20:38 +01:00
|
|
|
import { SendFileUploadDataResponse } from '../models/response/sendFileUploadDataResponse';
|
|
|
|
|
|
|
|
import { AzureFileUploadService } from './azureFileUpload.service';
|
|
|
|
import { BitwardenFileUploadService } from './bitwardenFileUpload.service';
|
|
|
|
|
|
|
|
export class FileUploadService implements FileUploadServiceAbstraction {
|
|
|
|
private azureFileUploadService: AzureFileUploadService;
|
|
|
|
private bitwardenFileUploadService: BitwardenFileUploadService;
|
|
|
|
|
|
|
|
constructor(private logService: LogService, private apiService: ApiService) {
|
|
|
|
this.azureFileUploadService = new AzureFileUploadService(logService);
|
|
|
|
this.bitwardenFileUploadService = new BitwardenFileUploadService(apiService);
|
|
|
|
}
|
|
|
|
|
2021-04-21 02:16:19 +02:00
|
|
|
async uploadSendFile(uploadData: SendFileUploadDataResponse, fileName: EncString, encryptedFileData: EncArrayBuffer) {
|
2021-03-25 16:20:38 +01:00
|
|
|
try {
|
|
|
|
switch (uploadData.fileUploadType) {
|
|
|
|
case FileUploadType.Direct:
|
2021-03-26 22:57:07 +01:00
|
|
|
await this.bitwardenFileUploadService.upload(fileName.encryptedString, encryptedFileData,
|
|
|
|
fd => this.apiService.postSendFile(uploadData.sendResponse.id, uploadData.sendResponse.file.id, fd));
|
2021-03-25 16:20:38 +01:00
|
|
|
break;
|
|
|
|
case FileUploadType.Azure:
|
|
|
|
const renewalCallback = async () => {
|
2021-04-20 21:59:51 +02:00
|
|
|
const renewalResponse = await this.apiService.renewSendFileUploadUrl(uploadData.sendResponse.id,
|
2021-03-25 16:20:38 +01:00
|
|
|
uploadData.sendResponse.file.id);
|
|
|
|
return renewalResponse.url;
|
|
|
|
};
|
|
|
|
await this.azureFileUploadService.upload(uploadData.url, encryptedFileData,
|
|
|
|
renewalCallback);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error('Unknown file upload type');
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2021-03-29 15:18:07 +02:00
|
|
|
await this.apiService.deleteSend(uploadData.sendResponse.id);
|
2021-03-25 16:20:38 +01:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2021-03-26 22:57:07 +01:00
|
|
|
|
2021-04-21 02:16:19 +02:00
|
|
|
async uploadCipherAttachment(admin: boolean, uploadData: AttachmentUploadDataResponse, encryptedFileName: string, encryptedFileData: EncArrayBuffer) {
|
2021-03-26 22:57:07 +01:00
|
|
|
const response = admin ? uploadData.cipherMiniResponse : uploadData.cipherResponse;
|
|
|
|
try {
|
|
|
|
switch (uploadData.fileUploadType) {
|
|
|
|
case FileUploadType.Direct:
|
|
|
|
await this.bitwardenFileUploadService.upload(encryptedFileName, encryptedFileData,
|
|
|
|
fd => this.apiService.postAttachmentFile(response.id, uploadData.attachmentId, fd));
|
|
|
|
break;
|
|
|
|
case FileUploadType.Azure:
|
|
|
|
const renewalCallback = async () => {
|
|
|
|
const renewalResponse = await this.apiService.renewAttachmentUploadUrl(response.id,
|
|
|
|
uploadData.attachmentId);
|
|
|
|
return renewalResponse.url;
|
|
|
|
};
|
2021-03-29 15:18:07 +02:00
|
|
|
await this.azureFileUploadService.upload(uploadData.url, encryptedFileData, renewalCallback);
|
2021-03-26 22:57:07 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error('Unknown file upload type.');
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
if (admin) {
|
2021-03-29 15:18:07 +02:00
|
|
|
await this.apiService.deleteCipherAttachmentAdmin(response.id, uploadData.attachmentId);
|
2021-03-26 22:57:07 +01:00
|
|
|
} else {
|
2021-03-29 15:18:07 +02:00
|
|
|
await this.apiService.deleteCipherAttachment(response.id, uploadData.attachmentId);
|
2021-03-26 22:57:07 +01:00
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2021-03-25 16:20:38 +01:00
|
|
|
}
|