-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR, but it doesn't build because data is string and don't let you switch it to Buffer (line 65).
Also you are always removing whatever there is before the ,
, even if it has encoding, that will remove part of texts that have ,
on it.
The code should look like:
let data = options.data;
if (!options.encoding) {
data = options.data.indexOf(',') >= 0 ? options.data.split(',')[1] : options.data;
}
let writeData = options.encoding ? data : Buffer.from(data, 'base64');
this.NodeFS.writeFile(lookupPath, writeData, options.encoding || 'binary', (err:any) => {
electron/src/electron/filesystem.ts
Outdated
if(err) { | ||
reject(err); | ||
return; | ||
} | ||
|
||
resolve({data}); | ||
resolve({ data: options.encoding ? data : data.toString('base64') }); |
There was a problem hiding this comment.
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:
resolve({ data: options.encoding ? data : data.toString('base64') }); | |
resolve({ data: options.encoding ? data : Buffer.from(data).toString('base64') }); |
There was a problem hiding this comment.
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 🤔
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
better a new one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added another comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see any difference in using Buffer.from(data, 'binary').toString('base64')
as you did or Buffer.from(data).toString('base64')
as I suggested, both work for me, so I'll merge, thanks!
I was working with the Filesystem plugin and some data URL files encoded in base64 and I stumbled upon the fact that Electron bridge is not implemented like the Android and iOS one. I simply cannot store a PDF file because it always encrypt as UTF8 and break the file.
On iOS and Android, the default encoding when writing files is
base64
. If a data URL is detected it deletes its header before converting it tobinary
. When reading, if no encoding is given, the same thing happens the other way, it convertsbinary
tobase64
.This is a breaking change for Electron users but ATM, Electron filesystem plugin does not behave at all like iOS and Android.