Skip to content

Commit

Permalink
Merge pull request #133 from ifrost/feature/74-dropbox-issue
Browse files Browse the repository at this point in the history
Update Dropbox API
[minor] [gh-pages]
  • Loading branch information
ifrost authored Oct 5, 2020
2 parents 3195dab + 7287dfb commit 4df160e
Show file tree
Hide file tree
Showing 20 changed files with 1,944 additions and 1,702 deletions.
1 change: 1 addition & 0 deletions acceptance.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<script type="text/javascript" src="node_modules/chai/chai.js"></script>
<script type="text/javascript" src="node_modules/mocha/mocha.js"></script>
<script type="text/javascript" src="test/phantomjs-polyfills.js"></script>
<script type="text/javascript" src="node_modules/babel-polyfill/dist/polyfill.js"></script>

<script type="text/javascript">
mocha.setup({
Expand Down
2 changes: 1 addition & 1 deletion js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
impromptu: 'libs/jquery-impromptu.min',
jstree: 'libs/jstree.min',
cookie: 'libs/jquery.cookie',
dropbox: 'libs/dropbox.min',
dropbox: '../node_modules/dropbox/dist/Dropbox-sdk.min',
protoplast: '../node_modules/protoplast/dist/protoplast',
acceptance: '../test/acceptance',
'aw-parser': '../node_modules/aw-parser/dist/aw-parser.amd',
Expand Down
2 changes: 1 addition & 1 deletion js/plugin/editor/controller/editor-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ define(function(require) {

_handleSync: function(content) {
content = converter.to_fountain(content).value;
if (content === undefined) {
if (content === undefined && this.editorModel.isSyncEnabled) {
this.toggleSync();
}
else if (this.editorModel.lastSyncContent !== content) {
Expand Down
168 changes: 73 additions & 95 deletions js/utils/dropbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@ define(function(require) {
};

var client_authenticate = function(callback) {
client = new Dropbox.Client({
key: key,
token: $.cookie('dbt'),
uid: $.cookie('dbu'),
sandbox: false
});
if (client.isAuthenticated()) {
if (module.token) {
callback();
} else {
var state = Dropbox.Util.Oauth.randomAuthStateParam();
var popup = window.open('https://www.dropbox.com/1/oauth2/authorize?response_type=token&redirect_uri=' + redirect_uri + '&client_id=' + key + '&state=' + state, '_blank', 'width=500, height=500');
client = new Dropbox({
clientId: key
});

var url = client.getAuthenticationUrl(redirect_uri);
var popup = window.open(url, '_blank', 'width=500, height=500');
$(window).on('message.dropboxclient', function(e) {
e = e.originalEvent;
e.target.removeEventListener(e.type, arguments.callee);
Expand All @@ -45,29 +43,22 @@ define(function(require) {

if (
(e.data.indexOf('access_token') === -1) ||
(e.data.indexOf('uid') === -1) ||
(e.data.indexOf('state') === -1)
(e.data.indexOf('uid') === -1)
) {
return;
}

var token = /access_token=([^\&]*)/.exec(e.data)[1],
uid = /uid=([^\&]*)/.exec(e.data)[1],
state_r = /state=([^\&]*)/.exec(e.data)[1];

if (state !== state_r) {
return;
}

uid = /uid=([^\&]*)/.exec(e.data)[1];

$.cookie('dbt', token);
$.cookie('dbu', uid);

client = new Dropbox.Client({
key: key,
sandbox: false,
token: token,
uid: uid
client = new Dropbox({
accessToken: token
});

module.token = token;

popup.close();
callback();
Expand All @@ -86,31 +77,19 @@ define(function(require) {


var item_to_data = function(item) {
if (is_lazy) {
return {
isFolder: item.isFolder,
content: [],
folder: item.path.substring(0, item.path.length - item.name.length - 1),
name: item.name,
path: item.path,
entry: item
};
}
else {
return {
isFolder: item.stat.isFolder,
content: [],
folder: item.path.substring(0, item.stat.path.length - item.stat.name.length - 1),
name: item.stat.name,
path: item.path,
entry: item
};
}
return {
isFolder: item['.tag'] === 'folder',
content: [],
folder: item.path_lower.substring(0, item.path_lower.length - item.name.length - 1),
name: item.name,
path: item.path_lower,
entry: item
};
};

var list = function(callback, options) {
options = options || {};
options.folder = options.folder || '/';
options.folder = options.folder || '';

if (options.lazy) {
is_lazy = true;
Expand All @@ -128,32 +107,25 @@ define(function(require) {
}

var pull_callback = function(error, result) {
result = is_lazy ? result : result.changes;
var items = result.filter(function(file) {
return !file.wasRemoved;
});
var root = {
'isRoot': true,
'isFolder': true,
'path': '/',
'path': '',
'name': 'Dropbox',
'content': []
};
//root.entry = root;

var folders = {
'/': root
'': root
};
var files = [];

items = items.filter(function(i) {
i = is_lazy ? i : i.stat;
return !options.pdfOnly || i.isFolder || i.mimeType === "application/pdf";
});

items.forEach(function(item) {

result.forEach(function(item) {
var data = item_to_data(item);
if (data.isFolder) {
folders[data.path] = data;
} else if (options.pdfOnly && !data.name.match(/.*\.pdf$/)) {
return;
}
files.push(data);
});
Expand All @@ -172,7 +144,10 @@ define(function(require) {
root.content.push(file);
}
else {
folders[file.folder || '/'].content.push(file);
var folder = folders[file.folder || ''];
if (folder) {
folder.content.push(file);
}
}
});

Expand All @@ -183,37 +158,32 @@ define(function(require) {

};

var conflate_caller = function(conflate_callback, error, result) {
if (is_lazy) {
client.metadata(options.folder, {readDir: true}, conflate_callback);
var conflate_caller = function(conflate_callback, result) {
if (result && result.cursor) {
client.filesListFolderContinue({
cursor: result.cursor
}).then(conflate_callback);
}
else {
client.pullChanges(result ? result.cursorTag : null, conflate_callback);
client.filesListFolder({
path: is_lazy ? options.folder : '',
recursive: !is_lazy
}).then(conflate_callback);
}
};

var conflate_tester = function(error, result) {
return !error && result.shouldPullAgain;
var conflate_tester = function(result) {
return result.has_more;
};

var conflate_final = function(results) {
var last_error = results[results.length - 1][0],
final_result;

if (is_lazy) {
final_result = [];
results.forEach(function(args) {
final_result = final_result.concat(args[2]);
});
}
else {
final_result = {
changes: []
};
results.forEach(function(args) {
final_result.changes = final_result.changes.concat(args[1].changes);
});
}

results.forEach(function(args) {
final_result = final_result.concat(args[0].entries);
});

pull_callback(last_error, final_result);
};

Expand All @@ -222,7 +192,7 @@ define(function(require) {
module.list = auth_method(list);

var lazy_list = function(options, node, callback) {
var folder = node.id === '#' ? '/' : node.id;
var folder = node.id === '#' ? '' : node.id;
var options = {
folder: folder,
pdfOnly: options.pdfOnly,
Expand All @@ -236,19 +206,17 @@ define(function(require) {
};

var load_file = function(path, callback) {
client.readFile(path, {
blob: true
}, function(error, blob) {
if (error) {
$.prompt('Cannot open the file');
callback(undefined);
} else {
var fileReader = new FileReader();
fileReader.onload = function() {
callback(this.result);
};
fileReader.readAsText(blob);
}
client.filesDownload({
path: path
}).then(function(data) {
var fileReader = new FileReader();
fileReader.onload = function() {
callback(this.result);
};
fileReader.readAsText(data.fileBlob);
}).catch(function() {
$.prompt('Cannot open the file');
callback(undefined);
});
};
module.load_file = auth_method(load_file);
Expand Down Expand Up @@ -286,7 +254,17 @@ define(function(require) {
};

var save = function(path, data, callback) {
client.writeFile(path, data, {}, callback);
client.filesUpload({
path: path,
contents: data,
mode: {
'.tag': 'overwrite'
}
}).then(function(response) {
callback();
}).catch(function(error) {
callback(error);
});
};
module.save = auth_method(save);

Expand Down
11 changes: 11 additions & 0 deletions local-template/token.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<body onload="send_message()">
<span id="alert" />
<script>
function send_message() {
window.opener.postMessage(window.location.href, 'http://localhost:8000/');
}
</script>
</body>

</html>
Loading

0 comments on commit 4df160e

Please sign in to comment.