Skip to content

Commit 845ef9b

Browse files
committed
Support export to and import from Google Drive
1 parent c183002 commit 845ef9b

8 files changed

Lines changed: 528 additions & 5 deletions

File tree

lib/response.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,8 @@ function actionDownload(req, res, noteId) {
329329
filename = encodeURIComponent(filename);
330330
res.writeHead(200, {
331331
'Access-Control-Allow-Origin': '*', //allow CORS as API
332+
'Access-Control-Allow-Headers': 'Range',
333+
'Access-Control-Expose-Headers': 'Cache-Control, Content-Encoding, Content-Range',
332334
'Content-Type': 'text/markdown; charset=UTF-8',
333335
'Cache-Control': 'private',
334336
'Content-disposition': 'attachment; filename=' + filename + '.md',

public/js/common.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
var domain = 'change this'; // domain name
33
var urlpath = ''; // sub url path, like: www.example.com/<urlpath>
44

5+
var GOOGLE_API_KEY = 'change this';
6+
var GOOGLE_CLIENT_ID = 'change this';
7+
58
var port = window.location.port;
69
var serverurl = window.location.protocol + '//' + domain + (port ? ':' + port : '') + (urlpath ? '/' + urlpath : '');
710
var noteid = urlpath ? window.location.pathname.slice(urlpath.length + 1, window.location.pathname.length).split('/')[1] : window.location.pathname.split('/')[1];

public/js/google-drive-picker.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/**!
2+
* Google Drive File Picker Example
3+
* By Daniel Lo Nigro (http://dan.cx/)
4+
*/
5+
(function() {
6+
/**
7+
* Initialise a Google Driver file picker
8+
*/
9+
var FilePicker = window.FilePicker = function(options) {
10+
// Config
11+
this.apiKey = options.apiKey;
12+
this.clientId = options.clientId;
13+
14+
// Elements
15+
this.buttonEl = options.buttonEl;
16+
17+
// Events
18+
this.onSelect = options.onSelect;
19+
this.buttonEl.on('click', this.open.bind(this));
20+
21+
// Disable the button until the API loads, as it won't work properly until then.
22+
this.buttonEl.prop('disabled', true);
23+
24+
// Load the drive API
25+
gapi.client.setApiKey(this.apiKey);
26+
gapi.client.load('drive', 'v2', this._driveApiLoaded.bind(this));
27+
google.load('picker', '1', { callback: this._pickerApiLoaded.bind(this) });
28+
}
29+
30+
FilePicker.prototype = {
31+
/**
32+
* Open the file picker.
33+
*/
34+
open: function() {
35+
// Check if the user has already authenticated
36+
var token = gapi.auth.getToken();
37+
if (token) {
38+
this._showPicker();
39+
} else {
40+
// The user has not yet authenticated with Google
41+
// We need to do the authentication before displaying the Drive picker.
42+
this._doAuth(false, function() { this._showPicker(); }.bind(this));
43+
}
44+
},
45+
46+
/**
47+
* Show the file picker once authentication has been done.
48+
* @private
49+
*/
50+
_showPicker: function() {
51+
var accessToken = gapi.auth.getToken().access_token;
52+
var view = new google.picker.DocsView();
53+
view.setMimeTypes("text/markdown,text/html");
54+
view.setIncludeFolders(true);
55+
this.picker = new google.picker.PickerBuilder().
56+
enableFeature(google.picker.Feature.NAV_HIDDEN).
57+
addView(view).
58+
setAppId(this.clientId).
59+
setOAuthToken(accessToken).
60+
setCallback(this._pickerCallback.bind(this)).
61+
build().
62+
setVisible(true);
63+
},
64+
65+
/**
66+
* Called when a file has been selected in the Google Drive file picker.
67+
* @private
68+
*/
69+
_pickerCallback: function(data) {
70+
if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
71+
var file = data[google.picker.Response.DOCUMENTS][0],
72+
id = file[google.picker.Document.ID],
73+
request = gapi.client.drive.files.get({
74+
fileId: id
75+
});
76+
77+
request.execute(this._fileGetCallback.bind(this));
78+
}
79+
},
80+
/**
81+
* Called when file details have been retrieved from Google Drive.
82+
* @private
83+
*/
84+
_fileGetCallback: function(file) {
85+
if (this.onSelect) {
86+
this.onSelect(file);
87+
}
88+
},
89+
90+
/**
91+
* Called when the Google Drive file picker API has finished loading.
92+
* @private
93+
*/
94+
_pickerApiLoaded: function() {
95+
this.buttonEl.prop('disabled', false);
96+
},
97+
98+
/**
99+
* Called when the Google Drive API has finished loading.
100+
* @private
101+
*/
102+
_driveApiLoaded: function() {
103+
this._doAuth(true);
104+
},
105+
106+
/**
107+
* Authenticate with Google Drive via the Google JavaScript API.
108+
* @private
109+
*/
110+
_doAuth: function(immediate, callback) {
111+
gapi.auth.authorize({
112+
client_id: this.clientId,
113+
scope: 'https://www.googleapis.com/auth/drive.readonly',
114+
immediate: immediate
115+
}, callback ? callback : function() {});
116+
}
117+
};
118+
}());

0 commit comments

Comments
 (0)