mirror of
https://github.com/bitwarden/browser
synced 2025-12-16 16:23:44 +00:00
* Use typescript-strict-plugin to iteratively turn on strict * Add strict testing to pipeline Can be executed locally through either `npm run test:types` for full type checking including spec files, or `npx tsc-strict` for only tsconfig.json included files. * turn on strict for scripts directory * Use plugin for all tsconfigs in monorepo vscode is capable of executing tsc with plugins, but uses the most relevant tsconfig to do so. If the plugin is not a part of that config, it is skipped and developers get no feedback of strict compile time issues. These updates remedy that at the cost of slightly more complex removal of the plugin when the time comes. * remove plugin from configs that extend one that already has it * Update workspace settings to honor strict plugin * Apply strict-plugin to native message test runner * Update vscode workspace to use root tsc version * `./node_modules/.bin/update-strict-comments` 🤖 This is a one-time operation. All future files should adhere to strict type checking. * Add fixme to `ts-strict-ignore` comments * `update-strict-comments` 🤖 repeated for new merge files
163 lines
5.4 KiB
TypeScript
163 lines
5.4 KiB
TypeScript
// FIXME: Update this file to be type safe and remove this and next line
|
|
// @ts-strict-ignore
|
|
import { ApiService } from "../../../abstractions/api.service";
|
|
import { ErrorResponse } from "../../../models/response/error.response";
|
|
import {
|
|
FileUploadApiMethods,
|
|
FileUploadService,
|
|
} from "../../../platform/abstractions/file-upload/file-upload.service";
|
|
import { Utils } from "../../../platform/misc/utils";
|
|
import { EncArrayBuffer } from "../../../platform/models/domain/enc-array-buffer";
|
|
import { EncString } from "../../../platform/models/domain/enc-string";
|
|
import { SymmetricCryptoKey } from "../../../platform/models/domain/symmetric-crypto-key";
|
|
import { CipherFileUploadService as CipherFileUploadServiceAbstraction } from "../../abstractions/file-upload/cipher-file-upload.service";
|
|
import { Cipher } from "../../models/domain/cipher";
|
|
import { AttachmentRequest } from "../../models/request/attachment.request";
|
|
import { AttachmentUploadDataResponse } from "../../models/response/attachment-upload-data.response";
|
|
import { CipherResponse } from "../../models/response/cipher.response";
|
|
|
|
export class CipherFileUploadService implements CipherFileUploadServiceAbstraction {
|
|
constructor(
|
|
private apiService: ApiService,
|
|
private fileUploadService: FileUploadService,
|
|
) {}
|
|
|
|
async upload(
|
|
cipher: Cipher,
|
|
encFileName: EncString,
|
|
encData: EncArrayBuffer,
|
|
admin: boolean,
|
|
dataEncKey: [SymmetricCryptoKey, EncString],
|
|
): Promise<CipherResponse> {
|
|
const request: AttachmentRequest = {
|
|
key: dataEncKey[1].encryptedString,
|
|
fileName: encFileName.encryptedString,
|
|
fileSize: encData.buffer.byteLength,
|
|
adminRequest: admin,
|
|
};
|
|
|
|
let response: CipherResponse;
|
|
try {
|
|
const uploadDataResponse = await this.apiService.postCipherAttachment(cipher.id, request);
|
|
response = admin ? uploadDataResponse.cipherMiniResponse : uploadDataResponse.cipherResponse;
|
|
await this.fileUploadService.upload(
|
|
uploadDataResponse,
|
|
encFileName,
|
|
encData,
|
|
this.generateMethods(uploadDataResponse, response, request.adminRequest),
|
|
);
|
|
} catch (e) {
|
|
if (
|
|
(e instanceof ErrorResponse && (e as ErrorResponse).statusCode === 404) ||
|
|
(e as ErrorResponse).statusCode === 405
|
|
) {
|
|
response = await this.legacyServerAttachmentFileUpload(
|
|
request.adminRequest,
|
|
cipher.id,
|
|
encFileName,
|
|
encData,
|
|
dataEncKey[1],
|
|
);
|
|
} else if (e instanceof ErrorResponse) {
|
|
throw new Error((e as ErrorResponse).getSingleMessage());
|
|
} else {
|
|
throw e;
|
|
}
|
|
}
|
|
return response;
|
|
}
|
|
|
|
private generateMethods(
|
|
uploadData: AttachmentUploadDataResponse,
|
|
response: CipherResponse,
|
|
isAdmin: boolean,
|
|
): FileUploadApiMethods {
|
|
return {
|
|
postDirect: this.generatePostDirectCallback(uploadData, isAdmin),
|
|
renewFileUploadUrl: this.generateRenewFileUploadUrlCallback(uploadData, response, isAdmin),
|
|
rollback: this.generateRollbackCallback(response, uploadData, isAdmin),
|
|
};
|
|
}
|
|
|
|
private generatePostDirectCallback(uploadData: AttachmentUploadDataResponse, isAdmin: boolean) {
|
|
return (data: FormData) => {
|
|
const response = isAdmin ? uploadData.cipherMiniResponse : uploadData.cipherResponse;
|
|
return this.apiService.postAttachmentFile(response.id, uploadData.attachmentId, data);
|
|
};
|
|
}
|
|
|
|
private generateRenewFileUploadUrlCallback(
|
|
uploadData: AttachmentUploadDataResponse,
|
|
response: CipherResponse,
|
|
isAdmin: boolean,
|
|
) {
|
|
return async () => {
|
|
const renewResponse = await this.apiService.renewAttachmentUploadUrl(
|
|
response.id,
|
|
uploadData.attachmentId,
|
|
);
|
|
return renewResponse?.url;
|
|
};
|
|
}
|
|
|
|
private generateRollbackCallback(
|
|
response: CipherResponse,
|
|
uploadData: AttachmentUploadDataResponse,
|
|
isAdmin: boolean,
|
|
) {
|
|
return () => {
|
|
if (isAdmin) {
|
|
return this.apiService.deleteCipherAttachmentAdmin(response.id, uploadData.attachmentId);
|
|
} else {
|
|
return this.apiService.deleteCipherAttachment(response.id, uploadData.attachmentId);
|
|
}
|
|
};
|
|
}
|
|
|
|
/**
|
|
* @deprecated Mar 25 2021: This method has been deprecated in favor of direct uploads.
|
|
* This method still exists for backward compatibility with old server versions.
|
|
*/
|
|
async legacyServerAttachmentFileUpload(
|
|
admin: boolean,
|
|
cipherId: string,
|
|
encFileName: EncString,
|
|
encData: EncArrayBuffer,
|
|
key: EncString,
|
|
) {
|
|
const fd = new FormData();
|
|
try {
|
|
const blob = new Blob([encData.buffer], { type: "application/octet-stream" });
|
|
fd.append("key", key.encryptedString);
|
|
fd.append("data", blob, encFileName.encryptedString);
|
|
} catch (e) {
|
|
if (Utils.isNode && !Utils.isBrowser) {
|
|
fd.append("key", key.encryptedString);
|
|
fd.append(
|
|
"data",
|
|
Buffer.from(encData.buffer) as any,
|
|
{
|
|
filepath: encFileName.encryptedString,
|
|
contentType: "application/octet-stream",
|
|
} as any,
|
|
);
|
|
} else {
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
let response: CipherResponse;
|
|
try {
|
|
if (admin) {
|
|
response = await this.apiService.postCipherAttachmentAdminLegacy(cipherId, fd);
|
|
} else {
|
|
response = await this.apiService.postCipherAttachmentLegacy(cipherId, fd);
|
|
}
|
|
} catch (e) {
|
|
throw new Error((e as ErrorResponse).getSingleMessage());
|
|
}
|
|
|
|
return response;
|
|
}
|
|
}
|