Commit d005d845 authored by lucas.parsy's avatar lucas.parsy Committed by Romain Courteaud

GdriveStorage: add a new google drive storage

Squashed commit of the following:

commit 2283732c2e3649d66c3b83f00ac2a9d846f50a35
Author: Lucas Parsy <lucas.parsy@nexedi.com>
Date:   Fri Dec 4 11:02:50 2015 +0100

    modified gdrivestorage's allDocs to return only the documents ids.
    modified gdrivestorage test file to fit the above changes.
    refactored an URI template in gdrivestorage.

commit eaf750820f349ff1add6ef5172779a119dc06597
Author: Lucas Parsy <lucas.parsy@nexedi.com>
Date:   Fri Nov 27 17:16:16 2015 +0100

    added allAttachments method to gdriveStorage with tests.
    corrected bug in get method returning a string instead of an object.

commit 212e4d6b41382c9d8ad2aaa9743671850eac245d
Author: Lucas Parsy <lucas.parsy@nexedi.com>
Date:   Fri Nov 27 15:49:05 2015 +0100

    various fixes on gdriveStorage and his tests.

    removed the removeAttachment method as it was useless.
    removed error catching when user was doing request with invalid credentials.
    throw error when doing a putAttachment on a folder.

    changed varaiables names in test file to respect naming convention.
    added/ removed tests following the changes on gdriveStorage file.

    added tests verifying that the user can only put "enclosure"
    as attachment name in putAttachment and getAttachment.

commit 5150c9420e30ab7869c4d4c2cedd5eddeb1e52de
Author: Lucas Parsy <lucas.parsy@nexedi.com>
Date:   Tue Nov 24 11:01:33 2015 +0100

    followed naming convention in gdrivestorage test file.
    post and put method now return the id of the docment they created/modified.
    corrected bug in getAttachment allowing the method to pass the test,
    but not to work on real utilisation.

commit 0195dc37eeac4ae8f009f483e566180c81c857b1
Author: Lucas Parsy <lucas.parsy@nexedi.com>
Date:   Tue Nov 3 16:44:20 2015 +0100

    added test for gdrivestorage for allDocs() method
    when method needs to do multiple calls to google Drive API.
    added gdrivestorage.tests.js to tests.html file.

commit 204acb98616267a5598b099f8375c3fe38d4e268
Author: Lucas Parsy <lucas.parsy@nexedi.com>
Date:   Fri Oct 30 12:27:49 2015 +0100

    refactored gdrivestorage.js
    Improved gdrive allDocs test to check return value.

commit 23f6ec71de45d161c36ca148d5fa53d475a51d89
Author: Lucas Parsy <lucas.parsy@nexedi.com>
Date:   Thu Oct 29 14:21:36 2015 +0100

    corrected 2 jslint errors in gdrivestorage test file

commit b472700451e72659fecfacd07305cddb48ece323
Author: Lucas Parsy <lucas.parsy@nexedi.com>
Date:   Thu Oct 29 13:40:08 2015 +0100

    completed google drive storage and added test file

commit 841a5fb5d2388cd822ed53dec1984b0aafb3accd
Author: Lucas Parsy <lucas.parsy@nexedi.com>
Date:   Mon Oct 19 16:12:56 2015 +0200

    changed names of variables and checked user input in putAttachment method

commit 991cd0a70ce3a9da61c724f6bf50336684776535
Author: Lucas Parsy <lucas.parsy@nexedi.com>
Date:   Fri Sep 25 15:31:57 2015 +0200

    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.

commit 8d82e3e51634d774685bc39c804a9a383dc46cad
Author: Lucas Parsy <lucas.parsy@nexedi.com>
Date:   Thu Sep 24 17:33:00 2015 +0200

    added google drive storage to jio (tests to be done)
parent e10057fd
...@@ -173,6 +173,7 @@ module.exports = function (grunt) { ...@@ -173,6 +173,7 @@ module.exports = function (grunt) {
'src/jio.storage/cryptstorage.js', 'src/jio.storage/cryptstorage.js',
'src/jio.storage/dropboxstorage.js', 'src/jio.storage/dropboxstorage.js',
'src/jio.storage/davstorage.js', 'src/jio.storage/davstorage.js',
'src/jio.storage/gdrivestorage.js',
'src/jio.storage/unionstorage.js', 'src/jio.storage/unionstorage.js',
'src/jio.storage/erp5storage.js', 'src/jio.storage/erp5storage.js',
'src/jio.storage/querystorage.js', 'src/jio.storage/querystorage.js',
......
...@@ -43,6 +43,7 @@ zip: ...@@ -43,6 +43,7 @@ zip:
@cp src/jio.storage/replicatestorage.js $(TMPDIR)/jio/storage/ @cp src/jio.storage/replicatestorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/splitstorage.js $(TMPDIR)/jio/storage/ @cp src/jio.storage/splitstorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/cryptstorage.js $(TMPDIR)/jio/storage/ @cp src/jio.storage/cryptstorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/gdrivestorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/revisionstorage.js $(TMPDIR)/jio/storage/ @cp src/jio.storage/revisionstorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/zipstorage.js $(TMPDIR)/jio/storage/ @cp src/jio.storage/zipstorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/replicaterevisionstorage.js $(TMPDIR)/jio/storage/ @cp src/jio.storage/replicaterevisionstorage.js $(TMPDIR)/jio/storage/
...@@ -76,6 +77,7 @@ zip: ...@@ -76,6 +77,7 @@ zip:
@$(UGLIFY) src/jio.storage/replicatestorage.js >$(TMPDIR)/jio/storage/replicatestorage.min.js 2>/dev/null @$(UGLIFY) src/jio.storage/replicatestorage.js >$(TMPDIR)/jio/storage/replicatestorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/splitstorage.js >$(TMPDIR)/jio/storage/splitstorage.min.js 2>/dev/null @$(UGLIFY) src/jio.storage/splitstorage.js >$(TMPDIR)/jio/storage/splitstorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/cryptstorage.js >$(TMPDIR)/jio/storage/cryptstorage.min.js 2>/dev/null @$(UGLIFY) src/jio.storage/cryptstorage.js >$(TMPDIR)/jio/storage/cryptstorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/gdrivestorage.js >$(TMPDIR)/jio/storage/gdrivestorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/revisionstorage.js >$(TMPDIR)/jio/storage/revisionstorage.min.js 2>/dev/null @$(UGLIFY) src/jio.storage/revisionstorage.js >$(TMPDIR)/jio/storage/revisionstorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/replicaterevisionstorage.js >$(TMPDIR)/jio/storage/replicaterevisionstorage.min.js 2>/dev/null @$(UGLIFY) src/jio.storage/replicaterevisionstorage.js >$(TMPDIR)/jio/storage/replicaterevisionstorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/zipstorage.js >$(TMPDIR)/jio/storage/zipstorage.min.js 2>/dev/null @$(UGLIFY) src/jio.storage/zipstorage.js >$(TMPDIR)/jio/storage/zipstorage.min.js 2>/dev/null
......
/*
* Copyright 2015, Nexedi SA
* Released under the LGPL license.
* http://www.gnu.org/licenses/lgpl.html
*/
/**
* JIO Google Drive Storage. Type = "gdrive".
* Google Drive "database" storage.
*/
/*global jIO, Blob, RSVP, UriTemplate, JSON*/
/*jslint nomen: true*/
(function (jIO, Blob, RSVP, UriTemplate, JSON) {
"use strict";
var UPLOAD_URL = "https://www.googleapis.com{/upload}/drive/v2/files{/id}" +
"{?uploadType,access_token}",
upload_template = UriTemplate.parse(UPLOAD_URL),
REMOVE_URL = "https://www.googleapis.com/drive/v2/" +
"files{/id,trash}{?access_token}",
remove_template = UriTemplate.parse(REMOVE_URL),
LIST_URL = "https://www.googleapis.com/drive/v2/files" +
"?prettyPrint=false{&pageToken}&q=trashed=false" +
"&fields=nextPageToken,items(id){&access_token}",
list_template = UriTemplate.parse(LIST_URL),
GET_URL = "https://www.googleapis.com/drive/v2/files{/id}{?alt}",
get_template = UriTemplate.parse(GET_URL);
function handleError(error, id) {
if (error.target.status === 404) {
throw new jIO.util.jIOError(
"Cannot find document: " + id,
404
);
}
throw error;
}
function listPage(result, token) {
var i,
obj;
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax({
"type": "GET",
"url": list_template.expand({
pageToken : (result.nextPageToken || ""),
access_token: token
})
});
})
.push(function (data) {
obj = JSON.parse(data.target.response || data.target.responseText);
for (i = 0; i < obj.items.length; i += 1) {
obj.items[i].value = {};
result.push(obj.items[i]);
}
result.nextPageToken = obj.nextPageToken;
return result;
}, handleError);
}
function checkName(name) {
if (name !== "enclosure") {
throw new jIO.util.jIOError("Only support 'enclosure' attachment", 400);
}
}
/**
* The JIO Google Drive Storage extension
*
* @class GdriveStorage
* @constructor
*/
function GdriveStorage(spec) {
if (spec === undefined || spec.access_token === undefined ||
typeof spec.access_token !== 'string') {
throw new TypeError("Access Token must be a string " +
"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._access_token = spec.access_token;
return;
}
function recursiveAllDocs(result, accessToken) {
return new RSVP.Queue()
.push(function () {
return listPage(result, accessToken);
})
.push(function () {
if (result.nextPageToken) {
return recursiveAllDocs(result, accessToken);
}
return result;
});
}
GdriveStorage.prototype.hasCapacity = function (name) {
return (name === "list");
};
GdriveStorage.prototype.buildQuery = function () {
return recursiveAllDocs([], this._access_token);
};
function sendMetaData(id, param, token) {
var boundary = "-------314159265358979323846";
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax({
"type": id ? "PUT" : "POST",
"url": upload_template.expand({
access_token: token,
id: id || [],
upload: id ? [] : "upload",
uploadType: "multipart"
}),
headers: {
"Content-Type" : 'multipart/related; boundary="' + boundary + '"'
},
data: '--' + boundary + '\n' +
'Content-Type: application/json; charset=UTF-8\n\n' +
JSON.stringify(param) + '\n\n--' + boundary + "--"
});
})
.push(function (result) {
var obj = JSON.parse(result.target.responseText);
return obj.id;
},
function (error) {handleError(error, id); });
}
GdriveStorage.prototype.put = function (id, param) {
return sendMetaData(id, param, this._access_token);
};
GdriveStorage.prototype.post = function (param) {
return sendMetaData(undefined, param, this._access_token);
};
function sendData(id, blob, token) {
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax({
"type": "PUT",
"url": upload_template.expand({
access_token: token,
upload: "upload",
id: id,
uploadType: "media"
}),
data: blob
});
})
.push(function (data) {
data = JSON.parse(data.target.responseText);
if (data.mimeType === "application/vnd.google-apps.folder") {
throw new jIO.util.jIOError("cannot put attachments to folder", 400);
}
return data;
}, function (error) {handleError(error, id); });
}
GdriveStorage.prototype.putAttachment = function (id, name, blob) {
checkName(name);
return sendData(id, blob, this._access_token);
};
GdriveStorage.prototype.remove = function (id) {
var that = this;
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax({
type: that._trashing ? "POST" : "DELETE",
url: remove_template.expand({
id : id,
access_token : that._access_token,
trash : that._trashing ? "trash" : []
})
});
})
.push(undefined, function (error) {handleError(error, id); });
};
function getData(id, attach, token) {
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax({
type: "GET",
dataType: attach ? "blob" : "json",
url: get_template.expand({
id: id,
alt: attach ? "media" : [],
access_token: token
}),
headers: {
"Authorization" : "Bearer " + token
}
});
})
.push(function (evt) {
return evt.target.response ||
(attach ? new Blob([evt.target.responseText],
{"type" :
evt.target.responseHeaders["Content-Type"]}) :
JSON.parse(evt.target.responseText));
}, function (error) {handleError(error, id); });
}
GdriveStorage.prototype.get = function (id) {
return getData(id, false, this._access_token);
};
GdriveStorage.prototype.getAttachment = function (id, name) {
checkName(name);
return getData(id, true, this._access_token);
};
GdriveStorage.prototype.allAttachments = function (id) {
var token = this._access_token;
return new RSVP.Queue()
.push(function () {
return getData(id, false, token);
})
.push(function (data) {
if (data.mimeType === "application/vnd.google-apps.folder") {
return {};
}
return {"enclosure": {}};
});
};
jIO.addStorage('gdrive', GdriveStorage);
}(jIO, Blob, RSVP, UriTemplate, JSON));
This diff is collapsed.
...@@ -46,7 +46,7 @@ ...@@ -46,7 +46,7 @@
<script src="jio.storage/cryptstorage.tests.js"></script> <script src="jio.storage/cryptstorage.tests.js"></script>
<script src="jio.storage/dropboxstorage.tests.js"></script> <script src="jio.storage/dropboxstorage.tests.js"></script>
<script src="jio.storage/zipstorage.tests.js"></script> <script src="jio.storage/zipstorage.tests.js"></script>
<script src="jio.storage/gdrivestorage.tests.js"></script>
<!--script src="../lib/jquery/jquery.min.js"></script> <!--script src="../lib/jquery/jquery.min.js"></script>
<script src="../src/jio.storage/xwikistorage.js"></script> <script src="../src/jio.storage/xwikistorage.js"></script>
<script src="jio.storage/xwikistorage.tests.js"></script--> <script src="jio.storage/xwikistorage.tests.js"></script-->
......
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