Commit de24c705 authored by Sven Franck's avatar Sven Franck

wip dirty path to dropbox api v2

parent 18cf665b
......@@ -7,27 +7,18 @@
* JIO Dropbox Storage. Type = "dropbox".
* Dropbox "database" storage.
*/
/*global Blob, jIO, RSVP, UriTemplate*/
/*global Blob, jIO, RSVP*/
/*jslint nomen: true*/
(function (jIO, RSVP, Blob, UriTemplate) {
(function (jIO, RSVP, Blob, JSON) {
"use strict";
var UPLOAD_URL = "https://content.dropboxapi.com/1/files_put/" +
"{+root}{+id}{+name}{?access_token}",
upload_template = UriTemplate.parse(UPLOAD_URL),
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/';
METADATA_URL = "https://api.dropboxapi.com/1/metadata" +
"{/root}{+id}{?access_token}",
metadata_template = UriTemplate.parse(METADATA_URL);
var GET_URL = "https://content.dropboxapi.com/2/files/download",
UPLOAD_URL = "https://content.dropboxapi.com/2/files/upload",
REMOVE_URL = "https://api.dropboxapi.com/2/files/delete_v2",
CREATE_DIR_URL = "https://api.dropboxapi.com/2/files/create_folder_v2",
METADATA_URL = "https://api.dropboxapi.com/2/files/get_metadata",
LIST_FOLDER_URL = "https://api.dropboxapi.com/2/files/list_folder",
LIST_MORE_URL = "https://api.dropboxapi.com/2/files/list_folder/continue";
function restrictDocumentId(id) {
if (id.indexOf("/") !== 0) {
......@@ -40,7 +31,6 @@
}
return id;
}
function restrictAttachmentId(id) {
if (id.indexOf("/") !== -1) {
throw new jIO.util.jIOError("attachment " + id + " is forbidden",
......@@ -48,6 +38,75 @@
}
}
// list_folder root must be "" in api-v2
function restrictRoot(id) {
if (id === "/") {
return "";
}
return id;
}
function listPage(result, token, id, cursor) {
var data = {
"path": id,
"recursive": false,
"include_media_info": false,
"include_deleted": false,
"include_has_explicit_shared_members": false,
"include_mounted_folders": true
};
if (cursor) {
data = {"cursor": cursor};
}
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax({
type: "POST",
url: cursor ? LIST_MORE_URL : LIST_FOLDER_URL,
headers: {
"Authorization": "Bearer " + token,
"Content-Type": "application/json"
},
data: JSON.stringify(data)
});
})
.push(function (evt) {
var obj = JSON.parse(evt.target.response || evt.target.responseText),
i;
if (obj.has_more) {
result.has_more = obj.has_more;
}
if (obj.entries.length === 1 && obj.entries[0][".tag"] !== "folder") {
throw new jIO.util.jIOError("Not a directory: " + id, 404);
}
for (i = 0; i < obj.entries.length; i += 1) {
if (obj.entries[i][".tag"] === "file") {
result[obj.entries[i].path_display.split("/").pop()] = {};
}
}
return result;
}, function (error) {
if (error.target !== undefined && error.target.status === 409) {
throw new jIO.util.jIOError("Cannot find document: " + id, 404);
}
throw error;
});
}
function recursiveAllAttachments(result, accessToken, id) {
var cursor = result.cursor;
return new RSVP.Queue()
.push(function () {
return listPage(result, accessToken, id);
})
.push(function () {
if (result.has_more) {
return recursiveAllAttachments(result, accessToken, id, cursor);
}
return result;
});
}
/**
* The JIO Dropbox Storage extension
*
......@@ -59,12 +118,7 @@
throw new TypeError("Access Token' must be a string " +
"which contains more than one character.");
}
if (typeof spec.root !== 'string' || !spec.root ||
(spec.root !== "dropbox" && spec.root !== "sandbox")) {
throw new TypeError("root must be 'dropbox' or 'sandbox'");
}
this._access_token = spec.access_token;
this._root = spec.root;
}
DropboxStorage.prototype.put = function (id, param) {
......@@ -79,11 +133,12 @@
.push(function () {
return jIO.util.ajax({
type: "POST",
url: create_dir_template.expand({
access_token: that._access_token,
root: that._root,
path: id
})
url: CREATE_DIR_URL,
headers: {
"Authorization": "Bearer " + that._access_token,
"Content-Type": "application/json"
},
data: JSON.stringify({"path": id, "autorename": false})
});
})
.push(undefined, function (err) {
......@@ -100,17 +155,17 @@
id = restrictDocumentId(id);
return jIO.util.ajax({
type: "POST",
url: remote_template.expand({
access_token: this._access_token,
root: this._root,
path: id
})
url: REMOVE_URL,
headers: {
"Authorization": "Bearer " + that._access_token,
"Content-Type": "application/json"
},
data: JSON.stringify({"path": id})
});
};
DropboxStorage.prototype.get = function (id) {
var that = this;
if (id === "/") {
return {};
}
......@@ -119,23 +174,24 @@
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax({
type: "GET",
url: metadata_template.expand({
access_token: that._access_token,
root: that._root,
id: id
})
type: "POST",
url: METADATA_URL,
headers: {
"Authorization": "Bearer " + that._access_token,
"Content-Type": "application/json"
},
data: JSON.stringify({"path": id})
});
})
.push(function (evt) {
var obj = JSON.parse(evt.target.response ||
evt.target.responseText);
if (obj.is_dir) {
if (obj[".tag"] === "folder") {
return {};
}
throw new jIO.util.jIOError("Not a directory: " + id, 404);
}, function (error) {
if (error.target !== undefined && error.target.status === 404) {
if (error.target !== undefined && error.target.status === 409) {
throw new jIO.util.jIOError("Cannot find document: " + id, 404);
}
throw error;
......@@ -143,40 +199,7 @@
};
DropboxStorage.prototype.allAttachments = function (id) {
var that = this;
id = restrictDocumentId(id);
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax({
type: "GET",
url: metadata_template.expand({
access_token: that._access_token,
root: that._root,
id: id
})
});
})
.push(function (evt) {
var obj = JSON.parse(evt.target.response || evt.target.responseText),
i,
result = {};
if (!obj.is_dir) {
throw new jIO.util.jIOError("Not a directory: " + id, 404);
}
for (i = 0; i < obj.contents.length; i += 1) {
if (!obj.contents[i].is_dir) {
result[obj.contents[i].path.split("/").pop()] = {};
}
}
return result;
}, function (error) {
if (error.target !== undefined && error.target.status === 404) {
throw new jIO.util.jIOError("Cannot find document: " + id, 404);
}
throw error;
});
return recursiveAllAttachments({}, this._access_token, restrictRoot(id));
};
//currently, putAttachment will fail with files larger than 150MB,
......@@ -187,18 +210,22 @@
//to an inexisting foler.
DropboxStorage.prototype.putAttachment = function (id, name, blob) {
var that = this;
id = restrictDocumentId(id);
restrictAttachmentId(name);
return jIO.util.ajax({
type: "PUT",
url: upload_template.expand({
root: this._root,
id: id,
name: name,
access_token: this._access_token
}),
dataType: blob.type,
type: "POST",
url: UPLOAD_URL,
headers: {
"Authorization": "Bearer " + that._access_token,
"Content-Type": "application/octet-stream",
"Dropbox-API-Arg": JSON.stringify({
"path": id + name,
"mode": "overwrite",
"autorename": true,
"mute": false})
},
data: blob
});
};
......@@ -212,14 +239,13 @@
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax({
type: "GET",
url: GET_URL,
type: "POST",
dataType: "blob",
url: get_template.expand({
root: that._root,
id: id,
name: name,
access_token: that._access_token
})
headers: {
"Authorization": "Bearer " + that._access_token,
"Dropbox-API-Arg": JSON.stringify({"path": id + name})
}
});
})
.push(function (evt) {
......@@ -229,7 +255,7 @@
"application/octet-stream"}
);
}, function (error) {
if (error.target !== undefined && error.target.status === 404) {
if (error.target !== undefined && error.target.status === 409) {
throw new jIO.util.jIOError("Cannot find attachment: " +
id + ", " + name, 404);
}
......@@ -248,14 +274,15 @@
.push(function () {
return jIO.util.ajax({
type: "POST",
url: remote_template.expand({
access_token: that._access_token,
root: that._root,
path: id + name
})
url: REMOVE_URL,
headers: {
"Authorization": "Bearer " + that._access_token,
"Content-Type": "application/json"
},
data: JSON.stringify({"path": id + name})
});
}).push(undefined, function (error) {
if (error.target !== undefined && error.target.status === 404) {
if (error.target !== undefined && error.target.status === 409) {
throw new jIO.util.jIOError("Cannot find attachment: " +
id + ", " + name, 404);
}
......@@ -265,4 +292,4 @@
jIO.addStorage('dropbox', DropboxStorage);
}(jIO, RSVP, Blob, UriTemplate));
}(jIO, RSVP, Blob, JSON));
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