123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- const Plugin = require('./Plugin')
- const UppySocket = require('../core/UppySocket')
- const Utils = require('../core/Utils')
- module.exports = class Multipart extends Plugin {
- constructor (core, opts) {
- super(core, opts)
- this.type = 'uploader'
- this.id = 'Multipart'
- this.title = 'Multipart'
- // Default options
- const defaultOptions = {
- fieldName: 'files[]',
- metaFields: null,
- responseUrlFieldName: 'url',
- bundle: true,
- headers: {},
- getUploadUrl (xhr) {
- const resp = JSON.parse(xhr.response)
- return resp[this.responseUrlFieldName]
- }
- }
- // Merge default options with the ones set by user
- this.opts = Object.assign({}, defaultOptions, opts)
- this.handleUpload = this.handleUpload.bind(this)
- }
- upload (file, current, total) {
- const opts = Object.assign({}, this.opts, file.multipart || {})
- this.core.log(`uploading ${current} of ${total}`)
- return new Promise((resolve, reject) => {
- // turn file into an array so we can use bundle
- // if (!opts.bundle) {
- // files = [files[current]]
- // }
- // for (let i in files) {
- // formPost.append(opts.fieldName, files[i])
- // }
- 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)
- const xhr = new XMLHttpRequest()
- xhr.upload.addEventListener('progress', (ev) => {
- if (ev.lengthComputable) {
- this.core.emitter.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 uploadURL = opts.getUploadUrl(xhr)
- this.core.emitter.emit('core:upload-success', file.id, uploadURL)
- if (uploadURL) {
- this.core.log(`Download ${file.name} from ${file.uploadURL}`)
- }
- return resolve(file)
- } else {
- this.core.emitter.emit('core:upload-error', file.id, xhr)
- return reject('Upload error')
- }
- // var upload = {}
- //
- // if (opts.bundle) {
- // upload = {files: files}
- // } else {
- // upload = {file: files[current]}
- // }
- })
- xhr.addEventListener('error', (ev) => {
- this.core.emitter.emit('core:upload-error', file.id)
- return reject('Upload error')
- })
- xhr.open('POST', opts.endpoint, true)
- Object.keys(opts.headers).forEach((header) => {
- xhr.setRequestHeader(header, opts.headers[header])
- })
- xhr.send(formPost)
- this.core.emitter.on('core:upload-cancel', (fileID) => {
- if (fileID === file.id) {
- xhr.abort()
- }
- })
- this.core.emitter.on('core:cancel-all', () => {
- // const files = this.core.getState().files
- // if (!files[file.id]) return
- xhr.abort()
- })
- this.core.emitter.emit('core:upload-started', file.id)
- })
- }
- uploadRemote (file, current, total) {
- const opts = Object.assign({}, this.opts, file.multipart || {})
- return new Promise((resolve, reject) => {
- this.core.emitter.emit('core:upload-started', file.id)
- 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
- }))
- })
- .then((res) => {
- if (res.status < 200 && res.status > 300) {
- return reject(res.statusText)
- }
- res.json().then((data) => {
- const token = data.token
- const host = Utils.getSocketHost(file.remote.host)
- const socket = new UppySocket({ target: `${host}/api/${token}` })
- socket.on('progress', (progressData) => Utils.emitSocketProgress(this, progressData, file))
- socket.on('success', (data) => {
- this.core.emitter.emit('core:upload-success', file.id, data.url)
- socket.close()
- return resolve()
- })
- })
- })
- })
- }
- selectForUpload (files) {
- files.forEach((file, i) => {
- const current = parseInt(i, 10) + 1
- const total = files.length
- if (file.isRemote) {
- this.uploadRemote(file, current, total)
- } else {
- this.upload(file, current, total)
- }
- })
- // if (this.opts.bundle) {
- // uploaders.push(this.upload(files, 0, files.length))
- // } else {
- // for (let i in files) {
- // uploaders.push(this.upload(files, i, files.length))
- // }
- // }
- }
- handleUpload (fileIDs) {
- if (fileIDs.length === 0) {
- this.core.log('Multipart: no files to upload!')
- return Promise.resolve()
- }
- this.core.log('Multipart is uploading...')
- const files = fileIDs.map(getFile, this)
- function getFile (fileID) {
- return this.core.state.files[fileID]
- }
- this.selectForUpload(files)
- return new Promise((resolve) => {
- this.core.bus.once('core:upload-complete', resolve)
- })
- }
- install () {
- this.core.addUploader(this.handleUpload)
- }
- uninstall () {
- this.core.removeUploader(this.handleUpload)
- }
- }
|