Provider.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. 'use strict'
  2. const RequestClient = require('./RequestClient')
  3. const tokenStorage = require('./tokenStorage')
  4. const _getName = (id) => {
  5. return id.split('-').map((s) => s.charAt(0).toUpperCase() + s.slice(1)).join(' ')
  6. }
  7. module.exports = class Provider extends RequestClient {
  8. constructor (uppy, opts) {
  9. super(uppy, opts)
  10. this.provider = opts.provider
  11. this.id = this.provider
  12. this.authProvider = opts.authProvider || this.provider
  13. this.name = this.opts.name || _getName(this.id)
  14. this.pluginId = this.opts.pluginId
  15. this.tokenKey = `companion-${this.pluginId}-auth-token`
  16. }
  17. headers () {
  18. return new Promise((resolve, reject) => {
  19. super.headers().then((headers) => {
  20. this.getAuthToken().then((token) => {
  21. resolve(Object.assign({}, headers, { 'uppy-auth-token': token }))
  22. })
  23. }).catch(reject)
  24. })
  25. }
  26. onReceiveResponse (response) {
  27. response = super.onReceiveResponse(response)
  28. const authenticated = response.status !== 401
  29. this.uppy.getPlugin(this.pluginId).setPluginState({ authenticated })
  30. return response
  31. }
  32. // @todo(i.olarewaju) consider whether or not this method should be exposed
  33. setAuthToken (token) {
  34. return this.uppy.getPlugin(this.pluginId).storage.setItem(this.tokenKey, token)
  35. }
  36. getAuthToken () {
  37. return this.uppy.getPlugin(this.pluginId).storage.getItem(this.tokenKey)
  38. }
  39. authUrl () {
  40. return `${this.hostname}/${this.id}/connect`
  41. }
  42. fileUrl (id) {
  43. return `${this.hostname}/${this.id}/get/${id}`
  44. }
  45. list (directory) {
  46. return this.get(`${this.id}/list/${directory || ''}`)
  47. }
  48. logout (redirect = location.href) {
  49. return new Promise((resolve, reject) => {
  50. this.get(`${this.id}/logout?redirect=${redirect}`)
  51. .then((res) => {
  52. this.uppy.getPlugin(this.pluginId).storage.removeItem(this.tokenKey)
  53. .then(() => resolve(res))
  54. .catch(reject)
  55. }).catch(reject)
  56. })
  57. }
  58. static initPlugin (plugin, opts, defaultOpts) {
  59. plugin.type = 'acquirer'
  60. plugin.files = []
  61. if (defaultOpts) {
  62. plugin.opts = Object.assign({}, defaultOpts, opts)
  63. }
  64. if (opts.serverUrl || opts.serverPattern) {
  65. throw new Error('`serverUrl` and `serverPattern` have been renamed to `companionUrl` and `companionAllowedHosts` respectively in the 0.30.5 release. Please consult the docs (for example, https://uppy.io/docs/instagram/ for the Instagram plugin) and use the updated options.`')
  66. }
  67. if (opts.companionAllowedHosts) {
  68. const pattern = opts.companionAllowedHosts
  69. // validate companionAllowedHosts param
  70. if (typeof pattern !== 'string' && !Array.isArray(pattern) && !(pattern instanceof RegExp)) {
  71. throw new TypeError(`${plugin.id}: the option "companionAllowedHosts" must be one of string, Array, RegExp`)
  72. }
  73. plugin.opts.companionAllowedHosts = pattern
  74. } else {
  75. // does not start with https://
  76. if (/^(?!https?:\/\/).*$/i.test(opts.companionUrl)) {
  77. plugin.opts.companionAllowedHosts = `https://${opts.companionUrl.replace(/^\/\//, '')}`
  78. } else {
  79. plugin.opts.companionAllowedHosts = opts.companionUrl
  80. }
  81. }
  82. plugin.storage = plugin.opts.storage || tokenStorage
  83. }
  84. }