1
0
mirror of https://github.com/bitwarden/cli synced 2026-01-03 16:53:14 +00:00
Files
cli/src/commands/send/get.command.ts
Matt Gibson fcd0c529ca Direct download for send (#243)
* Remove Get file capability

This needs to be removed because the SendFileResponse no longer contains
a url to download the file from. Instead, a GetDownloadLink method
must be used. That method increments access count, which is not
desirable for the owner of the Send. The cleanest approach is to remove
the capability, which also matches Web client's behavior

* jslib updates

* Use GetDownloadData method to receive download Url

* Update jslib
2021-03-02 10:05:20 -06:00

80 lines
2.7 KiB
TypeScript

import * as program from 'commander';
import { ApiService } from 'jslib/abstractions/api.service';
import { CryptoService } from 'jslib/abstractions/crypto.service';
import { EnvironmentService } from 'jslib/abstractions/environment.service';
import { SearchService } from 'jslib/abstractions/search.service';
import { SendService } from 'jslib/abstractions/send.service';
import { SendView } from 'jslib/models/view/sendView';
import { Response } from 'jslib/cli/models/response';
import { DownloadCommand } from '../download.command';
import { SendResponse } from '../../models/response/sendResponse';
import { Utils } from 'jslib/misc/utils';
export class SendGetCommand extends DownloadCommand {
constructor(private sendService: SendService, private environmentService: EnvironmentService,
private searchService: SearchService, cryptoService: CryptoService) {
super(cryptoService);
}
async run(id: string, options: program.OptionValues) {
let sends = await this.getSendView(id);
if (sends == null) {
return Response.notFound();
}
const webVaultUrl = this.environmentService.getWebVaultUrl();
let filter = (s: SendView) => true;
let selector = async (s: SendView): Promise<Response> => Response.success(new SendResponse(s, webVaultUrl));
if (options.text != null) {
filter = s => {
return filter(s) && s.text != null;
};
selector = async s => {
// Write to stdout and response success so we get the text string only to stdout
process.stdout.write(s.text.text);
return Response.success();
};
}
if (Array.isArray(sends)) {
if (filter != null) {
sends = sends.filter(filter);
}
if (sends.length > 1) {
return Response.multipleResults(sends.map(s => s.id));
}
if (sends.length > 0) {
return selector(sends[0]);
}
else {
return Response.notFound();
}
}
return selector(sends);
}
private async getSendView(id: string): Promise<SendView | SendView[]> {
if (Utils.isGuid(id)) {
const send = await this.sendService.get(id);
if (send != null) {
return await send.decrypt();
}
} else if (id.trim() !== '') {
let sends = await this.sendService.getAllDecrypted();
sends = this.searchService.searchSends(sends, id);
if (sends.length > 1) {
return sends;
} else if (sends.length > 0) {
return sends[0];
}
}
}
}