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(electron): correctly read/write file with no encoding #1905

Merged
merged 3 commits into from
Aug 28, 2019
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
17 changes: 11 additions & 6 deletions electron/src/electron/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,35 @@ export class FilesystemPluginElectron extends WebPlugin implements FilesystemPlu
if(Object.keys(this.fileLocations).indexOf(options.directory) === -1)
reject(`${options.directory} is currently not supported in the Electron implementation.`);
let lookupPath = this.fileLocations[options.directory] + options.path;
this.NodeFS.readFile(lookupPath, options.encoding, (err:any, data:any) => {
this.NodeFS.readFile(lookupPath, options.encoding || 'binary', (err:any, data:any) => {
if(err) {
reject(err);
return;
}

resolve({data});
resolve({ data: options.encoding ? data : data.toString('base64') });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

write is fine now, but read is returning the converted string, not the original base64 written string.
In example, if I write "e", in the file it's written as "hello", and if I read it, I get "hello" too, but I should get "e" as that's what I wrote. On Android and iOS it works like that.
So I think it should be something like this:

Suggested change
resolve({ data: options.encoding ? data : data.toString('base64') });
resolve({ data: options.encoding ? data : Buffer.from(data).toString('base64') });

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It bugs me because fs.readFile returns a Buffer to the callback so I shouldn't have wrap it again in a Buffer 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it returns a string if you pass 'binary' as encoding, but returns a Buffer if you pass null as encoding. At least that's what I'm getting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The trick was to add 'binary' as encoding when using Buffer.from. Works now! But I also saw that the data:application/pdf;base64, is not stripped on web. Should I fix it in this PR? Or should I make another one?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better a new one

});
});
}

writeFile(options: FileWriteOptions): Promise<FileWriteResult> {
return new Promise((resolve, reject) => {
if(Object.keys(this.fileLocations).indexOf(options.directory) === -1)
if (Object.keys(this.fileLocations).indexOf(options.directory) === -1)
reject(`${options.directory} is currently not supported in the Electron implementation.`);
let lookupPath = this.fileLocations[options.directory] + options.path;
this.NodeFS.writeFile(lookupPath, options.data, options.encoding, (err:any) => {
if(err) {
let data: (Buffer | string) = options.data;
if (!options.encoding) {
const base64Data = options.data.indexOf(',') >= 0 ? options.data.split(',')[1] : options.data;
data = Buffer.from(base64Data, 'base64');
}
this.NodeFS.writeFile(lookupPath, data, options.encoding || 'binary', (err: any) => {
if (err) {
reject(err);
return;
}

resolve();
})
});
});
}

Expand Down