mirror of
https://github.com/bitwarden/jslib
synced 2026-01-14 22:43:26 +00:00
30 lines
988 B
TypeScript
30 lines
988 B
TypeScript
import { ApiService } from '../abstractions/api.service';
|
|
|
|
import { EncArrayBuffer } from '../models/domain/encArrayBuffer';
|
|
|
|
import { Utils } from '../misc/utils';
|
|
|
|
export class BitwardenFileUploadService
|
|
{
|
|
constructor(private apiService: ApiService) { }
|
|
|
|
async upload(encryptedFileName: string, encryptedFileData: EncArrayBuffer, apiCall: (fd: FormData) => Promise<any>) {
|
|
const fd = new FormData();
|
|
try {
|
|
const blob = new Blob([encryptedFileData.buffer], { type: 'application/octet-stream' });
|
|
fd.append('data', blob, encryptedFileName);
|
|
} catch (e) {
|
|
if (Utils.isNode && !Utils.isBrowser) {
|
|
fd.append('data', Buffer.from(encryptedFileData.buffer) as any, {
|
|
filepath: encryptedFileName,
|
|
contentType: 'application/octet-stream',
|
|
} as any);
|
|
} else {
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
await apiCall(fd);
|
|
}
|
|
}
|