mirror of
https://github.com/transloadit/uppy.git
synced 2026-08-02 14:52:24 +00:00
124 lines
2.9 KiB
JavaScript
124 lines
2.9 KiB
JavaScript
const Plugin = require('../../core/Plugin')
|
|
const { Provider } = require('../../server')
|
|
const { ProviderView } = require('../../views')
|
|
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, {
|
|
serverUrl: this.opts.serverUrl,
|
|
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)
|
|
this.opts.serverPattern = opts.serverPattern || opts.serverUrl
|
|
}
|
|
|
|
install () {
|
|
this.view = new ProviderView(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')
|
|
}
|
|
}
|
|
|
|
getUsername (data) {
|
|
for (const item of data.items) {
|
|
if (item.userPermission.role === 'owner') {
|
|
for (const owner of item.owners) {
|
|
if (owner.isAuthenticatedUser) {
|
|
return owner.emailAddress
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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.serverUrl}/${this.GoogleDrive.id}/thumbnail/${this.getItemRequestPath(item)}`
|
|
}
|
|
|
|
render (state) {
|
|
return this.view.render(state)
|
|
}
|
|
}
|