Stackedit/src/services/providers/dropboxProvider.js

134 lines
4.0 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) {
2017-09-26 22:54:26 +00:00
return store.getters['data/dropboxTokens'][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(/^\/[^\\<>:"|?*]+$/);
},
2017-09-26 22:54:26 +00:00
downloadContent(token, syncLocation) {
return dropboxHelper.downloadFile(
token,
makePathRelative(token, syncLocation.path),
syncLocation.dropboxFileId,
)
2018-04-27 14:37:05 +00:00
.then(({ content }) => Provider.parseContent(content, `${syncLocation.fileId}/content`));
2017-09-23 19:01:50 +00:00
},
2017-09-26 22:54:26 +00:00
uploadContent(token, content, syncLocation) {
2017-09-23 19:01:50 +00:00
return dropboxHelper.uploadFile(
token,
2017-09-26 22:54:26 +00:00
makePathRelative(token, syncLocation.path),
2018-04-27 14:37:05 +00:00
Provider.serializeContent(content),
2017-09-26 22:54:26 +00:00
syncLocation.dropboxFileId,
2017-09-23 19:01:50 +00:00
)
.then(dropboxFile => ({
2017-09-26 22:54:26 +00:00
...syncLocation,
path: makePathAbsolute(token, dropboxFile.path_display),
2017-09-23 19:01:50 +00:00
dropboxFileId: dropboxFile.id,
}));
},
2017-09-26 22:54:26 +00:00
publish(token, html, metadata, publishLocation) {
2017-09-23 19:01:50 +00:00
return dropboxHelper.uploadFile(
token,
2017-09-26 22:54:26 +00:00
publishLocation.path,
2017-09-23 19:01:50 +00:00
html,
2017-09-26 22:54:26 +00:00
publishLocation.dropboxFileId,
2017-09-23 19:01:50 +00:00
)
.then(dropboxFile => ({
2017-09-26 22:54:26 +00:00
...publishLocation,
path: makePathAbsolute(token, dropboxFile.path_display),
2017-09-23 19:01:50 +00:00
dropboxFileId: dropboxFile.id,
}));
},
openFiles(token, paths) {
const openOneFile = () => {
2017-09-26 22:54:26 +00:00
const path = paths.pop();
2017-09-23 19:01:50 +00:00
if (!path) {
return null;
}
2018-04-27 14:37:05 +00:00
if (Provider.openFileWithLocation(store.getters['syncLocation/items'], {
providerId: this.id,
path,
})) {
// File exists and has just been opened. Next...
2017-09-23 19:01:50 +00:00
return openOneFile();
}
// Download content from Dropbox and create the file
const syncLocation = {
2017-09-23 19:01:50 +00:00
path,
providerId: this.id,
sub: token.sub,
};
return this.downloadContent(token, syncLocation)
.then((content) => {
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-05-04 18:07:28 +00:00
return fileSvc.createFile({
name,
2017-09-23 19:01:50 +00:00
parentId: store.getters['file/current'].parentId,
2018-05-04 18:07:28 +00:00
text: content.text,
properties: content.properties,
discussions: content.discussions,
comments: content.comments,
}, true);
})
.then((item) => {
store.commit('file/setCurrentId', item.id);
2017-09-23 19:01:50 +00:00
store.commit('syncLocation/setItem', {
...syncLocation,
id: utils.uid(),
2018-05-04 18:07:28 +00:00
fileId: item.id,
2017-09-23 19:01:50 +00:00
});
store.dispatch('notification/info', `${store.getters['file/current'].name} was imported from Dropbox.`);
2018-05-04 18:07:28 +00:00
})
.catch(() => {
2017-09-23 19:01:50 +00:00
store.dispatch('notification/error', `Could not open file ${path}.`);
})
.then(() => openOneFile());
};
2018-05-04 18:07:28 +00:00
return Promise.resolve(openOneFile());
2017-09-23 19:01:50 +00:00
},
makeLocation(token, path) {
return {
providerId: this.id,
sub: token.sub,
path,
};
},
});