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";
|
2022-10-14 18:25:50 +02:00
|
|
|
import { EncArrayBuffer } from "../models/domain/enc-array-buffer";
|
|
|
|
import { EncString } from "../models/domain/enc-string";
|
2022-10-18 19:01:42 +02:00
|
|
|
import { AttachmentUploadDataResponse } from "../models/response/attachment-upload-data.response";
|
|
|
|
import { SendFileUploadDataResponse } from "../models/response/send-file-upload-data.response";
|
2021-03-25 16:20:38 +01:00
|
|
|
|
|
|
|
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);
|
2022-08-25 19:09:27 +02:00
|
|
|
this.bitwardenFileUploadService = new BitwardenFileUploadService();
|
2021-03-25 16:20:38 +01:00
|
|
|
}
|
|
|
|
|
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-12-16 13:36:21 +01:00
|
|
|
)
|
2021-03-26 22:57:07 +01:00
|
|
|
);
|
2021-03-25 16:20:38 +01:00
|
|
|
break;
|
2022-02-22 15:39:11 +01:00
|
|
|
case FileUploadType.Azure: {
|
2021-03-25 16:20:38 +01:00
|
|
|
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;
|
2022-02-22 15:39:11 +01:00
|
|
|
}
|
2021-03-25 16:20:38 +01:00
|
|
|
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-12-16 13:36:21 +01:00
|
|
|
}
|
2021-03-26 22:57:07 +01:00
|
|
|
|
2021-06-08 21:02:08 +02:00
|
|
|
async uploadCipherAttachment(
|
|
|
|
admin: boolean,
|
|
|
|
uploadData: AttachmentUploadDataResponse,
|
|
|
|
encryptedFileName: EncString,
|
|
|
|
encryptedFileData: EncArrayBuffer
|
|
|
|
) {
|
2021-03-26 22:57:07 +01:00
|
|
|
const response = admin ? uploadData.cipherMiniResponse : uploadData.cipherResponse;
|
|
|
|
try {
|
|
|
|
switch (uploadData.fileUploadType) {
|
|
|
|
case FileUploadType.Direct:
|
2021-06-08 21:02:08 +02:00
|
|
|
await this.bitwardenFileUploadService.upload(
|
|
|
|
encryptedFileName.encryptedString,
|
|
|
|
encryptedFileData,
|
2021-03-26 22:57:07 +01:00
|
|
|
(fd) => this.apiService.postAttachmentFile(response.id, uploadData.attachmentId, fd)
|
|
|
|
);
|
|
|
|
break;
|
2022-02-22 15:39:11 +01:00
|
|
|
case FileUploadType.Azure: {
|
2021-03-26 22:57:07 +01:00
|
|
|
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;
|
2022-02-22 15:39:11 +01:00
|
|
|
}
|
2021-03-26 22:57:07 +01:00
|
|
|
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-12-16 13:36:21 +01:00
|
|
|
}
|
2021-03-25 16:20:38 +01:00
|
|
|
}
|