index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. const { Plugin } = require('@uppy/core')
  2. const Translator = require('@uppy/utils/lib/Translator')
  3. const { h } = require('preact')
  4. const { RequestClient } = require('@uppy/companion-client')
  5. const UrlUI = require('./UrlUI.js')
  6. const forEachDroppedOrPastedUrl = require('./utils/forEachDroppedOrPastedUrl')
  7. /**
  8. * Url
  9. *
  10. */
  11. module.exports = class Url extends Plugin {
  12. static VERSION = require('../package.json').version
  13. constructor (uppy, opts) {
  14. super(uppy, opts)
  15. this.id = this.opts.id || 'Url'
  16. this.title = this.opts.title || 'Link'
  17. this.type = 'acquirer'
  18. this.icon = () => <svg aria-hidden="true" focusable="false" width="23" height="23" viewBox="0 0 23 23">
  19. <path d="M20.485 11.236l-2.748 2.737c-.184.182-.367.365-.642.547-1.007.73-2.107 1.095-3.298 1.095-1.65 0-3.298-.73-4.398-2.19-.275-.365-.183-1.003.183-1.277.367-.273 1.008-.182 1.283.183 1.191 1.642 3.482 1.915 5.13.73a.714.714 0 0 0 .367-.365l2.75-2.737c1.373-1.46 1.373-3.74-.093-5.108a3.72 3.72 0 0 0-5.13 0L12.33 6.4a.888.888 0 0 1-1.283 0 .88.88 0 0 1 0-1.277l1.558-1.55a5.38 5.38 0 0 1 7.605 0c2.29 2.006 2.382 5.564.274 7.662zm-8.979 6.294L9.95 19.081a3.72 3.72 0 0 1-5.13 0c-1.467-1.368-1.467-3.74-.093-5.108l2.75-2.737.366-.365c.824-.547 1.74-.82 2.748-.73 1.008.183 1.833.639 2.382 1.46.275.365.917.456 1.283.182.367-.273.458-.912.183-1.277-.916-1.186-2.199-1.915-3.573-2.098-1.374-.273-2.84.091-4.031 1.004l-.55.547-2.749 2.737c-2.107 2.189-2.015 5.655.092 7.753C4.727 21.453 6.101 22 7.475 22c1.374 0 2.749-.547 3.848-1.55l1.558-1.551a.88.88 0 0 0 0-1.278c-.367-.364-1.008-.456-1.375-.09z" fill="#FF814F" fill-rule="nonzero" />
  20. </svg>
  21. // Set default options and locale
  22. this.defaultLocale = {
  23. strings: {
  24. import: 'Import',
  25. enterUrlToImport: 'Enter URL to import a file',
  26. failedToFetch: 'Companion failed to fetch this URL, please make sure it’s correct',
  27. enterCorrectUrl: 'Incorrect URL: Please make sure you are entering a direct link to a file'
  28. }
  29. }
  30. const defaultOptions = {}
  31. this.opts = Object.assign({}, defaultOptions, opts)
  32. // i18n
  33. this.translator = new Translator([ this.defaultLocale, this.uppy.locale, this.opts.locale ])
  34. this.i18n = this.translator.translate.bind(this.translator)
  35. this.i18nArray = this.translator.translateArray.bind(this.translator)
  36. this.hostname = this.opts.companionUrl
  37. if (!this.hostname) {
  38. throw new Error('Companion hostname is required, please consult https://uppy.io/docs/companion')
  39. }
  40. // Bind all event handlers for referencability
  41. this.getMeta = this.getMeta.bind(this)
  42. this.addFile = this.addFile.bind(this)
  43. this.handleRootDrop = this.handleRootDrop.bind(this)
  44. this.handleRootPaste = this.handleRootPaste.bind(this)
  45. this.client = new RequestClient(uppy, {
  46. companionUrl: this.opts.companionUrl,
  47. serverHeaders: this.opts.serverHeaders
  48. })
  49. }
  50. getFileNameFromUrl (url) {
  51. return url.substring(url.lastIndexOf('/') + 1)
  52. }
  53. checkIfCorrectURL (url) {
  54. if (!url) return false
  55. const protocol = url.match(/^([a-z0-9]+):\/\//)[1]
  56. if (protocol !== 'http' && protocol !== 'https') {
  57. return false
  58. }
  59. return true
  60. }
  61. addProtocolToURL (url) {
  62. const protocolRegex = /^[a-z0-9]+:\/\//
  63. const defaultProtocol = 'http://'
  64. if (protocolRegex.test(url)) {
  65. return url
  66. }
  67. return defaultProtocol + url
  68. }
  69. getMeta (url) {
  70. return this.client.post('url/meta', { url })
  71. .then((res) => {
  72. if (res.error) {
  73. this.uppy.log('[URL] Error:')
  74. this.uppy.log(res.error)
  75. throw new Error('Failed to fetch the file')
  76. }
  77. return res
  78. })
  79. }
  80. addFile (url) {
  81. url = this.addProtocolToURL(url)
  82. if (!this.checkIfCorrectURL(url)) {
  83. this.uppy.log(`[URL] Incorrect URL entered: ${url}`)
  84. this.uppy.info(this.i18n('enterCorrectUrl'), 'error', 4000)
  85. return
  86. }
  87. return this.getMeta(url)
  88. .then((meta) => {
  89. const tagFile = {
  90. source: this.id,
  91. name: this.getFileNameFromUrl(url),
  92. type: meta.type,
  93. data: {
  94. size: meta.size
  95. },
  96. isRemote: true,
  97. body: {
  98. url: url
  99. },
  100. remote: {
  101. companionUrl: this.opts.companionUrl,
  102. url: `${this.hostname}/url/get`,
  103. body: {
  104. fileId: url,
  105. url: url
  106. },
  107. providerOptions: this.client.opts
  108. }
  109. }
  110. return tagFile
  111. })
  112. .then((tagFile) => {
  113. this.uppy.log('[Url] Adding remote file')
  114. try {
  115. this.uppy.addFile(tagFile)
  116. } catch (err) {
  117. if (!err.isRestriction) {
  118. this.uppy.log(err)
  119. }
  120. }
  121. })
  122. .catch((err) => {
  123. this.uppy.log(err)
  124. this.uppy.info({
  125. message: this.i18n('failedToFetch'),
  126. details: err
  127. }, 'error', 4000)
  128. })
  129. }
  130. handleRootDrop (e) {
  131. forEachDroppedOrPastedUrl(e.dataTransfer, 'drop', (url) => {
  132. this.uppy.log(`[URL] Adding file from dropped url: ${url}`)
  133. this.addFile(url)
  134. })
  135. }
  136. handleRootPaste (e) {
  137. forEachDroppedOrPastedUrl(e.clipboardData, 'paste', (url) => {
  138. this.uppy.log(`[URL] Adding file from pasted url: ${url}`)
  139. this.addFile(url)
  140. })
  141. }
  142. render (state) {
  143. return <UrlUI
  144. i18n={this.i18n}
  145. addFile={this.addFile} />
  146. }
  147. install () {
  148. const target = this.opts.target
  149. if (target) {
  150. this.mount(target, this)
  151. }
  152. }
  153. uninstall () {
  154. this.unmount()
  155. }
  156. }