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*/
/*global RSVP, FileReader*/
// JIO AttachAsProperty Storage Description :
// {
/*global RSVP, FileReader, Blob*/
//JIO AttachAsProperty Storage Description :
//description = {
// type: "attachasproperty",
// map: {
// attach_id: 'property_ud',
// attach1_id: 'property1_ud',
// attach_id: {
// // name of property containing body of attachment
// body_name: 'property_id',
// // name of property containing content_type of attachment
// content_type_name: 'content_type'
// },
// sub_storage: {
// }
// attach1_id: {
// body_name: 'property1_id',
// content_type_name: 'content_type'
// }
// },
// sub_storage: {}
//};
(function (jIO, RSVP) {
"use strict";
......@@ -36,21 +43,24 @@
sub_storage = this._sub_storage;
return this._sub_storage.get.apply(this._sub_storage, arguments)
.push(function (result) {
var property_name,
arg_list = [],
var arg_list = [],
attach_name;
for (attach_name in map) {
if (map.hasOwnProperty(attach_name)) {
property_name = map[attach_name] || attach_name;
arg_list.push(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)
.push(function (value) {
if (content_type_name) {
result[content_type_name] = value.type;
}
return jIO.util.readBlobAsText(value)
.then(function (r) {
result[property_name] = r.target.result;
result[body_name] = r.target.result;
});
})
.push(undefined, function (error) {
......@@ -67,23 +77,38 @@
AttachAsProperty.prototype.put = function (key, value_to_save) {
var attach_name,
args_list = [],
property_name,
body_name,
content_type_name,
content_type,
value,
sub_storage = this._sub_storage;
for (attach_name in this.map) {
if (this.map.hasOwnProperty(attach_name)) {
property_name = this.map[attach_name] || attach_name;
value = value_to_save[property_name];
body_name = this.map[attach_name].body_name || attach_name;
value = value_to_save[body_name];
if (value !== undefined) {
delete value_to_save[property_name];
args_list.push([attach_name, value]);
content_type_name = this.map[attach_name].content_type_name;
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)
.push(function () {
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