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

Encode image to data:URL : Use content type from HTTP headers. #285

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions emmet/emmet-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9835,7 +9835,7 @@ emmet.exec(function(require, _) {
var editorFile = editor.getFilePath();
var defaultMimeType = 'application/octet-stream';

if (editorFile === null) {
if (!/^https?:\/\//.test(imgPath) && editorFile === null) {
throw "You should save your file before using this action";
}

Expand All @@ -9845,7 +9845,7 @@ emmet.exec(function(require, _) {
throw "Can't find " + imgPath + ' file';
}

file.read(realImgPath, function(err, content) {
file.read(realImgPath, function(err, content, ct) {
if (err) {
throw 'Unable to read ' + realImgPath + ': ' + err;
}
Expand All @@ -9855,7 +9855,7 @@ emmet.exec(function(require, _) {
throw "Can't encode file content to base64";
}

b64 = 'data:' + (actionUtils.mimeTypes[String(file.getExt(realImgPath))] || defaultMimeType) +
b64 = 'data:' + (ct || actionUtils.mimeTypes[String(file.getExt(realImgPath))] || defaultMimeType) +
';base64,' + b64;

editor.replaceContent('$0' + b64, pos, pos + imgPath.length);
Expand Down
8 changes: 4 additions & 4 deletions emmet/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ def is_url(path):

def read_http(url, size=-1, mode=None):
response = urllib2.urlopen(url, timeout=5)
return response.read(size)
return response.read(size), response.info().gettype()

def read_file(path, size=-1, mode='rb'):
kwargs = {}
if is_python3 and 'b' not in mode:
kwargs['encoding'] = 'utf-8'

with open(path, mode, **kwargs) as fp:
return fp.read(size)
return fp.read(size), None

class File():
def __init__(self):
Expand All @@ -48,7 +48,7 @@ def read(self, path, size, callback=None):
"""

try:
content = self._read(path, size)
content, ct = self._read(path, size)

# return as array of character codes since PyV8 may corrupt
# binary data when python string is translated into JS string
Expand All @@ -60,7 +60,7 @@ def read(self, path, size, callback=None):
except Exception as e:
return callback(str(e), None)

callback(None, content)
callback(None, content, ct)

def read_text(self, path, size, callback=None):
"""
Expand Down
6 changes: 3 additions & 3 deletions emmet/python-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ emmet.define('file', function(require, _) {
var params = this._parseParams(arguments);

try {
pyFile.read(params.path, params.size, function(err, content) {
pyFile.read(params.path, params.size, function(err, content, ct) {
if (err) {
return params.callback(err, content);
return params.callback(err, content, ct);
}

content = _.map(content || [], function(b) {
return String.fromCharCode(b);
}).join('');
params.callback(null, content);
params.callback(null, content, ct);
});
} catch(e) {
params.callback(e);
Expand Down