mirror of https://github.com/bitwarden/cli.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.6 KiB
88 lines
2.6 KiB
import { SendService } from "jslib-common/abstractions/send.service"; |
|
import { StateService } from "jslib-common/abstractions/state.service"; |
|
import { SendType } from "jslib-common/enums/sendType"; |
|
import { Response } from "jslib-node/cli/models/response"; |
|
|
|
import { SendResponse } from "../../models/response/sendResponse"; |
|
import { CliUtils } from "../../utils"; |
|
|
|
import { SendGetCommand } from "./get.command"; |
|
|
|
export class SendEditCommand { |
|
constructor( |
|
private sendService: SendService, |
|
private stateService: StateService, |
|
private getCommand: SendGetCommand |
|
) {} |
|
|
|
async run(requestJson: string, cmdOptions: Record<string, any>): Promise<Response> { |
|
if (process.env.BW_SERVE !== "true" && (requestJson == null || requestJson === "")) { |
|
requestJson = await CliUtils.readStdin(); |
|
} |
|
|
|
if (requestJson == null || requestJson === "") { |
|
return Response.badRequest("`requestJson` was not provided."); |
|
} |
|
|
|
let req: SendResponse = null; |
|
if (typeof requestJson !== "string") { |
|
req = requestJson; |
|
} else { |
|
try { |
|
const reqJson = Buffer.from(requestJson, "base64").toString(); |
|
req = SendResponse.fromJson(reqJson); |
|
} catch (e) { |
|
return Response.badRequest("Error parsing the encoded request data."); |
|
} |
|
} |
|
|
|
const normalizedOptions = new Options(cmdOptions); |
|
req.id = normalizedOptions.itemId || req.id; |
|
|
|
if (req.id != null) { |
|
req.id = req.id.toLowerCase(); |
|
} |
|
|
|
const send = await this.sendService.get(req.id); |
|
|
|
if (send == null) { |
|
return Response.notFound(); |
|
} |
|
|
|
if (send.type !== req.type) { |
|
return Response.badRequest("Cannot change a Send's type"); |
|
} |
|
|
|
if (send.type === SendType.File && !(await this.stateService.getCanAccessPremium())) { |
|
return Response.error("Premium status is required to use this feature."); |
|
} |
|
|
|
let sendView = await send.decrypt(); |
|
sendView = SendResponse.toView(req, sendView); |
|
|
|
if (typeof req.password !== "string" || req.password === "") { |
|
req.password = null; |
|
} |
|
|
|
try { |
|
const [encSend, encFileData] = await this.sendService.encrypt(sendView, null, req.password); |
|
// Add dates from template |
|
encSend.deletionDate = sendView.deletionDate; |
|
encSend.expirationDate = sendView.expirationDate; |
|
|
|
await this.sendService.saveWithServer([encSend, encFileData]); |
|
} catch (e) { |
|
return Response.error(e); |
|
} |
|
|
|
return await this.getCommand.run(send.id, {}); |
|
} |
|
} |
|
|
|
class Options { |
|
itemId: string; |
|
|
|
constructor(passedOptions: Record<string, any>) { |
|
this.itemId = passedOptions?.itemId || passedOptions?.itemid; |
|
} |
|
}
|
|
|