index.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. const Plugin = require('../../core/Plugin')
  2. const Translator = require('../../core/Translator')
  3. const { h } = require('preact')
  4. const { RequestClient } = require('../../server')
  5. const UrlUI = require('./UrlUI.js')
  6. const { toArray } = require('../../core/Utils')
  7. require('whatwg-fetch')
  8. /**
  9. * Url
  10. *
  11. */
  12. module.exports = class Url extends Plugin {
  13. constructor (uppy, opts) {
  14. super(uppy, opts)
  15. this.id = this.opts.id || 'Url'
  16. this.title = 'Link'
  17. this.type = 'acquirer'
  18. this.icon = () => <svg aria-hidden="true" class="UppyIcon UppyModalTab-icon" width="64" height="64" viewBox="0 0 64 64">
  19. <circle cx="32" cy="32" r="31" />
  20. <g fill-rule="nonzero" fill="#FFF">
  21. <path d="M25.774 47.357a4.077 4.077 0 0 1-5.76 0L16.9 44.24a4.076 4.076 0 0 1 0-5.758l5.12-5.12-1.817-1.818-5.12 5.122a6.651 6.651 0 0 0 0 9.392l3.113 3.116a6.626 6.626 0 0 0 4.699 1.943c1.7 0 3.401-.649 4.697-1.943l10.241-10.243a6.591 6.591 0 0 0 1.947-4.696 6.599 6.599 0 0 0-1.947-4.696l-3.116-3.114-1.817 1.817 3.116 3.114a4.045 4.045 0 0 1 1.194 2.88 4.045 4.045 0 0 1-1.194 2.878L25.774 47.357z" />
  22. <path d="M46.216 14.926a6.597 6.597 0 0 0-4.696-1.946h-.001a6.599 6.599 0 0 0-4.696 1.945L26.582 25.167a6.595 6.595 0 0 0-1.947 4.697 6.599 6.599 0 0 0 1.946 4.698l3.114 3.114 1.818-1.816-3.114-3.114a4.05 4.05 0 0 1-1.194-2.882c0-1.086.424-2.108 1.194-2.878L38.64 16.744a4.042 4.042 0 0 1 2.88-1.194c1.089 0 2.11.425 2.88 1.194l3.114 3.114a4.076 4.076 0 0 1 0 5.758l-5.12 5.12 1.818 1.817 5.12-5.122a6.649 6.649 0 0 0 0-9.393l-3.113-3.114-.003.002z" />
  23. </g>
  24. </svg>
  25. // Set default options and locale
  26. const defaultLocale = {
  27. strings: {
  28. import: 'Import',
  29. enterUrlToImport: 'Enter URL to import a file',
  30. failedToFetch: 'Uppy Server failed to fetch this URL, please make sure it’s correct',
  31. enterCorrectUrl: 'Incorrect URL: Please make sure you are entering a direct link to a file'
  32. }
  33. }
  34. const defaultOptions = {
  35. locale: defaultLocale
  36. }
  37. this.opts = Object.assign({}, defaultOptions, opts)
  38. this.locale = Object.assign({}, defaultLocale, this.opts.locale)
  39. this.locale.strings = Object.assign({}, defaultLocale.strings, this.opts.locale.strings)
  40. this.translator = new Translator({locale: this.locale})
  41. this.i18n = this.translator.translate.bind(this.translator)
  42. this.hostname = this.opts.host
  43. if (!this.hostname) {
  44. throw new Error('Uppy Server hostname is required, please consult https://uppy.io/docs/server')
  45. }
  46. // Bind all event handlers for referencability
  47. this.getMeta = this.getMeta.bind(this)
  48. this.addFile = this.addFile.bind(this)
  49. this.handleDrop = this.handleDrop.bind(this)
  50. this.handleDragOver = this.handleDragOver.bind(this)
  51. this.handleDragLeave = this.handleDragLeave.bind(this)
  52. this.handlePaste = this.handlePaste.bind(this)
  53. this.server = new RequestClient(uppy, {host: this.opts.host})
  54. }
  55. getFileNameFromUrl (url) {
  56. return url.substring(url.lastIndexOf('/') + 1)
  57. }
  58. checkIfCorrectURL (url) {
  59. if (!url) return false
  60. const protocol = url.match(/^([a-z0-9]+):\/\//)[1]
  61. if (protocol !== 'http' && protocol !== 'https') {
  62. return false
  63. }
  64. return true
  65. }
  66. addProtocolToURL (url) {
  67. const protocolRegex = /^[a-z0-9]+:\/\//
  68. const defaultProtocol = 'http://'
  69. if (protocolRegex.test(url)) {
  70. return url
  71. }
  72. return defaultProtocol + url
  73. }
  74. getMeta (url) {
  75. return this.server.post('url/meta', { url })
  76. .then((res) => {
  77. if (res.error) {
  78. this.uppy.log('[URL] Error:')
  79. this.uppy.log(res.error)
  80. throw new Error('Failed to fetch the file')
  81. }
  82. return res
  83. })
  84. }
  85. addFile (url) {
  86. url = this.addProtocolToURL(url)
  87. if (!this.checkIfCorrectURL(url)) {
  88. this.uppy.log(`[URL] Incorrect URL entered: ${url}`)
  89. this.uppy.info(this.i18n('enterCorrectUrl'), 'error', 4000)
  90. return
  91. }
  92. return this.getMeta(url)
  93. .then((meta) => {
  94. const tagFile = {
  95. source: this.id,
  96. name: this.getFileNameFromUrl(url),
  97. type: meta.type,
  98. data: {
  99. size: meta.size
  100. },
  101. isRemote: true,
  102. body: {
  103. url: url
  104. },
  105. remote: {
  106. host: this.opts.host,
  107. url: `${this.hostname}/url/get`,
  108. body: {
  109. fileId: url,
  110. url: url
  111. }
  112. }
  113. }
  114. return tagFile
  115. })
  116. .then((tagFile) => {
  117. this.uppy.log('[Url] Adding remote file')
  118. try {
  119. this.uppy.addFile(tagFile)
  120. } catch (err) {
  121. // Nothing, restriction errors handled in Core
  122. }
  123. })
  124. .then(() => {
  125. const dashboard = this.uppy.getPlugin('Dashboard')
  126. if (dashboard) dashboard.hideAllPanels()
  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. const items = toArray(e.clipboardData.items)
  161. // When a file is pasted, it appears as two items: file name string, then
  162. // the file itself; Url then treats file name string as URL, which is wrong.
  163. // This makes sure Url ignores paste event if it contains an actual file
  164. const hasFiles = items.filter(item => item.kind === 'file').length > 0
  165. if (hasFiles) return
  166. items.forEach((item) => {
  167. if (item.kind === 'string' && item.type === 'text/plain') {
  168. item.getAsString((url) => {
  169. this.uppy.log(`[URL] Adding file from pasted url: ${url}`)
  170. this.addFile(url)
  171. })
  172. }
  173. })
  174. }
  175. }
  176. render (state) {
  177. return <UrlUI
  178. i18n={this.i18n}
  179. addFile={this.addFile} />
  180. }
  181. install () {
  182. const target = this.opts.target
  183. if (target) {
  184. this.mount(target, this)
  185. }
  186. this.el.addEventListener('drop', this.handleDrop)
  187. this.el.addEventListener('dragover', this.handleDragOver)
  188. this.el.addEventListener('dragleave', this.handleDragLeave)
  189. this.el.addEventListener('paste', this.handlePaste)
  190. }
  191. uninstall () {
  192. this.el.removeEventListener('drop', this.handleDrop)
  193. this.el.removeEventListener('dragover', this.handleDragOver)
  194. this.el.removeEventListener('dragleave', this.handleDragLeave)
  195. this.el.removeEventListener('paste', this.handlePaste)
  196. this.unmount()
  197. }
  198. }