Skip to content

Commit

Permalink
fix: sanitize backup file names for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
arielsvg committed Mar 3, 2021
1 parent a14b7ed commit 9d017b5
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions app/assets/javascripts/services/archiveManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,25 @@ import {
PayloadContent,
} from '@standardnotes/snjs';

function zippableTxtName(name: string, suffix = ""): string {
const sanitizedName = name
.replace(/\//g, '')
.replace(/\\+/g, '')
.replace(/:/g, ' ')
.replace(/\./g, ' ');
const nameEnd = suffix + ".txt";
function sanitizeFileName(name: string) {
return name
.replace(/\//g, '')
.replace(/\\+/g, '')
.replace(/:/g, ' ')
.replace(/\|/g, ' ')
.replace(/\./g, ' ');
}

function zippableTxtName(name: string, suffix = ''): string {
const sanitizedName = sanitizeFileName(name);
const nameEnd = suffix + '.txt';
const maxFileNameLength = 255;
return sanitizedName.slice(0, maxFileNameLength - nameEnd.length) + nameEnd;
}

export class ArchiveManager {

private readonly application: WebApplication
private textFile?: string
private readonly application: WebApplication;
private textFile?: string;

constructor(application: WebApplication) {
this.application = application;
Expand All @@ -36,10 +40,9 @@ export class ArchiveManager {
if (!data) {
return;
}
const blobData = new Blob(
[JSON.stringify(data, null, 2)],
{ type: 'text/json' }
);
const blobData = new Blob([JSON.stringify(data, null, 2)], {
type: 'text/json',
});
if (encrypted) {
this.downloadData(
blobData,
Expand Down Expand Up @@ -85,21 +88,18 @@ export class ArchiveManager {
});
}

private async downloadZippedDecryptedItems(
data: BackupFile
) {
private async downloadZippedDecryptedItems(data: BackupFile) {
await this.loadZip();
const items = data.items;
this.zip.createWriter(
new this.zip.BlobWriter('application/zip'),
async (zipWriter: any) => {
await new Promise((resolve) => {
const blob = new Blob(
[JSON.stringify(data, null, 2)],
{ type: 'text/plain' }
);
const blob = new Blob([JSON.stringify(data, null, 2)], {
type: 'text/plain',
});
const fileName = zippableTxtName(
'Standard Notes Backup and Import File.txt'
'Standard Notes Backup and Import File'
);
zipWriter.add(fileName, new this.zip.BlobReader(blob), resolve);
});
Expand All @@ -120,7 +120,8 @@ export class ArchiveManager {
name = '';
}
const blob = new Blob([contents], { type: 'text/plain' });
const fileName = `Items/${item.content_type}/` +
const fileName =
`Items/${sanitizeFileName(item.content_type)}/` +
zippableTxtName(name, `-${item.uuid.split('-')[0]}`);
zipWriter.add(fileName, new this.zip.BlobReader(blob), () => {
index++;
Expand All @@ -138,7 +139,9 @@ export class ArchiveManager {
});
};
nextFile();
}, onerror);
},
onerror
);
}

private hrefForData(data: Blob) {
Expand Down

0 comments on commit 9d017b5

Please sign in to comment.