Commit 8dcd2eac authored by lucas.parsy's avatar lucas.parsy

refactoring of gdrivestorage.js

various conditions optimized
added compliance with future tests.

added error message when giving non-boolean value
for "trashed" value at creation of gdrive storage.
parent ee13ac63
...@@ -61,7 +61,7 @@ ...@@ -61,7 +61,7 @@
}); });
}) })
.push(function (data) { .push(function (data) {
obj = JSON.parse(data.target.response); obj = JSON.parse(data.target.response || data.target.responseText);
for (i = 0; i < obj.items.length; i += 1) { for (i = 0; i < obj.items.length; i += 1) {
result.push(obj.items[i]); result.push(obj.items[i]);
} }
...@@ -119,8 +119,7 @@ ...@@ -119,8 +119,7 @@
itPa += 1) { itPa += 1) {
if ((!it_name && files[itFi].parents[itPa].isRoot) || if ((!it_name && files[itFi].parents[itPa].isRoot) ||
(it_name && (it_name && files[itFi].parents[itPa].id === parentId)) {
files[itFi].parents[itPa].id === parentId)) {
if (foundItem === true) { if (foundItem === true) {
//if 2 files with same name in same folder. //if 2 files with same name in same folder.
...@@ -159,23 +158,28 @@ ...@@ -159,23 +158,28 @@
function GdriveStorage(spec) { function GdriveStorage(spec) {
if (spec === undefined || spec.access_token === undefined || if (spec === undefined || spec.access_token === undefined ||
typeof spec.access_token !== 'string') { typeof spec.access_token !== 'string') {
throw new TypeError("Access Token' must be a string " + throw new TypeError("Access Token must be a string " +
"which contains more than one character."); "which contains more than one character.");
} }
if (spec.trashing !== undefined &&
(spec.trashing !== true && spec.trashing !== false)) {
throw new TypeError("trashing parameter" +
" must be a boolean (true or false)");
}
this._trashing = spec.trashing || true; this._trashing = spec.trashing || true;
this._access_token = spec.access_token; this._access_token = spec.access_token;
return; return;
} }
function createPostRequest(title, parent, data, datatype) { function createPostRequest(title, parent, data, datatype, boundary) {
var str, var str,
boundary = "-------314159265358979323846",
parentlist = parent ? '{id: "' + parent + '"}' : "", parentlist = parent ? '{id: "' + parent + '"}' : "",
type = data ? "" : '"mimeType" : "' + FOLDER + '",\n'; type = data ? "" : '"mimeType" : "' + FOLDER + '",\n';
str = '--' + boundary + str = '--' + boundary + '\n' +
'\nContent-Type: application/json; charset=UTF-8\n\n' + 'Content-Type: application/json; charset=UTF-8\n\n' +
'{\n"title": "' + title + '",\n' + type + '{\n"title": "' + title + '",\n' +
type +
'"parents": [' + parentlist + ']\n}\n\n' + '"parents": [' + parentlist + ']\n}\n\n' +
'--' + boundary; '--' + boundary;
...@@ -202,8 +206,8 @@ ...@@ -202,8 +206,8 @@
}) })
.push(function (result) { .push(function (result) {
files = result; files = result;
if (title.length === 0 || (files.id && !data) || if ((files.id && !data) || (!files.parent && title.length > 1) ||
(!files.parent && title.length > 1)) { files.isDir) {
throw new jIO.util.jIOError("Method Not Allowed", 405); throw new jIO.util.jIOError("Method Not Allowed", 405);
} }
}) })
...@@ -224,7 +228,8 @@ ...@@ -224,7 +228,8 @@
headers: { headers: {
"Content-Type" : 'multipart/related; boundary="' + boundary + '"' "Content-Type" : 'multipart/related; boundary="' + boundary + '"'
}, },
data : createPostRequest(title.pop(), files.parent, blob, data.type) data : createPostRequest(title.pop(), files.parent,
blob, data.type, boundary)
}); });
}, undefined); }, undefined);
} }
...@@ -252,10 +257,10 @@ ...@@ -252,10 +257,10 @@
return getFileId(id, token); return getFileId(id, token);
}) })
.push(function (result) { .push(function (result) {
if (!result.id) { if (title.length && !result.id) {
throw new jIO.util.jIOError("Not Found", 404); throw new jIO.util.jIOError("Not Found", 404);
} }
if (title.length === 0 || result.isDir !== deleteDir) { if (!title.length || result.isDir !== deleteDir) {
throw new jIO.util.jIOError("Method Not Allowed", 405); throw new jIO.util.jIOError("Method Not Allowed", 405);
} }
return jIO.util.ajax({ return jIO.util.ajax({
...@@ -291,15 +296,11 @@ ...@@ -291,15 +296,11 @@
return getFileId(id, that._access_token); return getFileId(id, that._access_token);
}) })
.push(function (result) { .push(function (result) {
if (result.isDir) { if (!result.id) {
return {};
}
throw new jIO.util.jIOError("Not a directory: " + id, 404);
}, function (error) {
if (error.target !== undefined && error.target.status === 404) {
throw new jIO.util.jIOError("Cannot find document: " + id, 404); throw new jIO.util.jIOError("Cannot find document: " + id, 404);
} }
throw error; if (result.isDir) { return {}; }
throw new jIO.util.jIOError("Not a directory: " + id, 404);
}); });
}; };
......
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