mirror of
https://github.com/bitwarden/browser
synced 2025-12-16 08:13:42 +00:00
Attachment azure upload blobs (#312)
* Add direct attachment download and upload API endpoints * Use direct download method Enable download of emergency access attachments through EmergencyAccessId * Match new Server model items * New Server model for creating attachments. Provides a url to upload data to, the type of upload, and the Cipher Response expected by the previous call * Use direct upload url and scheme * Report Failed single shot azure uploads * Add cipher attachment upload to file upload service * Deprecate legacy api methods * Handle old servers missing new upload api methods * Improve Send error handling * Fallback attachment downloads on new endpoint not found Limit upload size to the new 500MB * Improve error handling * lint fixes
This commit is contained in:
@@ -40,6 +40,7 @@ import { SortedCiphersCache } from '../models/domain/sortedCiphersCache';
|
||||
import { ApiService } from '../abstractions/api.service';
|
||||
import { CipherService as CipherServiceAbstraction } from '../abstractions/cipher.service';
|
||||
import { CryptoService } from '../abstractions/crypto.service';
|
||||
import { FileUploadService } from '../abstractions/fileUpload.service';
|
||||
import { I18nService } from '../abstractions/i18n.service';
|
||||
import { SearchService } from '../abstractions/search.service';
|
||||
import { SettingsService } from '../abstractions/settings.service';
|
||||
@@ -50,6 +51,7 @@ import { ConstantsService } from './constants.service';
|
||||
|
||||
import { sequentialize } from '../misc/sequentialize';
|
||||
import { Utils } from '../misc/utils';
|
||||
import { AttachmentRequest } from '../models/request/attachmentRequest';
|
||||
|
||||
const Keys = {
|
||||
ciphersPrefix: 'ciphers_',
|
||||
@@ -69,8 +71,8 @@ export class CipherService implements CipherServiceAbstraction {
|
||||
|
||||
constructor(private cryptoService: CryptoService, private userService: UserService,
|
||||
private settingsService: SettingsService, private apiService: ApiService,
|
||||
private storageService: StorageService, private i18nService: I18nService,
|
||||
private searchService: () => SearchService) {
|
||||
private fileUploadService: FileUploadService, private storageService: StorageService,
|
||||
private i18nService: I18nService, private searchService: () => SearchService) {
|
||||
}
|
||||
|
||||
get decryptedCipherCache() {
|
||||
@@ -618,14 +620,50 @@ export class CipherService implements CipherServiceAbstraction {
|
||||
const dataEncKey = await this.cryptoService.makeEncKey(key);
|
||||
const encData = await this.cryptoService.encryptToBytes(data, dataEncKey[0]);
|
||||
|
||||
const request: AttachmentRequest = {
|
||||
key: dataEncKey[1].encryptedString,
|
||||
fileName: encFileName.encryptedString,
|
||||
fileSize: encData.byteLength,
|
||||
adminRequest: admin,
|
||||
};
|
||||
|
||||
let response: CipherResponse;
|
||||
try {
|
||||
const uploadDataResponse = await this.apiService.postCipherAttachment(cipher.id, request);
|
||||
response = admin ? uploadDataResponse.cipherMiniResponse : uploadDataResponse.cipherResponse;
|
||||
this.fileUploadService.uploadCipherAttachment(admin, uploadDataResponse, filename, data);
|
||||
} catch (e) {
|
||||
if (e instanceof ErrorResponse && (e as ErrorResponse).statusCode === 404 || (e as ErrorResponse).statusCode === 405) {
|
||||
response = await this.legacyServerAttachmentFileUpload(admin, cipher.id, encFileName, encData, dataEncKey[1]);
|
||||
} else if (e instanceof ErrorResponse) {
|
||||
throw new Error((e as ErrorResponse).getSingleMessage());
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
const userId = await this.userService.getUserId();
|
||||
const cData = new CipherData(response, userId, cipher.collectionIds);
|
||||
if (!admin) {
|
||||
await this.upsert(cData);
|
||||
}
|
||||
return new Cipher(cData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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: CipherString,
|
||||
encData: ArrayBuffer, key: CipherString) {
|
||||
const fd = new FormData();
|
||||
try {
|
||||
const blob = new Blob([encData], { type: 'application/octet-stream' });
|
||||
fd.append('key', dataEncKey[1].encryptedString);
|
||||
fd.append('key', key.encryptedString);
|
||||
fd.append('data', blob, encFileName.encryptedString);
|
||||
} catch (e) {
|
||||
if (Utils.isNode && !Utils.isBrowser) {
|
||||
fd.append('key', dataEncKey[1].encryptedString);
|
||||
fd.append('key', key.encryptedString);
|
||||
fd.append('data', Buffer.from(encData) as any, {
|
||||
filepath: encFileName.encryptedString,
|
||||
contentType: 'application/octet-stream',
|
||||
@@ -638,20 +676,15 @@ export class CipherService implements CipherServiceAbstraction {
|
||||
let response: CipherResponse;
|
||||
try {
|
||||
if (admin) {
|
||||
response = await this.apiService.postCipherAttachmentAdmin(cipher.id, fd);
|
||||
response = await this.apiService.postCipherAttachmentAdminLegacy(cipherId, fd);
|
||||
} else {
|
||||
response = await this.apiService.postCipherAttachment(cipher.id, fd);
|
||||
response = await this.apiService.postCipherAttachmentLegacy(cipherId, fd);
|
||||
}
|
||||
} catch (e) {
|
||||
throw new Error((e as ErrorResponse).getSingleMessage());
|
||||
}
|
||||
|
||||
const userId = await this.userService.getUserId();
|
||||
const cData = new CipherData(response, userId, cipher.collectionIds);
|
||||
if (!admin) {
|
||||
await this.upsert(cData);
|
||||
}
|
||||
return new Cipher(cData);
|
||||
return response;
|
||||
}
|
||||
|
||||
async saveCollectionsWithServer(cipher: Cipher): Promise<any> {
|
||||
|
||||
Reference in New Issue
Block a user