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';
|
2018-07-03 23:41:24 +00:00
|
|
|
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-03 23:41:24 +00:00
|
|
|
getToken({ sub }) {
|
|
|
|
return store.getters['data/dropboxTokensBySub'][sub];
|
2017-09-23 19:01:50 +00:00
|
|
|
},
|
2018-07-03 23:41:24 +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-03 23:41:24 +00:00
|
|
|
getLocationDescription({ path }) {
|
|
|
|
return 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
|
2018-07-03 23:41:24 +00:00
|
|
|
if (!Provider.openFileWithLocation({
|
2017-10-14 11:39:15 +00:00
|
|
|
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);
|
|
|
|
}
|
2018-07-03 23:41:24 +00:00
|
|
|
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);
|
2018-07-03 23:41:24 +00:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|