fix: zip decrypted backup items together as one file

This commit is contained in:
Baptiste Grob
2020-07-21 17:06:30 +02:00
parent 096b9c8c87
commit 79d48b7e07

View File

@@ -1,6 +1,17 @@
import { WebApplication } from '@/ui_models/application'; import { WebApplication } from '@/ui_models/application';
import { EncryptionIntent, ProtectedAction, SNItem, ContentType, SNNote } from 'snjs'; import { EncryptionIntent, ProtectedAction, SNItem, ContentType, SNNote } from 'snjs';
function zippableTxtName(name: string, suffix = ""): string {
const sanitizedName = name
.replace(/\//g, '')
.replace(/\\+/g, '')
.replace(/:/g, ' ')
.replace(/\./g, ' ');
const nameEnd = suffix + ".txt";
const maxFileNameLength = 255;
return sanitizedName.slice(0, maxFileNameLength - nameEnd.length) + nameEnd;
}
export class ArchiveManager { export class ArchiveManager {
private readonly application: WebApplication private readonly application: WebApplication
@@ -11,26 +22,36 @@ export class ArchiveManager {
} }
public async downloadBackup(encrypted: boolean) { public async downloadBackup(encrypted: boolean) {
return this.downloadBackupOfItems(this.application.allItems(), encrypted); const items = this.application.allItems();
}
public async downloadBackupOfItems(items: SNItem[], encrypted: boolean) {
const run = async () => { const run = async () => {
// download in Standard Notes format // download in Standard Notes format
const intent = encrypted const intent = encrypted
? EncryptionIntent.FileEncrypted ? EncryptionIntent.FileEncrypted
: EncryptionIntent.FileDecrypted; : EncryptionIntent.FileDecrypted;
this.itemsData(items, intent).then((data) => { const data = await this.itemsData(items, intent)
const modifier = encrypted ? 'Encrypted' : 'Decrypted'; if (encrypted) {
this.downloadData( this.downloadData(
data!, data!,
`Standard Notes ${modifier} Backup - ${this.formattedDate()}.txt` `Standard Notes Encrypted Backup - ${this.formattedDate()}.txt`
); );
// download as zipped plain text files } else {
if (!encrypted) { const data = await this.application.createBackupFile(items, intent);
this.downloadZippedItems(items); if (data) {
/** download as zipped plain text files */
this.downloadZippedItems(
items,
/** Add the backup file to the archive */
(zipWriter, zip) => new Promise((resolve) => {
const blob = new Blob([data], { type: 'text/plain' });
const fileName = zippableTxtName(
`Standard Notes Decrypted Backup - ${this.formattedDate()}`
);
zipWriter.add(fileName, new zip.BlobReader(blob), resolve);
})
);
} }
}); }
}; };
if ( if (
@@ -87,11 +108,15 @@ export class ArchiveManager {
}); });
} }
private async downloadZippedItems(items: SNItem[]) { private async downloadZippedItems(
items: SNItem[],
onOpenZip: (zipWriter: any, zip: any) => Promise<void>
) {
await this.loadZip(); await this.loadZip();
this.zip.createWriter( this.zip.createWriter(
new this.zip.BlobWriter('application/zip'), new this.zip.BlobWriter('application/zip'),
(zipWriter: any) => { async (zipWriter: any) => {
await onOpenZip(zipWriter, this.zip);
let index = 0; let index = 0;
const nextFile = () => { const nextFile = () => {
const item = items[index]; const item = items[index];
@@ -108,11 +133,8 @@ export class ArchiveManager {
name = ''; name = '';
} }
const blob = new Blob([contents], { type: 'text/plain' }); const blob = new Blob([contents], { type: 'text/plain' });
let filePrefix = name.replace(/\//g, '').replace(/\\+/g, ''); const fileName = item.content_type + '/' +
const fileSuffix = `-${item.uuid.split('-')[0]}.txt`; zippableTxtName(name, `-${item.uuid.split('-')[0]}`);
// Standard max filename length is 255. Slice the note name down to allow filenameEnd
filePrefix = filePrefix.slice(0, (255 - fileSuffix.length));
const fileName = `${item.content_type}/${filePrefix}${fileSuffix}`;
zipWriter.add(fileName, new this.zip.BlobReader(blob), () => { zipWriter.add(fileName, new this.zip.BlobReader(blob), () => {
index++; index++;
if (index < items.length) { if (index < items.length) {