-
Notifications
You must be signed in to change notification settings - Fork 3.4k
base64_encode embedded data files #14526
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,7 @@ | |
subdir\file, in JS it will be subdir/file. For simplicity we treat the web platform as a *NIX. | ||
""" | ||
|
||
import base64 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice that python makes this easy... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah is was rather easy to encode the files. Adding decoding was a little trickier but not by much, it's a small change with a visible impact. |
||
import os | ||
import sys | ||
import shutil | ||
|
@@ -95,6 +96,11 @@ | |
new_data_files = [] | ||
|
||
|
||
def base64_encode(b): | ||
b64 = base64.b64encode(b) | ||
return b64.decode('ascii') | ||
|
||
|
||
def has_hidden_attribute(filepath): | ||
"""Win32 code to test whether the given file has the hidden property set.""" | ||
|
||
|
@@ -464,18 +470,9 @@ def was_seen(name): | |
basename = os.path.basename(filename) | ||
if file_['mode'] == 'embed': | ||
# Embed | ||
data = list(bytearray(utils.read_binary(file_['srcpath']))) | ||
code += '''var fileData%d = [];\n''' % counter | ||
if data: | ||
parts = [] | ||
chunk_size = 10240 | ||
start = 0 | ||
while start < len(data): | ||
parts.append('''fileData%d.push.apply(fileData%d, %s);\n''' | ||
% (counter, counter, str(data[start:start + chunk_size]))) | ||
start += chunk_size | ||
code += ''.join(parts) | ||
code += ('''Module['FS_createDataFile']('%s', '%s', fileData%d, true, true, false);\n''' | ||
data = base64_encode(utils.read_binary(file_['srcpath'])) | ||
code += '''var fileData%d = '%s';\n''' % (counter, data) | ||
code += ('''Module['FS_createDataFile']('%s', '%s', decodeBase64(fileData%d), true, true, false);\n''' | ||
% (dirname, basename, counter)) | ||
counter += 1 | ||
elif file_['mode'] == 'preload': | ||
|
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.
How about replacing this specific change with this:
?
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 only reason I didn't do it that way is because using
SUPPORT_BASE64_EMBEDDING
has side effects which I'm not sure are needed in this case. Specifically, enablingSUPPORT_BASE64_EMBEDDING
adds some calls in the code totryParseAsDataURI
, which wouldn't be called otherwise.However, if that's OK, then sure,it's easier to just enable
SUPPORT_BASE64_EMBEDDING
👍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.
Oh, very good point. Yes, thinking on this more carefully, I agree with you.