Stackedit/src/services/providers/dropboxProvider.js

128 lines
3.9 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';
2018-05-04 18:07:28 +00:00
import fileSvc from '../fileSvc';
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',
getToken(location) {
2018-06-21 19:16:33 +00:00
return store.getters['data/dropboxTokensBySub'][location.sub];
2017-09-23 19:01:50 +00:00
},
getUrl(location) {
const pathComponents = location.path.split('/').map(encodeURIComponent);
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
},
getDescription(location) {
const token = this.getToken(location);
2017-09-26 22:54:26 +00:00
return `${location.path}${location.dropboxFileId}${token.name}`;
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(store.getters['syncLocation/items'], {
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 fileSvc.createFile({
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);
store.commit('syncLocation/setItem', {
...syncLocation,
id: utils.uid(),
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,
};
},
});