Commit 7b68148a authored by Boris Kocherov's avatar Boris Kocherov

attachaspropertystorage: allow using content_type for attachment and

fix bug
parent b7c65ab7
/*jslint nomen: true*/ /*jslint nomen: true*/
/*global RSVP, FileReader*/ /*global RSVP, FileReader, Blob*/
// JIO AttachAsProperty Storage Description : //JIO AttachAsProperty Storage Description :
// { //description = {
// type: "attachasproperty", // type: "attachasproperty",
// map: { // map: {
// attach_id: 'property_ud', // attach_id: {
// attach1_id: 'property1_ud', // // name of property containing body of attachment
// }, // body_name: 'property_id',
// sub_storage: { // // name of property containing content_type of attachment
// } // content_type_name: 'content_type'
// } // },
// attach1_id: {
// body_name: 'property1_id',
// content_type_name: 'content_type'
// }
// },
// sub_storage: {}
//};
(function (jIO, RSVP) { (function (jIO, RSVP) {
"use strict"; "use strict";
...@@ -36,21 +43,24 @@ ...@@ -36,21 +43,24 @@
sub_storage = this._sub_storage; sub_storage = this._sub_storage;
return this._sub_storage.get.apply(this._sub_storage, arguments) return this._sub_storage.get.apply(this._sub_storage, arguments)
.push(function (result) { .push(function (result) {
var property_name, var arg_list = [],
arg_list = [],
attach_name; attach_name;
for (attach_name in map) { for (attach_name in map) {
if (map.hasOwnProperty(attach_name)) { if (map.hasOwnProperty(attach_name)) {
property_name = map[attach_name] || attach_name;
arg_list.push(attach_name); arg_list.push(attach_name);
} }
} }
return RSVP.all(arg_list.map(function (attach_name) { return RSVP.all(arg_list.map(function (attach_name) {
var body_name = map[attach_name].body_name || attach_name,
content_type_name = map[attach_name].content_type_name;
return sub_storage.getAttachment(key, attach_name) return sub_storage.getAttachment(key, attach_name)
.push(function (value) { .push(function (value) {
if (content_type_name) {
result[content_type_name] = value.type;
}
return jIO.util.readBlobAsText(value) return jIO.util.readBlobAsText(value)
.then(function (r) { .then(function (r) {
result[property_name] = r.target.result; result[body_name] = r.target.result;
}); });
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
...@@ -67,23 +77,38 @@ ...@@ -67,23 +77,38 @@
AttachAsProperty.prototype.put = function (key, value_to_save) { AttachAsProperty.prototype.put = function (key, value_to_save) {
var attach_name, var attach_name,
args_list = [], args_list = [],
property_name, body_name,
content_type_name,
content_type,
value, value,
sub_storage = this._sub_storage; sub_storage = this._sub_storage;
for (attach_name in this.map) { for (attach_name in this.map) {
if (this.map.hasOwnProperty(attach_name)) { if (this.map.hasOwnProperty(attach_name)) {
property_name = this.map[attach_name] || attach_name; body_name = this.map[attach_name].body_name || attach_name;
value = value_to_save[property_name]; value = value_to_save[body_name];
if (value !== undefined) { if (value !== undefined) {
delete value_to_save[property_name]; content_type_name = this.map[attach_name].content_type_name;
args_list.push([attach_name, value]); if (content_type_name) {
content_type = value_to_save[content_type_name];
} else {
content_type = null;
}
delete value_to_save[body_name];
args_list.push([attach_name, value, content_type]);
} }
} }
} }
return sub_storage.put.apply(sub_storage, arguments) return sub_storage.put.apply(sub_storage, arguments)
.push(function () { .push(function () {
return RSVP.all(args_list.map(function (a) { return RSVP.all(args_list.map(function (a) {
return sub_storage.putAttachment(key, a[0], a[1]); var content_type = a[2],
body;
if (content_type) {
body = new Blob([a[1]], {type: content_type});
} else {
body = a[1];
}
return sub_storage.putAttachment(key, a[0], body);
})); }));
}); });
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment