Commit c43f7171 authored by Romain Courteaud's avatar Romain Courteaud

DropboxStorage: multiple fixes

Never build URL by concatenating strings, as it does not correctly handle escape.

Remove useless requests which will not prevent raise condition.
parent b681a950
...@@ -7,16 +7,23 @@ ...@@ -7,16 +7,23 @@
* JIO Dropbox Storage. Type = "dropbox". * JIO Dropbox Storage. Type = "dropbox".
* Dropbox "database" storage. * Dropbox "database" storage.
*/ */
/*global FormData, btoa, Blob, DOMParser, define, jIO, RSVP, ProgressEvent */ /*global Blob, jIO, RSVP, UriTemplate*/
/*js2lint nomen: true, unparam: true, bitwise: true */ /*jslint nomen: true*/
/*jslint nomen: true, unparam: true*/
(function (jIO, RSVP, DOMParser, Blob) { (function (jIO, RSVP, Blob, UriTemplate) {
"use strict"; "use strict";
var UPLOAD_URL = "https://content.dropboxapi.com/1/files_put/", var UPLOAD_URL = "https://content.dropboxapi.com/1/files_put/" +
CREATE_DIR_URL = "https://api.dropboxapi.com/1/fileops/create_folder", "{+root}{+id}{+name}{?access_token}",
REMOVE_URL = "https://api.dropboxapi.com/1/fileops/delete/", upload_template = UriTemplate.parse(UPLOAD_URL),
GET_URL = "https://content.dropboxapi.com/1/files"; CREATE_DIR_URL = "https://api.dropboxapi.com/1/fileops/create_folder" +
"{?access_token,root,path}",
create_dir_template = UriTemplate.parse(CREATE_DIR_URL),
REMOVE_URL = "https://api.dropboxapi.com/1/fileops/delete/" +
"{?access_token,root,path}",
remote_template = UriTemplate.parse(REMOVE_URL),
GET_URL = "https://content.dropboxapi.com/1/files" +
"{/root,id}{+name}{?access_token}",
get_template = UriTemplate.parse(GET_URL);
//LIST_URL = 'https://api.dropboxapi.com/1/metadata/sandbox/'; //LIST_URL = 'https://api.dropboxapi.com/1/metadata/sandbox/';
function restrictDocumentId(id) { function restrictDocumentId(id) {
...@@ -68,10 +75,12 @@ ...@@ -68,10 +75,12 @@
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return jIO.util.ajax({ return jIO.util.ajax({
"type": "POST", type: "POST",
"url": CREATE_DIR_URL + url: create_dir_template.expand({
'?access_token=' + that._access_token + access_token: that._access_token,
'&root=' + that._root + '&path=' + id root: that._root,
path: id
})
}); });
}) })
.push(undefined, function (err) { .push(undefined, function (err) {
...@@ -84,25 +93,20 @@ ...@@ -84,25 +93,20 @@
}; };
DropboxStorage.prototype.remove = function (id) { DropboxStorage.prototype.remove = function (id) {
var that = this;
id = restrictDocumentId(id); id = restrictDocumentId(id);
return new RSVP.Queue()
.push(function () {
return that.get(id);
})
.push(function () {
return jIO.util.ajax({ return jIO.util.ajax({
"type": "POST", type: "POST",
"url": REMOVE_URL + url: remote_template.expand({
'?access_token=' + that._access_token + access_token: this._access_token,
'&root=' + that._root + '&path=' + id root: this._root,
}); path: id
})
}); });
}; };
DropboxStorage.prototype.get = function (id) { DropboxStorage.prototype.get = function (id) {
var that = this, var that = this;
obj;
if (id === "/") { if (id === "/") {
return {}; return {};
} }
...@@ -117,16 +121,16 @@ ...@@ -117,16 +121,16 @@
'?access_token=' + that._access_token '?access_token=' + that._access_token
}); });
}) })
.push(function (xml) { .push(function (evt) {
obj = JSON.parse(xml.target.response || xml.target.responseText); var obj = JSON.parse(evt.target.response ||
if (obj.is_dir === true) { evt.target.responseText);
if (obj.is_dir) {
return {}; return {};
} }
throw new jIO.util.jIOError("cannot load" + id + throw new jIO.util.jIOError("Not a directory: " + id, 404);
". Invalid HTTP", 404);
}, function (error) { }, function (error) {
if (error !== undefined && error.target.status === 404) { if (error.target !== undefined && error.target.status === 404) {
throw new jIO.util.jIOError("Cannot find document", 404); throw new jIO.util.jIOError("Cannot find document: " + id, 404);
} }
throw error; throw error;
}); });
...@@ -134,12 +138,7 @@ ...@@ -134,12 +138,7 @@
DropboxStorage.prototype.allAttachments = function (id) { DropboxStorage.prototype.allAttachments = function (id) {
var that = this, var that = this;
i,
title,
ret,
obj;
id = restrictDocumentId(id); id = restrictDocumentId(id);
return new RSVP.Queue() return new RSVP.Queue()
...@@ -151,23 +150,22 @@ ...@@ -151,23 +150,22 @@
'?access_token=' + that._access_token '?access_token=' + that._access_token
}); });
}) })
.push(function (xml) { .push(function (evt) {
obj = JSON.parse(xml.target.response || xml.target.responseText); var obj = JSON.parse(evt.target.response || evt.target.responseText),
if (obj.is_dir === false) { i,
throw new jIO.util.jIOError("cannot load" + id + result = {};
". Invalid HTTP", 404); if (!obj.is_dir) {
throw new jIO.util.jIOError("Not a directory: " + id, 404);
} }
ret = {};
for (i = 0; i < obj.contents.length; i += 1) { for (i = 0; i < obj.contents.length; i += 1) {
if (obj.contents[i].is_dir !== true) { if (!obj.contents[i].is_dir) {
title = obj.contents[i].path.split("/").pop(); result[obj.contents[i].path.split("/").pop()] = {};
ret[title] = {};
} }
} }
return ret; return result;
}, function (error) { }, function (error) {
if (error !== undefined && error.target.status === 404) { if (error.target !== undefined && error.target.status === 404) {
throw new jIO.util.jIOError("Cannot find document", 404); throw new jIO.util.jIOError("Cannot find document: " + id, 404);
} }
throw error; throw error;
}); });
...@@ -178,37 +176,20 @@ ...@@ -178,37 +176,20 @@
//to pass this limit, but upload process becomes more complex to implement. //to pass this limit, but upload process becomes more complex to implement.
DropboxStorage.prototype.putAttachment = function (id, name, blob) { DropboxStorage.prototype.putAttachment = function (id, name, blob) {
var that = this;
id = restrictDocumentId(id); id = restrictDocumentId(id);
restrictAttachmentId(name); restrictAttachmentId(name);
return new RSVP.Queue()
.push(function () {
return that.get(id);
})
.push(undefined, function (error) {
throw new jIO.util.jIOError("Cannot access subdocument", 404);
})
.push(function () {
return that.get(id + name + '/');
})
.push(function () {
throw new jIO.util.jIOError("Cannot overwrite directory", 404);
}, function (error) {
if ((error instanceof jIO.util.jIOError) &&
((error.message === "cannot load" + id + name + '/. Invalid HTTP')
|| (error.message === "Cannot find document"))) {
return jIO.util.ajax({ return jIO.util.ajax({
"type": "PUT", type: "PUT",
"url": UPLOAD_URL + that._root + id + name + url: upload_template.expand({
'?access_token=' + that._access_token, root: this._root,
id: id,
name: name,
access_token: this._access_token
}),
dataType: blob.type, dataType: blob.type,
data: blob data: blob
}); });
}
throw error;
});
}; };
DropboxStorage.prototype.getAttachment = function (id, name) { DropboxStorage.prototype.getAttachment = function (id, name) {
...@@ -220,19 +201,23 @@ ...@@ -220,19 +201,23 @@
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return jIO.util.ajax({ return jIO.util.ajax({
"type": "GET", type: "GET",
"url": GET_URL + "/" + that._root + "/" + id + name + url: get_template.expand({
'?access_token=' + that._access_token root: that._root,
id: id,
name: name,
access_token: that._access_token
})
}); });
}) })
.push(function (response) { .push(function (evt) {
return new Blob( return new Blob(
[response.target.response || response.target.responseText], [evt.target.response || evt.target.responseText],
{"type": response.target.getResponseHeader('Content-Type') || {"type": evt.target.getResponseHeader('Content-Type') ||
"application/octet-stream"} "application/octet-stream"}
); );
}, function (error) { }, function (error) {
if (error !== undefined && error.target.status === 404) { if (error.target !== undefined && error.target.status === 404) {
throw new jIO.util.jIOError("Cannot find attachment: " + throw new jIO.util.jIOError("Cannot find attachment: " +
id + ", " + name, 404); id + ", " + name, 404);
} }
...@@ -241,35 +226,19 @@ ...@@ -241,35 +226,19 @@
}; };
DropboxStorage.prototype.removeAttachment = function (id, name) { DropboxStorage.prototype.removeAttachment = function (id, name) {
var that = this;
id = restrictDocumentId(id); id = restrictDocumentId(id);
restrictAttachmentId(name); restrictAttachmentId(name);
return new RSVP.Queue()
.push(function () {
return that.get(id + name + '/');
})
.push(function () {
throw new jIO.util.jIOError("Cannot remove directory", 404);
}, function (error) {
if (error instanceof jIO.util.jIOError &&
error.message === "Cannot find document") {
throw new jIO.util.jIOError("Cannot find attachment: " +
id + ", " + name, 404);
}
if (error instanceof jIO.util.jIOError &&
error.message === "cannot load" + id + name + '/. Invalid HTTP') {
return jIO.util.ajax({ return jIO.util.ajax({
"type": "POST", type: "POST",
"url": REMOVE_URL + url: remote_template.expand({
'?access_token=' + that._access_token + access_token: this._access_token,
'&root=' + that._root + '&path=' + id + name root: this._root,
}); path: id + name
} })
throw error;
}); });
}; };
jIO.addStorage('dropbox', DropboxStorage); jIO.addStorage('dropbox', DropboxStorage);
}(jIO, RSVP, DOMParser, Blob)); }(jIO, RSVP, Blob, UriTemplate));
\ No newline at end of file
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