Use the cell ‘attachments’ property instead of ‘metadata.attachments’.

Use the mime-bundle storage format to store the attachments
This commit is contained in:
Julien Rebetez 2015-10-20 11:15:07 +02:00
parent 3462b34fca
commit 398c90b0aa
3 changed files with 44 additions and 12 deletions

View File

@ -788,6 +788,23 @@ define([
return MathJax.Hub.Queue(["Typeset", MathJax.Hub, this]);
});
};
var parse_b64_data_uri = function(uri) {
/**
* Parses a base64 encoded data-uri to extract mimetype and the
* base64 string.
*
* For example, given 'data:image/png;base64,iVBORw', it will return
* ["image/png", "iVBORw"]
*
* Parameters
*/
var regex = /^data:(.+\/.+);base64,(.*)$/;
var matches = uri.match(regex);
var mime = matches[1];
var b64_data = matches[2];
return [mime, b64_data];
};
var time = {};
time.milliseconds = {};
@ -877,6 +894,7 @@ define([
resolve_promises_dict: resolve_promises_dict,
reject: reject,
typeset: typeset,
parse_b64_data_uri: parse_b64_data_uri,
time: time,
format_datetime: format_datetime,
datetime_sort_helper: datetime_sort_helper,

View File

@ -70,6 +70,8 @@ define([
}
});
this.attachments = {};
// backward compat.
Object.defineProperty(this, 'cm_config', {
get: function() {
@ -472,6 +474,8 @@ define([
var data = {};
// deepcopy the metadata so copied cells don't share the same object
data.metadata = JSON.parse(JSON.stringify(this.metadata));
// same for attachments
data.attachments = JSON.parse(JSON.stringify(this.attachments));
data.cell_type = this.cell_type;
return data;
};
@ -484,6 +488,9 @@ define([
if (data.metadata !== undefined) {
this.metadata = data.metadata;
}
if (data.attachments !== undefined) {
this.attachments = data.attachments;
}
};

View File

@ -263,8 +263,8 @@ define([
// Inline images insertion. When a user drops an image in a markdown
// cell, we do the following :
// - We insert the base64-encoded image into the cell metadata
// attachments directory, keyed by the filename.
// - We insert the base64-encoded image into the cell attachments
// directory, keyed by the filename.
// - We insert an img tag with a 'nbdata' src that refers to the
// attachments entry.
//
@ -284,14 +284,19 @@ define([
var reader = new FileReader;
reader.onloadend = function() {
var img_md = '<img width="200px" height="200px" src="nbdata:' + key + '" />';
if (that.metadata.attachments === undefined) {
that.metadata.attachments = {};
if (that.attachments === undefined) {
that.attachments = {};
}
that.metadata.attachments[key] = {
'data': reader.result,
'mime': file.type
that.attachments[key] = {};
// Strip the "data:image/png;base64," prefix from the data-url
// to turn it into a base64 encoded string
var d = utils.parse_b64_data_uri(reader.result);
if (file.type != d[0]) {
// TODO(julienr): Not sure what we should do in this case
console.log('File type (' + file.type + ') != data-uri ' +
'type (' + d[0] + ')');
}
//var img_md = '<img height="200px" src="' + reader.result + '" />';
that.attachments[key][file.type] = [d[1]];
that.code_mirror.replaceRange(img_md, pos);
}
reader.readAsDataURL(file);
@ -339,10 +344,12 @@ define([
html.find('img[src^="nbdata:"]').each(function (i, h) {
h = $(h);
var key = h.attr('src').replace(/^nbdata:/, '');
if (that.metadata.attachments !== undefined &&
key in that.metadata.attachments) {
var att = that.metadata.attachments[key];
h.attr('src', att['data']);
if (that.attachments !== undefined &&
key in that.attachments) {
var att = that.attachments[key];
var mime = Object.keys(att)[0];
h.attr('src', 'data:' + mime + ';base64,' + att[mime][0]);
}
});
that.set_rendered(html);