export to json

This commit is contained in:
Kyle Spearrin 2018-12-17 10:32:02 -05:00
parent 94f103c474
commit e7b5868aad
2 changed files with 91 additions and 50 deletions

View File

@ -1,5 +1,5 @@
export abstract class ExportService {
getExport: (format?: 'csv' | 'json') => Promise<string>;
getOrganizationExport: (organizationId: string, format?: 'csv' | 'json') => Promise<string>;
getFileName: (prefix?: string) => string;
getFileName: (prefix?: string, extension?: string) => string;
}

View File

@ -18,6 +18,10 @@ import { CipherData } from '../models/data/cipherData';
import { CollectionData } from '../models/data/collectionData';
import { CollectionDetailsResponse } from '../models/response/collectionResponse';
import { CipherWithIds as CipherExport } from '../models/export/cipherWithIds';
import { CollectionWithId as CollectionExport } from '../models/export/collectionWithId';
import { FolderWithId as FolderExport } from '../models/export/folderWithId';
export class ExportService implements ExportServiceAbstraction {
constructor(private folderService: FolderService, private cipherService: CipherService,
private apiService: ApiService) { }
@ -37,6 +41,7 @@ export class ExportService implements ExportServiceAbstraction {
await Promise.all(promises);
if (format === 'csv') {
const foldersMap = new Map<string, FolderView>();
decFolders.forEach((f) => {
foldersMap.set(f.id, f);
@ -48,22 +53,42 @@ export class ExportService implements ExportServiceAbstraction {
if (c.type !== CipherType.Login && c.type !== CipherType.SecureNote) {
return;
}
if (c.organizationId != null) {
return;
}
const cipher: any = {};
cipher.folder = c.folderId != null && foldersMap.has(c.folderId) ? foldersMap.get(c.folderId).name : null;
cipher.folder = c.folderId != null && foldersMap.has(c.folderId) ?
foldersMap.get(c.folderId).name : null;
cipher.favorite = c.favorite ? 1 : null;
this.buildCommonCipher(cipher, c);
exportCiphers.push(cipher);
});
if (format === 'csv') {
return papa.unparse(exportCiphers);
} else {
return JSON.stringify(exportCiphers, null, ' ');
const jsonDoc: any = {
folders: [],
items: [],
};
decFolders.forEach((f) => {
const folder = new FolderExport();
folder.build(f);
jsonDoc.folders.push(folder);
});
decCiphers.forEach((c) => {
if (c.organizationId != null) {
return;
}
const cipher = new CipherExport();
cipher.build(c);
cipher.collectionIds = null;
jsonDoc.items.push(cipher);
});
return JSON.stringify(jsonDoc, null, ' ');
}
}
@ -100,6 +125,7 @@ export class ExportService implements ExportServiceAbstraction {
await Promise.all(promises);
if (format === 'csv') {
const collectionsMap = new Map<string, CollectionView>();
decCollections.forEach((c) => {
collectionsMap.set(c.id, c);
@ -122,21 +148,36 @@ export class ExportService implements ExportServiceAbstraction {
exportCiphers.push(cipher);
});
if (format === 'csv') {
return papa.unparse(exportCiphers);
} else {
return JSON.stringify(exportCiphers, null, ' ');
const jsonDoc: any = {
collections: [],
items: [],
};
decCollections.forEach((c) => {
const collection = new CollectionExport();
collection.build(c);
jsonDoc.collections.push(collection);
});
decCiphers.forEach((c) => {
const cipher = new CipherExport();
cipher.build(c);
jsonDoc.items.push(cipher);
});
return JSON.stringify({}, null, ' ');
}
}
getFileName(prefix: string = null): string {
getFileName(prefix: string = null, extension: string = 'csv'): string {
const now = new Date();
const dateString =
now.getFullYear() + '' + this.padNumber(now.getMonth() + 1, 2) + '' + this.padNumber(now.getDate(), 2) +
this.padNumber(now.getHours(), 2) + '' + this.padNumber(now.getMinutes(), 2) +
this.padNumber(now.getSeconds(), 2);
return 'bitwarden' + (prefix ? ('_' + prefix) : '') + '_export_' + dateString + '.csv';
return 'bitwarden' + (prefix ? ('_' + prefix) : '') + '_export_' + dateString + '.' + extension;
}
private padNumber(num: number, width: number, padCharacter: string = '0'): string {