123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- const Plugin = require('./Plugin')
- const UppySocket = require('../core/UppySocket')
- const {
- emitSocketProgress,
- getSocketHost,
- settle
- } = require('../core/Utils')
- module.exports = class XHRUpload extends Plugin {
- constructor (core, opts) {
- super(core, opts)
- this.type = 'uploader'
- this.id = 'XHRUpload'
- this.title = 'XHRUpload'
- // Default options
- const defaultOptions = {
- formData: true,
- fieldName: 'files[]',
- method: 'post',
- metaFields: null,
- responseUrlFieldName: 'url',
- bundle: true,
- headers: {},
- getResponseData (xhr) {
- return JSON.parse(xhr.response)
- },
- getResponseError (xhr) {
- return new Error('Upload error')
- }
- }
- // Merge default options with the ones set by user
- this.opts = Object.assign({}, defaultOptions, opts)
- this.handleUpload = this.handleUpload.bind(this)
- }
- getOptions (file) {
- const opts = Object.assign({},
- this.opts,
- this.core.state.xhrUpload || {},
- file.xhrUpload || {}
- )
- opts.headers = {}
- Object.assign(opts.headers, this.opts.headers)
- if (this.core.state.xhrUpload) {
- Object.assign(opts.headers, this.core.state.xhrUpload.headers)
- }
- if (file.xhrUpload) {
- Object.assign(opts.headers, file.xhrUpload.headers)
- }
- return opts
- }
- createFormDataUpload (file, opts) {
- const formPost = new FormData()
- const metaFields = Array.isArray(opts.metaFields)
- ? opts.metaFields
- // Send along all fields by default.
- : Object.keys(file.meta)
- metaFields.forEach((item) => {
- formPost.append(item, file.meta[item])
- })
- formPost.append(opts.fieldName, file.data)
- return formPost
- }
- createBareUpload (file, opts) {
- return file.data
- }
- upload (file, current, total) {
- const opts = this.getOptions(file)
- this.core.log(`uploading ${current} of ${total}`)
- return new Promise((resolve, reject) => {
- const data = opts.formData
- ? this.createFormDataUpload(file, opts)
- : this.createBareUpload(file, opts)
- const xhr = new XMLHttpRequest()
- xhr.upload.addEventListener('progress', (ev) => {
- if (ev.lengthComputable) {
- this.core.emit('core:upload-progress', {
- uploader: this,
- id: file.id,
- bytesUploaded: ev.loaded,
- bytesTotal: ev.total
- })
- }
- })
- xhr.addEventListener('load', (ev) => {
- if (ev.target.status >= 200 && ev.target.status < 300) {
- const resp = opts.getResponseData(xhr)
- const uploadURL = resp[opts.responseUrlFieldName]
- this.core.emit('core:upload-success', file.id, resp, uploadURL)
- if (uploadURL) {
- this.core.log(`Download ${file.name} from ${file.uploadURL}`)
- }
- return resolve(file)
- } else {
- const error = opts.getResponseError(xhr) || new Error('Upload error')
- error.request = xhr
- this.core.emit('core:upload-error', file.id, error)
- return reject(error)
- }
- })
- xhr.addEventListener('error', (ev) => {
- const error = opts.getResponseError(xhr) || new Error('Upload error')
- this.core.emit('core:upload-error', file.id, error)
- return reject(new Error('Upload error'))
- })
- xhr.open(opts.method.toUpperCase(), opts.endpoint, true)
- Object.keys(opts.headers).forEach((header) => {
- xhr.setRequestHeader(header, opts.headers[header])
- })
- xhr.send(data)
- this.core.on('core:upload-cancel', (fileID) => {
- if (fileID === file.id) {
- xhr.abort()
- }
- })
- this.core.on('core:cancel-all', () => {
- // const files = this.core.getState().files
- // if (!files[file.id]) return
- xhr.abort()
- })
- this.core.emit('core:upload-started', file.id)
- })
- }
- uploadRemote (file, current, total) {
- const opts = this.getOptions(file)
- return new Promise((resolve, reject) => {
- this.core.emit('core:upload-started', file.id)
- const fields = {}
- const metaFields = Array.isArray(opts.metaFields)
- ? opts.metaFields
- // Send along all fields by default.
- : Object.keys(file.meta)
- metaFields.forEach((name) => {
- fields[name] = file.meta.name
- })
- fetch(file.remote.url, {
- method: 'post',
- credentials: 'include',
- headers: {
- 'Accept': 'application/json',
- 'Content-Type': 'application/json'
- },
- body: JSON.stringify(Object.assign({}, file.remote.body, {
- endpoint: opts.endpoint,
- size: file.data.size,
- fieldname: opts.fieldName,
- fields,
- headers: opts.headers
- }))
- })
- .then((res) => {
- if (res.status < 200 && res.status > 300) {
- return reject(res.statusText)
- }
- res.json().then((data) => {
- const token = data.token
- const host = getSocketHost(file.remote.host)
- const socket = new UppySocket({ target: `${host}/api/${token}` })
- socket.on('progress', (progressData) => emitSocketProgress(this, progressData, file))
- socket.on('success', (data) => {
- this.core.emit('core:upload-success', file.id, data, data.url)
- socket.close()
- return resolve()
- })
- })
- })
- })
- }
- uploadFiles (files) {
- const promises = files.map((file, i) => {
- const current = parseInt(i, 10) + 1
- const total = files.length
- if (file.isRemote) {
- return this.uploadRemote(file, current, total)
- } else {
- return this.upload(file, current, total)
- }
- })
- return settle(promises)
- }
- handleUpload (fileIDs) {
- if (fileIDs.length === 0) {
- this.core.log('[XHRUpload] No files to upload!')
- return Promise.resolve()
- }
- this.core.log('[XHRUpload] Uploading...')
- const files = fileIDs.map(getFile, this)
- function getFile (fileID) {
- return this.core.state.files[fileID]
- }
- return this.uploadFiles(files).then(() => null)
- }
- install () {
- this.core.addUploader(this.handleUpload)
- }
- uninstall () {
- this.core.removeUploader(this.handleUpload)
- }
- }
|