mirror of
https://github.com/bitwarden/browser
synced 2025-12-15 15:53:27 +00:00
sub out create command
This commit is contained in:
46
src/commands/create.command.ts
Normal file
46
src/commands/create.command.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import * as program from 'commander';
|
||||
|
||||
import { CipherService } from 'jslib/abstractions/cipher.service';
|
||||
import { FolderService } from 'jslib/services/folder.service';
|
||||
|
||||
import { Response } from '../models/response';
|
||||
|
||||
import { CipherRequest } from '../models/request/cipherRequest';
|
||||
import { FolderRequest } from '../models/request/folderRequest';
|
||||
|
||||
export class CreateCommand {
|
||||
constructor(private cipherService: CipherService, private folderService: FolderService) { }
|
||||
|
||||
async run(object: string, requestData: string, cmd: program.Command): Promise<Response> {
|
||||
const reqJson = new Buffer(requestData, 'base64').toString();
|
||||
const req = JSON.parse(reqJson);
|
||||
switch (object.toLowerCase()) {
|
||||
case 'item':
|
||||
return await this.createCipher(req);
|
||||
case 'folder':
|
||||
return await this.createFolder(req);
|
||||
default:
|
||||
return Response.badRequest('Unknown object.');
|
||||
}
|
||||
}
|
||||
|
||||
private async createCipher(req: CipherRequest) {
|
||||
const cipher = await this.cipherService.encrypt(CipherRequest.toView(req));
|
||||
try {
|
||||
await this.cipherService.saveWithServer(cipher);
|
||||
return Response.success();
|
||||
} catch (e) {
|
||||
return Response.error(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
private async createFolder(req: FolderRequest) {
|
||||
const folder = await this.folderService.encrypt(FolderRequest.toView(req));
|
||||
try {
|
||||
await this.folderService.saveWithServer(folder);
|
||||
return Response.success();
|
||||
} catch (e) {
|
||||
return Response.error(e.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user