diff --git a/apps/cli/src/commands/send-download.command.ts b/apps/cli/src/commands/send-download.command.ts new file mode 100644 index 00000000000..4ee91a88578 --- /dev/null +++ b/apps/cli/src/commands/send-download.command.ts @@ -0,0 +1,47 @@ +// FIXME: Update this file to be type safe and remove this and next line +// @ts-strict-ignore +import { firstValueFrom } from "rxjs"; + +import { ApiService } from "@bitwarden/common/abstractions/api.service"; +import { EncryptService } from "@bitwarden/common/key-management/crypto/abstractions/encrypt.service"; +import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service"; +import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service"; + +import { DownloadCommand } from "./download.command"; + +/** + * Used to download and save attachments + */ +export abstract class SendDownload extends DownloadCommand { + /** + * @param encryptService - Needed for decryption of the retrieved attachment + * @param apiService - Needed to override the existing nativeFetch which is available as of Node 18, to support proxies + */ + constructor( + protected encryptService: EncryptService, + protected apiService: ApiService, + protected environmentService: EnvironmentService, + protected platformUtilsService: PlatformUtilsService, + ) { + super(encryptService, apiService); + } + + protected getIdAndKey(url: URL): [string, string] { + const result = url.hash.slice(1).split("/").slice(-2); + return [result[0], result[1]]; + } + + protected async getApiUrl(url: URL) { + const env = await firstValueFrom(this.environmentService.environment$); + const urls = env.getUrls(); + if (url.origin === "https://send.bitwarden.com") { + return "https://api.bitwarden.com"; + } else if (url.origin === urls.api) { + return url.origin; + } else if (this.platformUtilsService.isDev() && url.origin === urls.webVault) { + return urls.api; + } else { + return url.origin + "/api"; + } + } +} diff --git a/apps/cli/src/tools/send/services/send-download.service.ts b/apps/cli/src/tools/send/services/send-download.service.ts index 2519edc65a5..8c1d1108669 100644 --- a/apps/cli/src/tools/send/services/send-download.service.ts +++ b/apps/cli/src/tools/send/services/send-download.service.ts @@ -21,13 +21,13 @@ import { SendApiService } from "@bitwarden/common/tools/send/services/send-api.s import { KeyService } from "@bitwarden/key-management"; import { NodeUtils } from "@bitwarden/node/node-utils"; -import { DownloadCommand } from "../../../commands/download.command"; +import { SendDownload } from "../../../commands/send-download.command"; import { Response } from "../../../models/response"; import { SendAccessResponse } from "../models/send-access.response"; import { SendResponse } from "../models/send.response"; // Note: DownloadCommand is actually an abstract class -export class SendDownloadService extends DownloadCommand { +export class SendDownloadService extends SendDownload { private decKey: SymmetricCryptoKey; constructor( @@ -39,7 +39,7 @@ export class SendDownloadService extends DownloadCommand { private cryptoFunctionService: CryptoFunctionService, private sendApiService: SendApiService, ) { - super(encryptService, apiService); + super(encryptService, apiService, environmentService, platformUtilsService); } async download(sends: SendView, options: OptionValues) {