import Utils from '../core/Utils' import Plugin from './Plugin' export default class Google extends Plugin { constructor (core, opts) { super(core, opts) this.type = 'acquirer' this.files = [] this.name = 'Google Drive' this.icon = ` ` this.authUrl = 'http://localhost:3020/connect/google' // set default options const defaultOptions = {} // merge default options with the ones set by user this.opts = Object.assign({}, defaultOptions, opts) this.currentFolder = 'root' this.isAuthenticated = false this.checkAuthentication() } focus () { this.checkAuthentication() .then((res) => { if (!this.isAuthenticated) { this.target.innerHTML = this.renderAuth() } else { this.renderFolder() } }) .catch((err) => { this.target.innerHTML = this.renderError(err) }) } checkAuthentication () { return fetch('http://localhost:3020/google/authorize', { method: 'get', credentials: 'include', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' } }) .then((res) => { if (res.status >= 200 && res.status <= 300) { return res.json().then((data) => { this.isAuthenticated = data.isAuthenticated }) } else { let error = new Error(res.statusText) error.response = res throw error } }) .catch((err) => { this.target.innerHTML = this.renderError(err) }) } getFolder (folderId = this.currentFolder) { /** * Leave this here */ // fetch('http://localhost:3020/google/logout', { // method: 'get', // credentials: 'include', // headers: { // 'Accept': 'application/json', // 'Content-Type': 'application/json' // } // }).then(res => console.log(res)) return fetch('http://localhost:3020/google/list', { method: 'get', credentials: 'include', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: { dir: folderId || undefined } }) .then((res) => { if (res.status >= 200 && res.status <= 300) { return res.json().then((data) => { let folders = [] let files = [] data.items.forEach((item) => { if (item.mimeType === 'application/vnd.google-apps.folder') { folders.push(item) } else { files.push(item) } }) return { folders, files } }) } }) } getFile (fileId) { if (fileId !== 'string') { return console.log('Error: File Id not a string.') } return fetch('http://localhost:3020/google/get', { method: 'get', credentials: 'include', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: { fileId } }) } install () { const caller = this this.target = document.querySelector(this.getTarget(this.opts.target, caller)) return } renderAuth () { return `