UNPKG

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:

92 lines (80 loc) 2.29 kB
'use strict' require('whatwg-fetch') const _getName = (id) => { return id.split('-').map((s) => s.charAt(0).toUpperCase() + s.slice(1)).join(' ') } module.exports = class Provider { constructor (uppy, opts) { this.uppy = uppy this.opts = opts this.provider = opts.provider this.id = this.provider this.authProvider = opts.authProvider || this.provider this.name = this.opts.name || _getName(this.id) this.onReceiveResponse = this.onReceiveResponse.bind(this) } get hostname () { const uppyServer = this.uppy.state.uppyServer || {} const host = this.opts.host return uppyServer[host] || host } onReceiveResponse (response) { const uppyServer = this.uppy.state.uppyServer || {} const host = this.opts.host const headers = response.headers // Store the self-identified domain name for the uppy-server we just hit. if (headers.has('i-am') && headers.get('i-am') !== uppyServer[host]) { this.uppy.setState({ uppyServer: Object.assign({}, uppyServer, { [host]: headers.get('i-am') }) }) } return response } checkAuth () { return fetch(`${this.hostname}/${this.id}/authorized`, { method: 'get', credentials: 'include', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' } }) .then(this.onReceiveResponse) .then((res) => { return res.json() .then((payload) => { return payload.authenticated }) }) } authUrl () { return `${this.hostname}/${this.id}/connect` } fileUrl (id) { return `${this.hostname}/${this.id}/get/${id}` } list (directory) { return fetch(`${this.hostname}/${this.id}/list/${directory || ''}`, { method: 'get', credentials: 'include', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' } }) .then(this.onReceiveResponse) .then((res) => res.json()) } logout (redirect = location.href) { return fetch(`${this.hostname}/${this.id}/logout?redirect=${redirect}`, { method: 'get', credentials: 'include', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' } }) } }