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

Conversation

D34THWINGS
Copy link
Contributor

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 to binary. When reading, if no encoding is given, the same thing happens the other way, it converts binary to base64.

This is a breaking change for Electron users but ATM, Electron filesystem plugin does not behave at all like iOS and Android.

Copy link
Member

@jcesarmobile jcesarmobile left a 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) => {

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

Copy link
Member

@jcesarmobile jcesarmobile left a comment

Choose a reason for hiding this comment

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

Added another comment

Copy link
Member

@jcesarmobile jcesarmobile left a 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!

@jcesarmobile jcesarmobile changed the title Fixing Electron filesystem implementation fix(electron): correctly read/write file with no encoding Aug 28, 2019
@jcesarmobile jcesarmobile merged commit 3266dee into ionic-team:master Aug 28, 2019
@D34THWINGS D34THWINGS deleted the electron-fs branch August 28, 2019 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants