uppy
Version:
Extensible JavaScript file upload widget with support for drag&drop, resumable uploads, previews, restrictions, file processing/encoding, remote providers like Instagram, Dropbox, Google Drive, S3 and more :dog:
112 lines (90 loc) • 2.59 kB
JavaScript
const Plugin = require('../../core/Plugin')
const Provider = require('../Provider')
const View = require('../Provider/view')
const { h } = require('preact')
module.exports = class GoogleDrive extends Plugin {
constructor (uppy, opts) {
super(uppy, opts)
this.type = 'acquirer'
this.id = this.opts.id || 'GoogleDrive'
this.title = 'Google Drive'
this.icon = () =>
<svg aria-hidden="true" class="UppyIcon UppyModalTab-icon" width="28" height="28" viewBox="0 0 16 16">
<path d="M2.955 14.93l2.667-4.62H16l-2.667 4.62H2.955zm2.378-4.62l-2.666 4.62L0 10.31l5.19-8.99 2.666 4.62-2.523 4.37zm10.523-.25h-5.333l-5.19-8.99h5.334l5.19 8.99z" />
</svg>
this[this.id] = new Provider(uppy, {
host: this.opts.host,
provider: 'drive',
authProvider: 'google'
})
this.files = []
this.onAuth = this.onAuth.bind(this)
this.render = this.render.bind(this)
// set default options
const defaultOptions = {}
// merge default options with the ones set by user
this.opts = Object.assign({}, defaultOptions, opts)
}
install () {
this.view = new View(this)
// Set default state for Google Drive
this.setPluginState({
authenticated: false,
files: [],
folders: [],
directories: [],
activeRow: -1,
filterInput: '',
isSearchVisible: false
})
const target = this.opts.target
if (target) {
this.mount(target, this)
}
}
uninstall () {
this.view.tearDown()
this.unmount()
}
onAuth (authenticated) {
this.setPluginState({ authenticated })
if (authenticated) {
this.view.getFolder('root')
}
}
isFolder (item) {
return item.mimeType === 'application/vnd.google-apps.folder'
}
getItemData (item) {
return Object.assign({}, item, {size: parseFloat(item.fileSize)})
}
getItemIcon (item) {
return <img src={item.iconLink} />
}
getItemSubList (item) {
return item.items.filter((i) => {
return this.isFolder(i) || !i.mimeType.startsWith('application/vnd.google')
})
}
getItemName (item) {
return item.title ? item.title : '/'
}
getMimeType (item) {
return item.mimeType
}
getItemId (item) {
return item.id
}
getItemRequestPath (item) {
return this.getItemId(item)
}
getItemModifiedDate (item) {
return item.modifiedByMeDate
}
getItemThumbnailUrl (item) {
return `${this.opts.host}/${this.GoogleDrive.id}/thumbnail/${this.getItemRequestPath(item)}`
}
render (state) {
return this.view.render(state)
}
}