Stackedit/src/services/providers/dropboxProvider.js

154 lines
4.6 KiB
JavaScript
Raw Normal View History

2017-09-23 19:01:50 +00:00
import store from '../../store';
import dropboxHelper from './helpers/dropboxHelper';
2018-04-27 14:37:05 +00:00
import Provider from './common/Provider';
2017-09-23 19:01:50 +00:00
import utils from '../utils';
import workspaceSvc from '../workspaceSvc';
2017-09-23 19:01:50 +00:00
2017-09-26 22:54:26 +00:00
const makePathAbsolute = (token, path) => {
if (!token.fullAccess) {
return `/Applications/StackEdit (restricted)${path}`;
}
return path;
};
const makePathRelative = (token, path) => {
if (!token.fullAccess) {
return path.replace(/^\/Applications\/StackEdit \(restricted\)/, '');
}
return path;
};
2017-09-23 19:01:50 +00:00
2018-04-27 14:37:05 +00:00
export default new Provider({
2017-09-23 19:01:50 +00:00
id: 'dropbox',
2018-07-17 19:58:40 +00:00
name: 'Dropbox',
getToken({ sub }) {
return store.getters['data/dropboxTokensBySub'][sub];
2017-09-23 19:01:50 +00:00
},
getLocationUrl({ path }) {
const pathComponents = path.split('/').map(encodeURIComponent);
2017-09-23 19:01:50 +00:00
const filename = pathComponents.pop();
2017-09-26 22:54:26 +00:00
return `https://www.dropbox.com/home${pathComponents.join('/')}?preview=${filename}`;
2017-09-23 19:01:50 +00:00
},
2018-07-17 19:58:40 +00:00
getLocationDescription({ path, dropboxFileId }) {
return dropboxFileId || path;
2017-09-23 19:01:50 +00:00
},
checkPath(path) {
return path && path.match(/^\/[^\\<>:"|?*]+$/);
},
2018-05-13 13:27:33 +00:00
async downloadContent(token, syncLocation) {
const { content } = await dropboxHelper.downloadFile({
2017-09-26 22:54:26 +00:00
token,
2018-05-13 13:27:33 +00:00
path: makePathRelative(token, syncLocation.path),
fileId: syncLocation.dropboxFileId,
});
return Provider.parseContent(content, `${syncLocation.fileId}/content`);
2017-09-23 19:01:50 +00:00
},
2018-05-13 13:27:33 +00:00
async uploadContent(token, content, syncLocation) {
const dropboxFile = await dropboxHelper.uploadFile({
2017-09-23 19:01:50 +00:00
token,
2018-05-13 13:27:33 +00:00
path: makePathRelative(token, syncLocation.path),
content: Provider.serializeContent(content),
fileId: syncLocation.dropboxFileId,
});
return {
...syncLocation,
path: makePathAbsolute(token, dropboxFile.path_display),
dropboxFileId: dropboxFile.id,
};
2017-09-23 19:01:50 +00:00
},
2018-05-13 13:27:33 +00:00
async publish(token, html, metadata, publishLocation) {
const dropboxFile = await dropboxHelper.uploadFile({
2017-09-23 19:01:50 +00:00
token,
2018-05-13 13:27:33 +00:00
path: publishLocation.path,
content: html,
fileId: publishLocation.dropboxFileId,
});
return {
...publishLocation,
path: makePathAbsolute(token, dropboxFile.path_display),
dropboxFileId: dropboxFile.id,
};
2017-09-23 19:01:50 +00:00
},
2018-05-13 13:27:33 +00:00
async openFiles(token, paths) {
await utils.awaitSequence(paths, async (path) => {
// Check if the file exists and open it
if (!Provider.openFileWithLocation({
providerId: this.id,
path,
})) {
2018-05-13 13:27:33 +00:00
// Download content from Dropbox
const syncLocation = {
path,
providerId: this.id,
sub: token.sub,
};
let content;
try {
content = await this.downloadContent(token, syncLocation);
} catch (e) {
2017-09-23 19:01:50 +00:00
store.dispatch('notification/error', `Could not open file ${path}.`);
2018-05-13 13:27:33 +00:00
return;
}
// Create the file
let name = path;
const slashPos = name.lastIndexOf('/');
if (slashPos > -1 && slashPos < name.length - 1) {
name = name.slice(slashPos + 1);
}
const dotPos = name.lastIndexOf('.');
if (dotPos > 0 && slashPos < name.length) {
name = name.slice(0, dotPos);
}
const item = await workspaceSvc.createFile({
2018-05-13 13:27:33 +00:00
name,
parentId: store.getters['file/current'].parentId,
text: content.text,
properties: content.properties,
discussions: content.discussions,
comments: content.comments,
}, true);
store.commit('file/setCurrentId', item.id);
workspaceSvc.addSyncLocation({
2018-05-13 13:27:33 +00:00
...syncLocation,
fileId: item.id,
});
store.dispatch('notification/info', `${store.getters['file/current'].name} was imported from Dropbox.`);
}
});
2017-09-23 19:01:50 +00:00
},
makeLocation(token, path) {
return {
providerId: this.id,
sub: token.sub,
path,
};
},
2018-07-17 19:58:40 +00:00
async listFileRevisions({ token, syncLocation }) {
const entries = await dropboxHelper.listRevisions({
token,
path: makePathRelative(token, syncLocation.path),
fileId: syncLocation.dropboxFileId,
});
2018-07-17 19:58:40 +00:00
return entries.map(entry => ({
id: entry.rev,
2018-09-19 08:59:22 +00:00
sub: `${dropboxHelper.subPrefix}:${(entry.sharing_info || {}).modified_by || token.sub}`,
2018-07-17 19:58:40 +00:00
created: new Date(entry.server_modified).getTime(),
}));
},
async loadFileRevision() {
// Revision are already loaded
return false;
},
async getFileRevisionContent({
token,
contentId,
revisionId,
}) {
const { content } = await dropboxHelper.downloadFile({
token,
path: `rev:${revisionId}`,
});
return Provider.parseContent(content, contentId);
},
2017-09-23 19:01:50 +00:00
});