Stackedit/src/services/providers/dropboxProvider.js

143 lines
4.4 KiB
JavaScript
Raw Normal View History

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;
}
let syncLocation;
// Try to find an existing sync location
store.getters['syncLocation/items'].some((existingSyncLocation) => {
if (existingSyncLocation.providerId === this.id &&
existingSyncLocation.path === path
) {
syncLocation = existingSyncLocation;
}
return syncLocation;
});
if (syncLocation) {
// Sync location already exists, just open the file
store.commit('file/setCurrentId', syncLocation.fileId);
return openOneFile();
}
// Sync location does not exist, download content from Dropbox and create the file
syncLocation = {
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,
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,
};
},
});