1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-19 07:35:48 +02:00
bitwarden-browser/src/services/folder.service.ts

162 lines
5.1 KiB
TypeScript
Raw Normal View History

2017-11-05 02:09:26 +01:00
import { CipherString } from '../models/domain/cipherString';
import { Folder } from '../models/domain/folder';
2017-11-05 02:37:35 +01:00
2017-11-05 02:09:26 +01:00
import { FolderData } from '../models/data/folderData';
2017-11-05 02:37:35 +01:00
2017-11-05 02:09:26 +01:00
import { FolderRequest } from '../models/request/folderRequest';
2017-11-05 02:37:35 +01:00
import { FolderResponse } from '../models/response/folderResponse';
2017-11-05 02:09:26 +01:00
import ApiService from './api.service';
import CryptoService from './crypto.service';
import UserService from './user.service';
import UtilsService from './utils.service';
const Keys = {
2017-11-05 02:37:35 +01:00
foldersPrefix: 'folders_',
2017-11-05 02:09:26 +01:00
};
export default class FolderService {
decryptedFolderCache: any[];
2017-11-05 02:37:35 +01:00
constructor(private cryptoService: CryptoService, private userService: UserService,
2017-11-16 18:49:23 +01:00
private i18nService: any, private apiService: ApiService) {
2017-11-05 02:09:26 +01:00
}
clearCache(): void {
this.decryptedFolderCache = null;
}
async encrypt(model: any): Promise<Folder> {
const folder = new Folder();
2017-11-05 04:18:38 +01:00
folder.id = model.id;
2017-11-05 02:09:26 +01:00
folder.name = await this.cryptoService.encrypt(model.name);
return folder;
}
2017-11-05 02:37:35 +01:00
async get(id: string): Promise<Folder> {
2017-11-05 02:09:26 +01:00
const userId = await this.userService.getUserId();
2017-11-05 04:18:38 +01:00
const folders = await UtilsService.getObjFromStorage<{ [id: string]: FolderData; }>(
Keys.foldersPrefix + userId);
if (folders == null || !folders.hasOwnProperty(id)) {
2017-11-05 02:09:26 +01:00
return null;
}
2017-11-05 04:18:38 +01:00
return new Folder(folders[id]);
2017-11-05 02:09:26 +01:00
}
async getAll(): Promise<Folder[]> {
const userId = await this.userService.getUserId();
2017-11-05 04:18:38 +01:00
const folders = await UtilsService.getObjFromStorage<{ [id: string]: FolderData; }>(
Keys.foldersPrefix + userId);
2017-11-05 02:37:35 +01:00
const response: Folder[] = [];
2017-11-05 04:18:38 +01:00
for (const id in folders) {
if (folders.hasOwnProperty(id)) {
response.push(new Folder(folders[id]));
}
}
2017-11-05 02:09:26 +01:00
return response;
}
async getAllDecrypted(): Promise<any[]> {
if (this.decryptedFolderCache != null) {
return this.decryptedFolderCache;
}
const decFolders: any[] = [{
id: null,
2017-11-05 02:37:35 +01:00
name: this.i18nService.noneFolder,
2017-11-05 02:09:26 +01:00
}];
const key = await this.cryptoService.getKey();
2017-11-05 02:37:35 +01:00
if (key == null) {
2017-11-05 02:09:26 +01:00
throw new Error('No key.');
}
2017-11-16 18:49:23 +01:00
const promises: Array<Promise<any>> = [];
2017-11-05 02:09:26 +01:00
const folders = await this.getAll();
2017-11-16 18:49:23 +01:00
folders.forEach((folder) => {
2017-11-05 02:09:26 +01:00
promises.push(folder.decrypt().then((f: any) => {
decFolders.push(f);
}));
2017-11-16 18:49:23 +01:00
});
2017-11-05 02:09:26 +01:00
await Promise.all(promises);
this.decryptedFolderCache = decFolders;
return this.decryptedFolderCache;
}
2017-11-05 02:37:35 +01:00
async saveWithServer(folder: Folder): Promise<any> {
2017-11-05 02:09:26 +01:00
const request = new FolderRequest(folder);
let response: FolderResponse;
2017-11-05 02:37:35 +01:00
if (folder.id == null) {
2017-11-05 02:09:26 +01:00
response = await this.apiService.postFolder(request);
folder.id = response.id;
} else {
response = await this.apiService.putFolder(folder.id, request);
}
const userId = await this.userService.getUserId();
const data = new FolderData(response, userId);
await this.upsert(data);
}
async upsert(folder: FolderData | FolderData[]): Promise<any> {
const userId = await this.userService.getUserId();
2017-11-05 04:18:38 +01:00
let folders = await UtilsService.getObjFromStorage<{ [id: string]: FolderData; }>(
Keys.foldersPrefix + userId);
2017-11-05 02:37:35 +01:00
if (folders == null) {
2017-11-05 04:18:38 +01:00
folders = {};
2017-11-05 02:09:26 +01:00
}
2017-11-05 02:37:35 +01:00
if (folder instanceof FolderData) {
2017-11-05 02:09:26 +01:00
const f = folder as FolderData;
2017-11-05 04:18:38 +01:00
folders[f.id] = f;
2017-11-05 02:09:26 +01:00
} else {
2017-11-16 18:49:23 +01:00
(folder as FolderData[]).forEach((f) => {
2017-11-05 04:18:38 +01:00
folders[f.id] = f;
2017-11-16 18:49:23 +01:00
});
2017-11-05 02:09:26 +01:00
}
await UtilsService.saveObjToStorage(Keys.foldersPrefix + userId, folders);
this.decryptedFolderCache = null;
}
2017-11-06 17:55:17 +01:00
async replace(folders: { [id: string]: FolderData; }): Promise<any> {
2017-11-05 02:09:26 +01:00
const userId = await this.userService.getUserId();
await UtilsService.saveObjToStorage(Keys.foldersPrefix + userId, folders);
this.decryptedFolderCache = null;
}
async clear(userId: string): Promise<any> {
2017-11-05 02:37:35 +01:00
await UtilsService.removeFromStorage(Keys.foldersPrefix + userId);
2017-11-05 02:09:26 +01:00
this.decryptedFolderCache = null;
}
async delete(id: string | string[]): Promise<any> {
const userId = await this.userService.getUserId();
2017-11-05 04:18:38 +01:00
const folders = await UtilsService.getObjFromStorage<{ [id: string]: FolderData; }>(
Keys.foldersPrefix + userId);
2017-11-05 02:37:35 +01:00
if (folders == null) {
2017-11-05 02:09:26 +01:00
return;
}
2017-11-05 04:18:38 +01:00
if (typeof id === 'string') {
2017-11-05 02:09:26 +01:00
const i = id as string;
2017-11-05 04:18:38 +01:00
delete folders[id];
2017-11-05 02:09:26 +01:00
} else {
2017-11-16 18:49:23 +01:00
(id as string[]).forEach((i) => {
2017-11-05 04:18:38 +01:00
delete folders[i];
2017-11-16 18:49:23 +01:00
});
2017-11-05 02:09:26 +01:00
}
await UtilsService.saveObjToStorage(Keys.foldersPrefix + userId, folders);
this.decryptedFolderCache = null;
}
async deleteWithServer(id: string): Promise<any> {
await this.apiService.deleteFolder(id);
await this.delete(id);
}
}