index.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 toArray = require('@uppy/utils/lib/toArray')
  7. /**
  8. * Url
  9. *
  10. */
  11. module.exports = class Url extends Plugin {
  12. constructor (uppy, opts) {
  13. super(uppy, opts)
  14. this.id = this.opts.id || 'Url'
  15. this.title = this.opts.title || 'Link'
  16. this.type = 'acquirer'
  17. this.icon = () => <svg aria-hidden="true" width="23" height="23" viewBox="0 0 23 23">
  18. <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" />
  19. </svg>
  20. // Set default options and locale
  21. this.defaultLocale = {
  22. strings: {
  23. import: 'Import',
  24. enterUrlToImport: 'Enter URL to import a file',
  25. failedToFetch: 'Companion failed to fetch this URL, please make sure it’s correct',
  26. enterCorrectUrl: 'Incorrect URL: Please make sure you are entering a direct link to a file'
  27. }
  28. }
  29. const defaultOptions = {}
  30. this.opts = Object.assign({}, defaultOptions, opts)
  31. // i18n
  32. this.translator = new Translator([ this.defaultLocale, this.uppy.locale, this.opts.locale ])
  33. this.i18n = this.translator.translate.bind(this.translator)
  34. this.i18nArray = this.translator.translateArray.bind(this.translator)
  35. this.hostname = this.opts.companionUrl
  36. if (!this.hostname) {
  37. throw new Error('Companion hostname is required, please consult https://uppy.io/docs/companion')
  38. }
  39. // Bind all event handlers for referencability
  40. this.getMeta = this.getMeta.bind(this)
  41. this.addFile = this.addFile.bind(this)
  42. this.handleDrop = this.handleDrop.bind(this)
  43. this.handleDragOver = this.handleDragOver.bind(this)
  44. this.handleDragLeave = this.handleDragLeave.bind(this)
  45. this.handlePaste = this.handlePaste.bind(this)
  46. this.client = new RequestClient(uppy, {
  47. companionUrl: this.opts.companionUrl,
  48. serverHeaders: this.opts.serverHeaders
  49. })
  50. }
  51. getFileNameFromUrl (url) {
  52. return url.substring(url.lastIndexOf('/') + 1)
  53. }
  54. checkIfCorrectURL (url) {
  55. if (!url) return false
  56. const protocol = url.match(/^([a-z0-9]+):\/\//)[1]
  57. if (protocol !== 'http' && protocol !== 'https') {
  58. return false
  59. }
  60. return true
  61. }
  62. addProtocolToURL (url) {
  63. const protocolRegex = /^[a-z0-9]+:\/\//
  64. const defaultProtocol = 'http://'
  65. if (protocolRegex.test(url)) {
  66. return url
  67. }
  68. return defaultProtocol + url
  69. }
  70. getMeta (url) {
  71. return this.client.post('url/meta', { url })
  72. .then((res) => {
  73. if (res.error) {
  74. this.uppy.log('[URL] Error:')
  75. this.uppy.log(res.error)
  76. throw new Error('Failed to fetch the file')
  77. }
  78. return res
  79. })
  80. }
  81. addFile (url) {
  82. url = this.addProtocolToURL(url)
  83. if (!this.checkIfCorrectURL(url)) {
  84. this.uppy.log(`[URL] Incorrect URL entered: ${url}`)
  85. this.uppy.info(this.i18n('enterCorrectUrl'), 'error', 4000)
  86. return
  87. }
  88. return this.getMeta(url)
  89. .then((meta) => {
  90. const tagFile = {
  91. source: this.id,
  92. name: this.getFileNameFromUrl(url),
  93. type: meta.type,
  94. data: {
  95. size: meta.size
  96. },
  97. isRemote: true,
  98. body: {
  99. url: url
  100. },
  101. remote: {
  102. companionUrl: this.opts.companionUrl,
  103. url: `${this.hostname}/url/get`,
  104. body: {
  105. fileId: url,
  106. url: url
  107. },
  108. providerOptions: this.client.opts
  109. }
  110. }
  111. return tagFile
  112. })
  113. .then((tagFile) => {
  114. this.uppy.log('[Url] Adding remote file')
  115. try {
  116. this.uppy.addFile(tagFile)
  117. } catch (err) {
  118. // Nothing, restriction errors handled in Core
  119. }
  120. })
  121. .then(() => {
  122. // Close the Dashboard panel if plugin is installed
  123. // into Dashboard (could be other parent UI plugin)
  124. // if (this.parent && this.parent.hideAllPanels) {
  125. // this.parent.hideAllPanels()
  126. // }
  127. })
  128. .catch((err) => {
  129. this.uppy.log(err)
  130. this.uppy.info({
  131. message: this.i18n('failedToFetch'),
  132. details: err
  133. }, 'error', 4000)
  134. })
  135. }
  136. handleDrop (e) {
  137. e.preventDefault()
  138. if (e.dataTransfer.items) {
  139. const items = toArray(e.dataTransfer.items)
  140. items.forEach((item) => {
  141. if (item.kind === 'string' && item.type === 'text/uri-list') {
  142. item.getAsString((url) => {
  143. this.uppy.log(`[URL] Adding file from dropped url: ${url}`)
  144. this.addFile(url)
  145. })
  146. }
  147. })
  148. }
  149. }
  150. handleDragOver (e) {
  151. e.preventDefault()
  152. this.el.classList.add('drag')
  153. }
  154. handleDragLeave (e) {
  155. e.preventDefault()
  156. this.el.classList.remove('drag')
  157. }
  158. handlePaste (e) {
  159. if (!e.clipboardData.items) {
  160. return
  161. }
  162. const items = toArray(e.clipboardData.items)
  163. // When a file is pasted, it appears as two items: file name string, then
  164. // the file itself; Url then treats file name string as URL, which is wrong.
  165. // This makes sure Url ignores paste event if it contains an actual file
  166. const hasFiles = items.filter(item => item.kind === 'file').length > 0
  167. if (hasFiles) return
  168. items.forEach((item) => {
  169. if (item.kind === 'string' && item.type === 'text/plain') {
  170. item.getAsString((url) => {
  171. this.uppy.log(`[URL] Adding file from pasted url: ${url}`)
  172. this.addFile(url)
  173. })
  174. }
  175. })
  176. }
  177. render (state) {
  178. return <UrlUI
  179. i18n={this.i18n}
  180. addFile={this.addFile} />
  181. }
  182. onMount () {
  183. if (this.el) {
  184. this.el.addEventListener('drop', this.handleDrop)
  185. this.el.addEventListener('dragover', this.handleDragOver)
  186. this.el.addEventListener('dragleave', this.handleDragLeave)
  187. this.el.addEventListener('paste', this.handlePaste)
  188. }
  189. }
  190. install () {
  191. const target = this.opts.target
  192. if (target) {
  193. this.mount(target, this)
  194. }
  195. }
  196. uninstall () {
  197. if (this.el) {
  198. this.el.removeEventListener('drop', this.handleDrop)
  199. this.el.removeEventListener('dragover', this.handleDragOver)
  200. this.el.removeEventListener('dragleave', this.handleDragLeave)
  201. this.el.removeEventListener('paste', this.handlePaste)
  202. }
  203. this.unmount()
  204. }
  205. }