Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(filesystem): web appendFile with base64 data #928

Merged
merged 6 commits into from
Apr 27, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion filesystem/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,15 @@ export class FilesystemWeb extends WebPlugin implements FilesystemPlugin {
}

if (occupiedEntry !== undefined) {
data = occupiedEntry.content + data;
if (occupiedEntry.content && this.isBase64String(occupiedEntry.content)) {
if (!this.isBase64String(data))
throw Error(
'Supplied data does not match the current file data encoding (base64)',
);
data = btoa(atob(occupiedEntry.content) + atob(data));
} else {
data = occupiedEntry.content + data;
}
ctime = occupiedEntry.ctime;
}
const pathObj: EntryObj = {
Expand Down Expand Up @@ -597,6 +605,12 @@ export class FilesystemWeb extends WebPlugin implements FilesystemPlugin {
}
}
}

private isBase64String(str: string): boolean {
const base64regex =
/^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
return base64regex.test(str);
}
}

interface EntryObj {
Expand Down