2017-09-17 15:32:39 +00:00
|
|
|
import store from '../../store';
|
|
|
|
import googleHelper from './helpers/googleHelper';
|
|
|
|
import providerUtils from './providerUtils';
|
2017-09-23 19:01:50 +00:00
|
|
|
import providerRegistry from './providerRegistry';
|
2017-09-17 15:32:39 +00:00
|
|
|
import utils from '../utils';
|
|
|
|
|
2017-09-23 19:01:50 +00:00
|
|
|
export default providerRegistry.register({
|
|
|
|
id: 'googleDrive',
|
|
|
|
getToken(location) {
|
|
|
|
const token = store.getters['data/googleTokens'][location.sub];
|
|
|
|
return token && token.isDrive ? token : null;
|
|
|
|
},
|
|
|
|
getUrl(location) {
|
|
|
|
return `https://docs.google.com/file/d/${location.driveFileId}/edit`;
|
|
|
|
},
|
|
|
|
getDescription(location) {
|
|
|
|
const token = this.getToken(location);
|
|
|
|
return `${location.driveFileId} — ${token.name}`;
|
|
|
|
},
|
2017-09-17 15:32:39 +00:00
|
|
|
downloadContent(token, syncLocation) {
|
|
|
|
return googleHelper.downloadFile(token, syncLocation.driveFileId)
|
2017-09-26 22:54:26 +00:00
|
|
|
.then(content => providerUtils.parseContent(content, syncLocation));
|
2017-09-17 15:32:39 +00:00
|
|
|
},
|
2017-09-23 19:01:50 +00:00
|
|
|
uploadContent(token, content, syncLocation, ifNotTooLate) {
|
2017-09-17 15:32:39 +00:00
|
|
|
const file = store.state.file.itemMap[syncLocation.fileId];
|
2017-10-09 07:11:18 +00:00
|
|
|
const name = utils.sanitizeName(file && file.name);
|
2017-09-17 15:32:39 +00:00
|
|
|
const parents = [];
|
|
|
|
if (syncLocation.driveParentId) {
|
|
|
|
parents.push(syncLocation.driveParentId);
|
|
|
|
}
|
2017-09-23 19:01:50 +00:00
|
|
|
return googleHelper.uploadFile(
|
2017-09-17 15:32:39 +00:00
|
|
|
token,
|
|
|
|
name,
|
|
|
|
parents,
|
2017-09-23 19:01:50 +00:00
|
|
|
providerUtils.serializeContent(content),
|
|
|
|
undefined,
|
|
|
|
syncLocation.driveFileId,
|
2017-09-17 15:32:39 +00:00
|
|
|
ifNotTooLate,
|
|
|
|
)
|
|
|
|
.then(driveFile => ({
|
|
|
|
...syncLocation,
|
|
|
|
driveFileId: driveFile.id,
|
|
|
|
}));
|
|
|
|
},
|
2017-09-23 19:01:50 +00:00
|
|
|
publish(token, html, metadata, publishLocation) {
|
|
|
|
return googleHelper.uploadFile(
|
|
|
|
token,
|
|
|
|
metadata.title,
|
|
|
|
[],
|
|
|
|
html,
|
|
|
|
publishLocation.templateId ? 'text/html' : undefined,
|
|
|
|
publishLocation.driveFileId,
|
|
|
|
)
|
|
|
|
.then(driveFile => ({
|
|
|
|
...publishLocation,
|
|
|
|
driveFileId: driveFile.id,
|
|
|
|
}));
|
|
|
|
},
|
2017-10-14 11:39:15 +00:00
|
|
|
openFiles(token, driveFiles) {
|
2017-09-17 15:32:39 +00:00
|
|
|
const openOneFile = () => {
|
2017-10-14 11:39:15 +00:00
|
|
|
const driveFile = driveFiles.pop();
|
|
|
|
if (!driveFile) {
|
2017-09-17 15:32:39 +00:00
|
|
|
return null;
|
|
|
|
}
|
2017-10-14 11:39:15 +00:00
|
|
|
if (providerUtils.openFileWithLocation(store.getters['syncLocation/items'], {
|
|
|
|
providerId: this.id,
|
|
|
|
driveFileId: driveFile.id,
|
|
|
|
})) {
|
|
|
|
// File exists and has just been opened. Next...
|
2017-09-17 15:32:39 +00:00
|
|
|
return openOneFile();
|
|
|
|
}
|
2017-10-14 11:39:15 +00:00
|
|
|
// Download content from Google Drive and create the file
|
|
|
|
const syncLocation = {
|
|
|
|
driveFileId: driveFile.id,
|
2017-09-23 19:01:50 +00:00
|
|
|
providerId: this.id,
|
2017-09-17 15:32:39 +00:00
|
|
|
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`,
|
|
|
|
});
|
|
|
|
store.commit('file/setItem', {
|
|
|
|
id,
|
2017-10-14 11:39:15 +00:00
|
|
|
name: utils.sanitizeName(driveFile.name),
|
2017-09-17 15:32:39 +00:00
|
|
|
parentId: store.getters['file/current'].parentId,
|
|
|
|
});
|
|
|
|
store.commit('syncLocation/setItem', {
|
|
|
|
...syncLocation,
|
|
|
|
id: utils.uid(),
|
|
|
|
fileId: id,
|
|
|
|
});
|
|
|
|
store.commit('file/setCurrentId', id);
|
2017-09-23 19:01:50 +00:00
|
|
|
store.dispatch('notification/info', `${store.getters['file/current'].name} was imported from Google Drive.`);
|
2017-09-17 15:32:39 +00:00
|
|
|
}, () => {
|
2017-10-14 11:39:15 +00:00
|
|
|
store.dispatch('notification/error', `Could not open file ${driveFile.id}.`);
|
2017-09-17 15:32:39 +00:00
|
|
|
})
|
|
|
|
.then(() => openOneFile());
|
|
|
|
};
|
|
|
|
return Promise.resolve()
|
|
|
|
.then(() => openOneFile());
|
|
|
|
},
|
2017-09-23 19:01:50 +00:00
|
|
|
makeLocation(token, fileId, folderId) {
|
|
|
|
const location = {
|
|
|
|
providerId: this.id,
|
|
|
|
sub: token.sub,
|
|
|
|
};
|
|
|
|
if (fileId) {
|
|
|
|
location.driveFileId = fileId;
|
|
|
|
}
|
|
|
|
if (folderId) {
|
|
|
|
location.driveParentId = folderId;
|
|
|
|
}
|
|
|
|
return location;
|
|
|
|
},
|
|
|
|
});
|