2017-09-23 19:01:50 +00:00
|
|
|
import store from '../../store';
|
|
|
|
import dropboxHelper from './helpers/dropboxHelper';
|
|
|
|
import providerUtils from './providerUtils';
|
|
|
|
import providerRegistry from './providerRegistry';
|
|
|
|
import utils from '../utils';
|
|
|
|
|
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
|
|
|
|
|
|
|
export default providerRegistry.register({
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
.then(({ content }) => providerUtils.parseContent(content, syncLocation));
|
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),
|
2017-09-23 19:01:50 +00:00
|
|
|
providerUtils.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;
|
|
|
|
}
|
2017-10-14 11:39:15 +00:00
|
|
|
if (providerUtils.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();
|
|
|
|
}
|
2017-10-14 11:39:15 +00:00
|
|
|
// 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) => {
|
|
|
|
const id = utils.uid();
|
|
|
|
delete content.history;
|
|
|
|
store.commit('content/setItem', {
|
|
|
|
...content,
|
|
|
|
id: `${id}/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);
|
|
|
|
}
|
|
|
|
store.commit('file/setItem', {
|
|
|
|
id,
|
2017-10-09 07:11:18 +00:00
|
|
|
name: utils.sanitizeName(name),
|
2017-09-23 19:01:50 +00:00
|
|
|
parentId: store.getters['file/current'].parentId,
|
|
|
|
});
|
|
|
|
store.commit('syncLocation/setItem', {
|
|
|
|
...syncLocation,
|
|
|
|
id: utils.uid(),
|
|
|
|
fileId: id,
|
|
|
|
});
|
|
|
|
store.commit('file/setCurrentId', id);
|
|
|
|
store.dispatch('notification/info', `${store.getters['file/current'].name} was imported from Dropbox.`);
|
|
|
|
}, () => {
|
|
|
|
store.dispatch('notification/error', `Could not open file ${path}.`);
|
|
|
|
})
|
|
|
|
.then(() => openOneFile());
|
|
|
|
};
|
|
|
|
return Promise.resolve()
|
|
|
|
.then(() => openOneFile());
|
|
|
|
},
|
|
|
|
makeLocation(token, path) {
|
|
|
|
return {
|
|
|
|
providerId: this.id,
|
|
|
|
sub: token.sub,
|
|
|
|
path,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|