mirror of
https://github.com/bitwarden/browser.git
synced 2025-02-02 23:11:40 +01:00
export to json
This commit is contained in:
parent
94f103c474
commit
e7b5868aad
@ -1,5 +1,5 @@
|
|||||||
export abstract class ExportService {
|
export abstract class ExportService {
|
||||||
getExport: (format?: 'csv' | 'json') => Promise<string>;
|
getExport: (format?: 'csv' | 'json') => Promise<string>;
|
||||||
getOrganizationExport: (organizationId: string, format?: 'csv' | 'json') => Promise<string>;
|
getOrganizationExport: (organizationId: string, format?: 'csv' | 'json') => Promise<string>;
|
||||||
getFileName: (prefix?: string) => string;
|
getFileName: (prefix?: string, extension?: string) => string;
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,10 @@ import { CipherData } from '../models/data/cipherData';
|
|||||||
import { CollectionData } from '../models/data/collectionData';
|
import { CollectionData } from '../models/data/collectionData';
|
||||||
import { CollectionDetailsResponse } from '../models/response/collectionResponse';
|
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 {
|
export class ExportService implements ExportServiceAbstraction {
|
||||||
constructor(private folderService: FolderService, private cipherService: CipherService,
|
constructor(private folderService: FolderService, private cipherService: CipherService,
|
||||||
private apiService: ApiService) { }
|
private apiService: ApiService) { }
|
||||||
@ -37,6 +41,7 @@ export class ExportService implements ExportServiceAbstraction {
|
|||||||
|
|
||||||
await Promise.all(promises);
|
await Promise.all(promises);
|
||||||
|
|
||||||
|
if (format === 'csv') {
|
||||||
const foldersMap = new Map<string, FolderView>();
|
const foldersMap = new Map<string, FolderView>();
|
||||||
decFolders.forEach((f) => {
|
decFolders.forEach((f) => {
|
||||||
foldersMap.set(f.id, f);
|
foldersMap.set(f.id, f);
|
||||||
@ -48,22 +53,42 @@ export class ExportService implements ExportServiceAbstraction {
|
|||||||
if (c.type !== CipherType.Login && c.type !== CipherType.SecureNote) {
|
if (c.type !== CipherType.Login && c.type !== CipherType.SecureNote) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (c.organizationId != null) {
|
if (c.organizationId != null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const cipher: any = {};
|
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;
|
cipher.favorite = c.favorite ? 1 : null;
|
||||||
this.buildCommonCipher(cipher, c);
|
this.buildCommonCipher(cipher, c);
|
||||||
exportCiphers.push(cipher);
|
exportCiphers.push(cipher);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (format === 'csv') {
|
|
||||||
return papa.unparse(exportCiphers);
|
return papa.unparse(exportCiphers);
|
||||||
} else {
|
} 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);
|
await Promise.all(promises);
|
||||||
|
|
||||||
|
if (format === 'csv') {
|
||||||
const collectionsMap = new Map<string, CollectionView>();
|
const collectionsMap = new Map<string, CollectionView>();
|
||||||
decCollections.forEach((c) => {
|
decCollections.forEach((c) => {
|
||||||
collectionsMap.set(c.id, c);
|
collectionsMap.set(c.id, c);
|
||||||
@ -122,21 +148,36 @@ export class ExportService implements ExportServiceAbstraction {
|
|||||||
exportCiphers.push(cipher);
|
exportCiphers.push(cipher);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (format === 'csv') {
|
|
||||||
return papa.unparse(exportCiphers);
|
return papa.unparse(exportCiphers);
|
||||||
} else {
|
} 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 now = new Date();
|
||||||
const dateString =
|
const dateString =
|
||||||
now.getFullYear() + '' + this.padNumber(now.getMonth() + 1, 2) + '' + this.padNumber(now.getDate(), 2) +
|
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.getHours(), 2) + '' + this.padNumber(now.getMinutes(), 2) +
|
||||||
this.padNumber(now.getSeconds(), 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 {
|
private padNumber(num: number, width: number, padCharacter: string = '0'): string {
|
||||||
|
Loading…
Reference in New Issue
Block a user