Commit 6bc97e8e authored by Klaus Wölfel's avatar Klaus Wölfel

ERP5Storage: Add range and blob support

parent 9f23bfd0
...@@ -296,8 +296,10 @@ ...@@ -296,8 +296,10 @@
}); });
}; };
ERP5Storage.prototype.getAttachment = function (id, action) { ERP5Storage.prototype.getAttachment = function (id, action, options) {
if (options === undefined) {
options = {};
}
if (action === "view") { if (action === "view") {
if (this._default_view_reference === undefined) { if (this._default_view_reference === undefined) {
throw new jIO.util.jIOError( throw new jIO.util.jIOError(
...@@ -335,21 +337,71 @@ ...@@ -335,21 +337,71 @@
if (action.indexOf(this._url) === 0) { if (action.indexOf(this._url) === 0) {
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return jIO.util.ajax({ var start,
"type": "GET", end,
"url": action, range,
"xhrFields": { request_options = {
withCredentials: true "type": "GET",
"dataType": "blob",
"url": action,
"xhrFields": {
withCredentials: true
}
};
if (options.start !== undefined || options.end !== undefined) {
start = options.start || 0;
end = options.end;
if (end !== undefined && end < 0) {
throw new jIO.util.jIOError("end must be positive",
400);
} }
}); if (start < 0) {
range = "bytes=" + start;
} else if (end === undefined) {
range = "bytes=" + start + "-";
} else {
if (start > end) {
throw new jIO.util.jIOError("start is greater than end",
400);
}
range = "bytes=" + start + "-" + end;
}
request_options.headers = {Range: range};
}
return jIO.util.ajax(request_options);
}) })
.push(function (evt) { .push(function (evt) {
var result = JSON.parse(evt.target.responseText); var content_type = evt.target.getResponseHeader("Content-Type");
result._id = id; if (content_type === "application/json") {
return new Blob( return new RSVP.Queue()
[JSON.stringify(result)], .push(function () {
{"type": evt.target.getResponseHeader("Content-Type")} if (evt.target.responseText === undefined) {
); return new RSVP.Queue()
.push(function () {
return jIO.util.readBlobAsText(evt.target.response);
})
.push(function (evt) {
return evt.target.result;
});
}
return evt.target.responseText;
})
.push(function (response_text) {
var result = JSON.parse(response_text);
result._id = id;
return new Blob(
[JSON.stringify(result)],
{"type": content_type}
);
});
}
if (evt.target.response === undefined) {
return new Blob(
[evt.target.responseText],
{"type": content_type}
);
}
return evt.target.response;
}); });
} }
throw new jIO.util.jIOError("ERP5: not support get attachment: " + action, throw new jIO.util.jIOError("ERP5: not support get attachment: " + action,
......
...@@ -917,6 +917,75 @@ ...@@ -917,6 +917,75 @@
}); });
}); });
test("getAttachment: non-JSON callable url", function () {
var callable_url = domain + "foobar",
id = "fake",
server = this.server;
this.server.respondWith("GET", callable_url, [200, {
"Content-Type": "text/plain"
}, "foo\nbaré"]);
stop();
expect(8);
this.jio.getAttachment(id, callable_url)
.then(function (result) {
equal(server.requests.length, 1);
equal(server.requests[0].method, "GET");
equal(server.requests[0].url, callable_url);
equal(server.requests[0].requestBody, undefined);
equal(server.requests[0].withCredentials, true);
ok(result instanceof Blob, "Data is Blob");
deepEqual(result.type, "text/plain", "Check mimetype");
return jIO.util.readBlobAsText(result);
})
.then(function (result) {
var expected = "foo\nbaré";
equal(result.target.result, expected, "Attachment correctly fetched");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("getAttachment: slicing parameters", function () {
var callable_url = domain + "foobar",
id = "fake",
server = this.server;
this.server.respondWith("GET", callable_url, [200, {
"Content-Type": "application/octet-stream"
}, "foo\nbaré"]);
stop();
expect(8);
this.jio.getAttachment(id, callable_url,
{start: 123, end: 456})
.then(function (result) {
equal(server.requests.length, 1);
equal(server.requests[0].method, "GET");
equal(server.requests[0].url, callable_url);
equal(server.requests[0].requestBody, undefined);
equal(server.requests[0].withCredentials, true);
equal(server.requests[0].requestHeaders.Range, "bytes=123-456");
ok(result instanceof Blob, "Data is Blob");
deepEqual(result.type, "application/octet-stream", "Check mimetype");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// erp5Storage.hasCapacity // erp5Storage.hasCapacity
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
......
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