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 4 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
30 changes: 25 additions & 5 deletions filesystem/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,14 @@ export class FilesystemWeb extends WebPlugin implements FilesystemPlugin {
*/
async writeFile(options: WriteFileOptions): Promise<WriteFileResult> {
const path: string = this.getPath(options.directory, options.path);
const data = options.data;
let data = options.data;
const encoding = options.encoding;
const doRecursive = options.recursive;

const occupiedEntry = (await this.dbRequest('get', [path])) as EntryObj;
if (occupiedEntry && occupiedEntry.type === 'directory')
throw Error('The supplied path is a directory.');

const encoding = options.encoding;
const parentPath = path.substr(0, path.lastIndexOf('/'));

const parentEntry = (await this.dbRequest('get', [parentPath])) as EntryObj;
Expand All @@ -192,6 +192,13 @@ export class FilesystemWeb extends WebPlugin implements FilesystemPlugin {
});
}
}

if (!encoding) {
data = data.indexOf(',') >= 0 ? data.split(',')[1] : data;
if (!this.isBase64String(data))
throw Error('The supplied data is not valid base64 content.');
}

const now = Date.now();
const pathObj: EntryObj = {
path: path,
Expand All @@ -200,7 +207,7 @@ export class FilesystemWeb extends WebPlugin implements FilesystemPlugin {
size: data.length,
ctime: now,
mtime: now,
content: !encoding && data.indexOf(',') >= 0 ? data.split(',')[1] : data,
content: data,
};
await this.dbRequest('put', [pathObj]);
return {
Expand All @@ -216,7 +223,7 @@ export class FilesystemWeb extends WebPlugin implements FilesystemPlugin {
async appendFile(options: AppendFileOptions): Promise<void> {
const path: string = this.getPath(options.directory, options.path);
let data = options.data;
// const encoding = options.encoding;
const encoding = options.encoding;
const parentPath = path.substr(0, path.lastIndexOf('/'));

const now = Date.now();
Expand All @@ -239,8 +246,15 @@ export class FilesystemWeb extends WebPlugin implements FilesystemPlugin {
}
}

if (!encoding && !this.isBase64String(data))
throw Error('The supplied data is not valid base64 content.');

if (occupiedEntry !== undefined) {
data = occupiedEntry.content + data;
if (occupiedEntry.content !== undefined && !encoding) {
data = btoa(atob(occupiedEntry.content) + atob(data));
} else {
data = occupiedEntry.content + data;
}
ctime = occupiedEntry.ctime;
}
const pathObj: EntryObj = {
Expand Down Expand Up @@ -597,6 +611,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