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

ERP5Storage: Add range and blob support

parent 9f23bfd0
......@@ -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 (this._default_view_reference === undefined) {
throw new jIO.util.jIOError(
......@@ -335,23 +337,73 @@
if (action.indexOf(this._url) === 0) {
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax({
var start,
end,
range,
request_options = {
"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) {
var result = JSON.parse(evt.target.responseText);
var content_type = evt.target.getResponseHeader("Content-Type");
if (content_type === "application/json") {
return new RSVP.Queue()
.push(function () {
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": evt.target.getResponseHeader("Content-Type")}
{"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,
400);
};
......
......@@ -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
/////////////////////////////////////////////////////////////////
......
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