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

156 lines
5.0 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 ConstantsService from './constants.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,
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();
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();
const folders = await UtilsService.getObjFromStorage<Map<string, FolderData>>(Keys.foldersPrefix + userId);
2017-11-05 02:37:35 +01:00
if (folders == null || !folders.has(id)) {
2017-11-05 02:09:26 +01:00
return null;
}
return new Folder(folders.get(id));
}
async getAll(): Promise<Folder[]> {
const userId = await this.userService.getUserId();
const folders = await UtilsService.getObjFromStorage<Map<string, FolderData>>(Keys.foldersPrefix + userId);
2017-11-05 02:37:35 +01:00
const response: Folder[] = [];
2017-11-05 02:09:26 +01:00
folders.forEach((folder) => {
response.push(new Folder(folder));
});
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.');
}
const promises = [];
const folders = await this.getAll();
2017-11-05 02:37:35 +01:00
for (const folder of folders) {
2017-11-05 02:09:26 +01:00
promises.push(folder.decrypt().then((f: any) => {
decFolders.push(f);
}));
}
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();
let folders = await UtilsService.getObjFromStorage<Map<string, FolderData>>(Keys.foldersPrefix + userId);
2017-11-05 02:37:35 +01:00
if (folders == null) {
2017-11-05 02:09:26 +01:00
folders = new Map<string, FolderData>();
}
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;
folders.set(f.id, f);
} else {
2017-11-05 02:37:35 +01:00
for (const f of (folder as FolderData[])) {
2017-11-05 02:09:26 +01:00
folders.set(f.id, f);
}
}
await UtilsService.saveObjToStorage(Keys.foldersPrefix + userId, folders);
this.decryptedFolderCache = null;
}
2017-11-05 02:37:35 +01:00
async replace(folders: 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 02:37:35 +01:00
const folders = await UtilsService.getObjFromStorage<Map<string, FolderData>>(Keys.foldersPrefix + userId);
if (folders == null) {
2017-11-05 02:09:26 +01:00
return;
}
2017-11-05 02:37:35 +01:00
if (id instanceof String) {
2017-11-05 02:09:26 +01:00
const i = id as string;
folders.delete(i);
} else {
2017-11-05 02:37:35 +01:00
for (const i of (id as string[])) {
2017-11-05 02:09:26 +01:00
folders.delete(i);
}
}
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);
}
}