12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181 |
- const Utils = require('../core/Utils')
- const Translator = require('../core/Translator')
- const ee = require('namespace-emitter')
- const cuid = require('cuid')
- const throttle = require('lodash.throttle')
- const prettyBytes = require('prettier-bytes')
- const match = require('mime-match')
- const DefaultStore = require('../store/DefaultStore')
- /**
- * Uppy Core module.
- * Manages plugins, state updates, acts as an event bus,
- * adds/removes files and metadata.
- */
- class Uppy {
- /**
- * Instantiate Uppy
- * @param {object} opts — Uppy options
- */
- constructor (opts) {
- const defaultLocale = {
- strings: {
- youCanOnlyUploadX: {
- 0: 'You can only upload %{smart_count} file',
- 1: 'You can only upload %{smart_count} files'
- },
- youHaveToAtLeastSelectX: {
- 0: 'You have to select at least %{smart_count} file',
- 1: 'You have to select at least %{smart_count} files'
- },
- exceedsSize: 'This file exceeds maximum allowed size of',
- youCanOnlyUploadFileTypes: 'You can only upload:',
- uppyServerError: 'Connection with Uppy Server failed',
- failedToUpload: 'Failed to upload',
- noInternetConnection: 'No Internet connection',
- connectedToInternet: 'Connected to the Internet'
- }
- }
- // set default options
- const defaultOptions = {
- id: 'uppy',
- autoProceed: true,
- debug: false,
- restrictions: {
- maxFileSize: false,
- maxNumberOfFiles: false,
- minNumberOfFiles: false,
- allowedFileTypes: false
- },
- meta: {},
- onBeforeFileAdded: (currentFile, files) => currentFile,
- onBeforeUpload: (files) => files,
- locale: defaultLocale,
- store: DefaultStore()
- }
- // Merge default options with the ones set by user
- this.opts = Object.assign({}, defaultOptions, opts)
- this.opts.restrictions = Object.assign({}, defaultOptions.restrictions, this.opts.restrictions)
- this.locale = Object.assign({}, defaultLocale, this.opts.locale)
- this.locale.strings = Object.assign({}, defaultLocale.strings, this.opts.locale.strings)
- // i18n
- this.translator = new Translator({locale: this.locale})
- this.i18n = this.translator.translate.bind(this.translator)
- // Container for different types of plugins
- this.plugins = {}
- this.getState = this.getState.bind(this)
- this.getPlugin = this.getPlugin.bind(this)
- this.setFileMeta = this.setFileMeta.bind(this)
- this.setFileState = this.setFileState.bind(this)
- this.log = this.log.bind(this)
- this.info = this.info.bind(this)
- this.hideInfo = this.hideInfo.bind(this)
- this.addFile = this.addFile.bind(this)
- this.removeFile = this.removeFile.bind(this)
- this.pauseResume = this.pauseResume.bind(this)
- this._calculateProgress = this._calculateProgress.bind(this)
- this.updateOnlineStatus = this.updateOnlineStatus.bind(this)
- this.resetProgress = this.resetProgress.bind(this)
- this.pauseAll = this.pauseAll.bind(this)
- this.resumeAll = this.resumeAll.bind(this)
- this.retryAll = this.retryAll.bind(this)
- this.cancelAll = this.cancelAll.bind(this)
- this.retryUpload = this.retryUpload.bind(this)
- this.upload = this.upload.bind(this)
- this.emitter = ee()
- this.on = this.on.bind(this)
- this.off = this.off.bind(this)
- this.once = this.emitter.once.bind(this.emitter)
- this.emit = this.emitter.emit.bind(this.emitter)
- this.preProcessors = []
- this.uploaders = []
- this.postProcessors = []
- this.store = this.opts.store
- this.setState({
- plugins: {},
- files: {},
- currentUploads: {},
- capabilities: {
- resumableUploads: false
- },
- totalProgress: 0,
- meta: Object.assign({}, this.opts.meta),
- info: {
- isHidden: true,
- type: 'info',
- message: ''
- }
- })
- this._storeUnsubscribe = this.store.subscribe((prevState, nextState, patch) => {
- this.emit('state-update', prevState, nextState, patch)
- this.updateAll(nextState)
- })
- // for debugging and testing
- // this.updateNum = 0
- if (this.opts.debug && typeof window !== 'undefined') {
- window['uppyLog'] = ''
- window[this.opts.id] = this
- }
- this._addListeners()
- }
- on (event, callback) {
- this.emitter.on(event, callback)
- return this
- }
- off (event, callback) {
- this.emitter.off(event, callback)
- return this
- }
- /**
- * Iterate on all plugins and run `update` on them.
- * Called each time state changes.
- *
- */
- updateAll (state) {
- this.iteratePlugins(plugin => {
- plugin.update(state)
- })
- }
- /**
- * Updates state with a patch
- *
- * @param {object} patch {foo: 'bar'}
- */
- setState (patch) {
- this.store.setState(patch)
- }
- /**
- * Returns current state.
- * @return {object}
- */
- getState () {
- return this.store.getState()
- }
- /**
- * Back compat for when this.state is used instead of this.getState().
- */
- get state () {
- return this.getState()
- }
- /**
- * Shorthand to set state for a specific file.
- */
- setFileState (fileID, state) {
- this.setState({
- files: Object.assign({}, this.getState().files, {
- [fileID]: Object.assign({}, this.getState().files[fileID], state)
- })
- })
- }
- resetProgress () {
- const defaultProgress = {
- percentage: 0,
- bytesUploaded: 0,
- uploadComplete: false,
- uploadStarted: false
- }
- const files = Object.assign({}, this.getState().files)
- const updatedFiles = {}
- Object.keys(files).forEach(fileID => {
- const updatedFile = Object.assign({}, files[fileID])
- updatedFile.progress = Object.assign({}, updatedFile.progress, defaultProgress)
- updatedFiles[fileID] = updatedFile
- })
- this.setState({
- files: updatedFiles,
- totalProgress: 0
- })
- // TODO Document on the website
- this.emit('reset-progress')
- }
- addPreProcessor (fn) {
- this.preProcessors.push(fn)
- }
- removePreProcessor (fn) {
- const i = this.preProcessors.indexOf(fn)
- if (i !== -1) {
- this.preProcessors.splice(i, 1)
- }
- }
- addPostProcessor (fn) {
- this.postProcessors.push(fn)
- }
- removePostProcessor (fn) {
- const i = this.postProcessors.indexOf(fn)
- if (i !== -1) {
- this.postProcessors.splice(i, 1)
- }
- }
- addUploader (fn) {
- this.uploaders.push(fn)
- }
- removeUploader (fn) {
- const i = this.uploaders.indexOf(fn)
- if (i !== -1) {
- this.uploaders.splice(i, 1)
- }
- }
- setMeta (data) {
- const updatedMeta = Object.assign({}, this.getState().meta, data)
- const updatedFiles = Object.assign({}, this.getState().files)
- Object.keys(updatedFiles).forEach((fileID) => {
- updatedFiles[fileID] = Object.assign({}, updatedFiles[fileID], {
- meta: Object.assign({}, updatedFiles[fileID].meta, data)
- })
- })
- this.log('Adding metadata:')
- this.log(data)
- this.setState({
- meta: updatedMeta,
- files: updatedFiles
- })
- }
- setFileMeta (fileID, data) {
- const updatedFiles = Object.assign({}, this.getState().files)
- if (!updatedFiles[fileID]) {
- this.log('Was trying to set metadata for a file that’s not with us anymore: ', fileID)
- return
- }
- const newMeta = Object.assign({}, updatedFiles[fileID].meta, data)
- updatedFiles[fileID] = Object.assign({}, updatedFiles[fileID], {
- meta: newMeta
- })
- this.setState({files: updatedFiles})
- }
- /**
- * Get a file object.
- *
- * @param {string} fileID The ID of the file object to return.
- */
- getFile (fileID) {
- return this.getState().files[fileID]
- }
- /**
- * Get all files in an array.
- */
- getFiles () {
- const { files } = this.getState()
- return Object.keys(files).map((fileID) => files[fileID])
- }
- /**
- * Check if minNumberOfFiles restriction is reached before uploading.
- *
- * @private
- */
- _checkMinNumberOfFiles (files) {
- const {minNumberOfFiles} = this.opts.restrictions
- if (Object.keys(files).length < minNumberOfFiles) {
- throw new Error(`${this.i18n('youHaveToAtLeastSelectX', { smart_count: minNumberOfFiles })}`)
- }
- }
- /**
- * Check if file passes a set of restrictions set in options: maxFileSize,
- * maxNumberOfFiles and allowedFileTypes.
- *
- * @param {object} file object to check
- * @private
- */
- _checkRestrictions (file) {
- const {maxFileSize, maxNumberOfFiles, allowedFileTypes} = this.opts.restrictions
- if (maxNumberOfFiles) {
- if (Object.keys(this.getState().files).length + 1 > maxNumberOfFiles) {
- throw new Error(`${this.i18n('youCanOnlyUploadX', { smart_count: maxNumberOfFiles })}`)
- }
- }
- if (allowedFileTypes) {
- const isCorrectFileType = allowedFileTypes.filter((type) => {
- if (!file.type) return false
- return match(file.type, type)
- }).length > 0
- if (!isCorrectFileType) {
- const allowedFileTypesString = allowedFileTypes.join(', ')
- throw new Error(`${this.i18n('youCanOnlyUploadFileTypes')} ${allowedFileTypesString}`)
- }
- }
- if (maxFileSize) {
- if (file.data.size > maxFileSize) {
- throw new Error(`${this.i18n('exceedsSize')} ${prettyBytes(maxFileSize)}`)
- }
- }
- }
- /**
- * Add a new file to `state.files`. This will run `onBeforeFileAdded`,
- * try to guess file type in a clever way, check file against restrictions,
- * and start an upload if `autoProceed === true`.
- *
- * @param {object} file object to add
- */
- addFile (file) {
- const { files } = this.getState()
- const onError = (msg) => {
- const err = typeof msg === 'object' ? msg : new Error(msg)
- this.log(err.message)
- this.info(err.message, 'error', 5000)
- throw err
- }
- const onBeforeFileAddedResult = this.opts.onBeforeFileAdded(file, files)
- if (onBeforeFileAddedResult === false) {
- this.log('Not adding file because onBeforeFileAdded returned false')
- return
- }
- if (typeof onBeforeFileAddedResult === 'object' && onBeforeFileAddedResult) {
- // warning after the change in 0.24
- if (onBeforeFileAddedResult.then) {
- throw new TypeError('onBeforeFileAdded() returned a Promise, but this is no longer supported. It must be synchronous.')
- }
- file = onBeforeFileAddedResult
- }
- const fileType = Utils.getFileType(file)
- let fileName
- if (file.name) {
- fileName = file.name
- } else if (fileType.split('/')[0] === 'image') {
- fileName = fileType.split('/')[0] + '.' + fileType.split('/')[1]
- } else {
- fileName = 'noname'
- }
- const fileExtension = Utils.getFileNameAndExtension(fileName).extension
- const isRemote = file.isRemote || false
- const fileID = Utils.generateFileID(file)
- const newFile = {
- source: file.source || '',
- id: fileID,
- name: fileName,
- extension: fileExtension || '',
- meta: Object.assign({}, this.getState().meta, {
- name: fileName,
- type: fileType
- }),
- type: fileType,
- data: file.data,
- progress: {
- percentage: 0,
- bytesUploaded: 0,
- bytesTotal: file.data.size || 0,
- uploadComplete: false,
- uploadStarted: false
- },
- size: file.data.size || 0,
- isRemote: isRemote,
- remote: file.remote || '',
- preview: file.preview
- }
- try {
- this._checkRestrictions(newFile)
- } catch (err) {
- onError(err)
- }
- this.setState({
- files: Object.assign({}, files, {
- [fileID]: newFile
- })
- })
- this.emit('file-added', newFile)
- this.log(`Added file: ${fileName}, ${fileID}, mime type: ${fileType}`)
- if (this.opts.autoProceed && !this.scheduledAutoProceed) {
- this.scheduledAutoProceed = setTimeout(() => {
- this.scheduledAutoProceed = null
- this.upload().catch((err) => {
- console.error(err.stack || err.message || err)
- })
- }, 4)
- }
- }
- removeFile (fileID) {
- const { files, currentUploads } = this.state
- const updatedFiles = Object.assign({}, files)
- const removedFile = updatedFiles[fileID]
- delete updatedFiles[fileID]
- // Remove this file from its `currentUpload`.
- const updatedUploads = Object.assign({}, currentUploads)
- const removeUploads = []
- Object.keys(updatedUploads).forEach((uploadID) => {
- const newFileIDs = currentUploads[uploadID].fileIDs.filter((uploadFileID) => uploadFileID !== fileID)
- // Remove the upload if no files are associated with it anymore.
- if (newFileIDs.length === 0) {
- removeUploads.push(uploadID)
- return
- }
- updatedUploads[uploadID] = Object.assign({}, currentUploads[uploadID], {
- fileIDs: newFileIDs
- })
- })
- this.setState({
- currentUploads: updatedUploads,
- files: updatedFiles
- })
- removeUploads.forEach((uploadID) => {
- this._removeUpload(uploadID)
- })
- this._calculateTotalProgress()
- this.emit('file-removed', removedFile)
- this.log(`File removed: ${removedFile.id}`)
- // Clean up object URLs.
- if (removedFile.preview && Utils.isObjectURL(removedFile.preview)) {
- URL.revokeObjectURL(removedFile.preview)
- }
- this.log(`Removed file: ${fileID}`)
- }
- pauseResume (fileID) {
- if (this.getFile(fileID).uploadComplete) return
- const wasPaused = this.getFile(fileID).isPaused || false
- const isPaused = !wasPaused
- this.setFileState(fileID, {
- isPaused: isPaused
- })
- this.emit('upload-pause', fileID, isPaused)
- return isPaused
- }
- pauseAll () {
- const updatedFiles = Object.assign({}, this.getState().files)
- const inProgressUpdatedFiles = Object.keys(updatedFiles).filter((file) => {
- return !updatedFiles[file].progress.uploadComplete &&
- updatedFiles[file].progress.uploadStarted
- })
- inProgressUpdatedFiles.forEach((file) => {
- const updatedFile = Object.assign({}, updatedFiles[file], {
- isPaused: true
- })
- updatedFiles[file] = updatedFile
- })
- this.setState({files: updatedFiles})
- this.emit('pause-all')
- }
- resumeAll () {
- const updatedFiles = Object.assign({}, this.getState().files)
- const inProgressUpdatedFiles = Object.keys(updatedFiles).filter((file) => {
- return !updatedFiles[file].progress.uploadComplete &&
- updatedFiles[file].progress.uploadStarted
- })
- inProgressUpdatedFiles.forEach((file) => {
- const updatedFile = Object.assign({}, updatedFiles[file], {
- isPaused: false,
- error: null
- })
- updatedFiles[file] = updatedFile
- })
- this.setState({files: updatedFiles})
- this.emit('resume-all')
- }
- retryAll () {
- const updatedFiles = Object.assign({}, this.getState().files)
- const filesToRetry = Object.keys(updatedFiles).filter(file => {
- return updatedFiles[file].error
- })
- filesToRetry.forEach((file) => {
- const updatedFile = Object.assign({}, updatedFiles[file], {
- isPaused: false,
- error: null
- })
- updatedFiles[file] = updatedFile
- })
- this.setState({
- files: updatedFiles,
- error: null
- })
- this.emit('retry-all', filesToRetry)
- const uploadID = this._createUpload(filesToRetry)
- return this._runUpload(uploadID)
- }
- cancelAll () {
- this.emit('cancel-all')
- // TODO Or should we just call removeFile on all files?
- const { currentUploads } = this.getState()
- const uploadIDs = Object.keys(currentUploads)
- uploadIDs.forEach((id) => {
- this._removeUpload(id)
- })
- this.setState({
- files: {},
- totalProgress: 0
- })
- }
- retryUpload (fileID) {
- const updatedFiles = Object.assign({}, this.getState().files)
- const updatedFile = Object.assign({}, updatedFiles[fileID],
- { error: null, isPaused: false }
- )
- updatedFiles[fileID] = updatedFile
- this.setState({
- files: updatedFiles
- })
- this.emit('upload-retry', fileID)
- const uploadID = this._createUpload([ fileID ])
- return this._runUpload(uploadID)
- }
- reset () {
- this.cancelAll()
- }
- _calculateProgress (file, data) {
- if (!this.getFile(file.id)) {
- this.log(`Not setting progress for a file that has been removed: ${file.id}`)
- return
- }
- this.setFileState(file.id, {
- progress: Object.assign({}, this.getFile(file.id).progress, {
- bytesUploaded: data.bytesUploaded,
- bytesTotal: data.bytesTotal,
- percentage: Math.floor((data.bytesUploaded / data.bytesTotal * 100).toFixed(2))
- })
- })
- this._calculateTotalProgress()
- }
- _calculateTotalProgress () {
- // calculate total progress, using the number of files currently uploading,
- // multiplied by 100 and the summ of individual progress of each file
- const files = Object.assign({}, this.getState().files)
- const inProgress = Object.keys(files).filter((file) => {
- return files[file].progress.uploadStarted
- })
- const progressMax = inProgress.length * 100
- let progressAll = 0
- inProgress.forEach((file) => {
- progressAll = progressAll + files[file].progress.percentage
- })
- const totalProgress = progressMax === 0 ? 0 : Math.floor((progressAll * 100 / progressMax).toFixed(2))
- this.setState({
- totalProgress: totalProgress
- })
- }
- /**
- * Registers listeners for all global actions, like:
- * `error`, `file-removed`, `upload-progress`
- */
- _addListeners () {
- this.on('error', (error) => {
- this.setState({ error: error.message })
- })
- this.on('upload-error', (file, error) => {
- this.setFileState(file.id, { error: error.message })
- this.setState({ error: error.message })
- let message
- message = `${this.i18n('failedToUpload')} ${file.name}`
- if (typeof error === 'object' && error.message) {
- message = { message: message, details: error.message }
- }
- this.info(message, 'error', 5000)
- })
- this.on('upload', () => {
- this.setState({ error: null })
- })
- this.on('upload-started', (file, upload) => {
- if (!this.getFile(file.id)) {
- this.log(`Not setting progress for a file that has been removed: ${file.id}`)
- return
- }
- this.setFileState(file.id, {
- progress: {
- uploadStarted: Date.now(),
- uploadComplete: false,
- percentage: 0,
- bytesUploaded: 0,
- bytesTotal: file.size
- }
- })
- })
- // upload progress events can occur frequently, especially when you have a good
- // connection to the remote server. Therefore, we are throtteling them to
- // prevent accessive function calls.
- // see also: https://github.com/tus/tus-js-client/commit/9940f27b2361fd7e10ba58b09b60d82422183bbb
- const _throttledCalculateProgress = throttle(this._calculateProgress, 100, { leading: true, trailing: true })
- this.on('upload-progress', _throttledCalculateProgress)
- this.on('upload-success', (file, uploadResp, uploadURL) => {
- this.setFileState(file.id, {
- progress: Object.assign({}, this.getFile(file.id).progress, {
- uploadComplete: true,
- percentage: 100
- }),
- uploadURL: uploadURL,
- isPaused: false
- })
- this._calculateTotalProgress()
- })
- this.on('preprocess-progress', (file, progress) => {
- if (!this.getFile(file.id)) {
- this.log(`Not setting progress for a file that has been removed: ${file.id}`)
- return
- }
- this.setFileState(file.id, {
- progress: Object.assign({}, this.getFile(file.id).progress, {
- preprocess: progress
- })
- })
- })
- this.on('preprocess-complete', (file) => {
- if (!this.getFile(file.id)) {
- this.log(`Not setting progress for a file that has been removed: ${file.id}`)
- return
- }
- const files = Object.assign({}, this.getState().files)
- files[file.id] = Object.assign({}, files[file.id], {
- progress: Object.assign({}, files[file.id].progress)
- })
- delete files[file.id].progress.preprocess
- this.setState({ files: files })
- })
- this.on('postprocess-progress', (file, progress) => {
- if (!this.getFile(file.id)) {
- this.log(`Not setting progress for a file that has been removed: ${file.id}`)
- return
- }
- this.setFileState(file.id, {
- progress: Object.assign({}, this.getState().files[file.id].progress, {
- postprocess: progress
- })
- })
- })
- this.on('postprocess-complete', (file) => {
- if (!this.getFile(file.id)) {
- this.log(`Not setting progress for a file that has been removed: ${file.id}`)
- return
- }
- const files = Object.assign({}, this.getState().files)
- files[file.id] = Object.assign({}, files[file.id], {
- progress: Object.assign({}, files[file.id].progress)
- })
- delete files[file.id].progress.postprocess
- // TODO should we set some kind of `fullyComplete` property on the file object
- // so it's easier to see that the file is upload…fully complete…rather than
- // what we have to do now (`uploadComplete && !postprocess`)
- this.setState({ files: files })
- })
- this.on('restored', () => {
- // Files may have changed--ensure progress is still accurate.
- this._calculateTotalProgress()
- })
- // show informer if offline
- if (typeof window !== 'undefined') {
- window.addEventListener('online', () => this.updateOnlineStatus())
- window.addEventListener('offline', () => this.updateOnlineStatus())
- setTimeout(() => this.updateOnlineStatus(), 3000)
- }
- }
- updateOnlineStatus () {
- const online =
- typeof window.navigator.onLine !== 'undefined'
- ? window.navigator.onLine
- : true
- if (!online) {
- this.emit('is-offline')
- this.info(this.i18n('noInternetConnection'), 'error', 0)
- this.wasOffline = true
- } else {
- this.emit('is-online')
- if (this.wasOffline) {
- this.emit('back-online')
- this.info(this.i18n('connectedToInternet'), 'success', 3000)
- this.wasOffline = false
- }
- }
- }
- getID () {
- return this.opts.id
- }
- /**
- * Registers a plugin with Core.
- *
- * @param {object} Plugin object
- * @param {object} [opts] object with options to be passed to Plugin
- * @return {Object} self for chaining
- */
- use (Plugin, opts) {
- if (typeof Plugin !== 'function') {
- let msg = `Expected a plugin class, but got ${Plugin === null ? 'null' : typeof Plugin}.` +
- ' Please verify that the plugin was imported and spelled correctly.'
- throw new TypeError(msg)
- }
- // Instantiate
- const plugin = new Plugin(this, opts)
- const pluginId = plugin.id
- this.plugins[plugin.type] = this.plugins[plugin.type] || []
- if (!pluginId) {
- throw new Error('Your plugin must have an id')
- }
- if (!plugin.type) {
- throw new Error('Your plugin must have a type')
- }
- let existsPluginAlready = this.getPlugin(pluginId)
- if (existsPluginAlready) {
- let msg = `Already found a plugin named '${existsPluginAlready.id}'. ` +
- `Tried to use: '${pluginId}'.\n` +
- `Uppy plugins must have unique 'id' options. See https://uppy.io/docs/plugins/#id.`
- throw new Error(msg)
- }
- this.plugins[plugin.type].push(plugin)
- plugin.install()
- return this
- }
- /**
- * Find one Plugin by name.
- *
- * @param {string} name description
- * @return {object | boolean}
- */
- getPlugin (name) {
- let foundPlugin = null
- this.iteratePlugins((plugin) => {
- const pluginName = plugin.id
- if (pluginName === name) {
- foundPlugin = plugin
- return false
- }
- })
- return foundPlugin
- }
- /**
- * Iterate through all `use`d plugins.
- *
- * @param {function} method that will be run on each plugin
- */
- iteratePlugins (method) {
- Object.keys(this.plugins).forEach(pluginType => {
- this.plugins[pluginType].forEach(method)
- })
- }
- /**
- * Uninstall and remove a plugin.
- *
- * @param {object} instance The plugin instance to remove.
- */
- removePlugin (instance) {
- const list = this.plugins[instance.type]
- if (instance.uninstall) {
- instance.uninstall()
- }
- const index = list.indexOf(instance)
- if (index !== -1) {
- list.splice(index, 1)
- }
- }
- /**
- * Uninstall all plugins and close down this Uppy instance.
- */
- close () {
- this.reset()
- this._storeUnsubscribe()
- this.iteratePlugins((plugin) => {
- plugin.uninstall()
- })
- }
- /**
- * Set info message in `state.info`, so that UI plugins like `Informer`
- * can display the message.
- *
- * @param {string | object} message Message to be displayed by the informer
- * @param {string} [type]
- * @param {number} [duration]
- */
- info (message, type = 'info', duration = 3000) {
- const isComplexMessage = typeof message === 'object'
- this.setState({
- info: {
- isHidden: false,
- type: type,
- message: isComplexMessage ? message.message : message,
- details: isComplexMessage ? message.details : null
- }
- })
- this.emit('info-visible')
- clearTimeout(this.infoTimeoutID)
- if (duration === 0) {
- this.infoTimeoutID = undefined
- return
- }
- // hide the informer after `duration` milliseconds
- this.infoTimeoutID = setTimeout(this.hideInfo, duration)
- }
- hideInfo () {
- const newInfo = Object.assign({}, this.getState().info, {
- isHidden: true
- })
- this.setState({
- info: newInfo
- })
- this.emit('info-hidden')
- }
- /**
- * Logs stuff to console, only if `debug` is set to true. Silent in production.
- *
- * @param {String|Object} msg to log
- * @param {String} [type] optional `error` or `warning`
- */
- log (msg, type) {
- if (!this.opts.debug) {
- return
- }
- let message = `[Uppy] [${Utils.getTimeStamp()}] ${msg}`
- window['uppyLog'] = window['uppyLog'] + '\n' + 'DEBUG LOG: ' + msg
- if (type === 'error') {
- console.error(message)
- return
- }
- if (type === 'warning') {
- console.warn(message)
- return
- }
- if (msg === `${msg}`) {
- console.log(message)
- } else {
- message = `[Uppy] [${Utils.getTimeStamp()}]`
- console.log(message)
- console.dir(msg)
- }
- }
- /**
- * Obsolete, event listeners are now added in the constructor.
- */
- run () {
- this.log('Calling run() is no longer necessary.', 'warning')
- return this
- }
- /**
- * Restore an upload by its ID.
- */
- restore (uploadID) {
- this.log(`Core: attempting to restore upload "${uploadID}"`)
- if (!this.getState().currentUploads[uploadID]) {
- this._removeUpload(uploadID)
- return Promise.reject(new Error('Nonexistent upload'))
- }
- return this._runUpload(uploadID)
- }
- /**
- * Create an upload for a bunch of files.
- *
- * @param {Array<string>} fileIDs File IDs to include in this upload.
- * @return {string} ID of this upload.
- */
- _createUpload (fileIDs) {
- const uploadID = cuid()
- this.emit('upload', {
- id: uploadID,
- fileIDs: fileIDs
- })
- this.setState({
- currentUploads: Object.assign({}, this.getState().currentUploads, {
- [uploadID]: {
- fileIDs: fileIDs,
- step: 0,
- result: {}
- }
- })
- })
- return uploadID
- }
- _getUpload (uploadID) {
- return this.getState().currentUploads[uploadID]
- }
- /**
- * Add data to an upload's result object.
- *
- * @param {string} uploadID The ID of the upload.
- * @param {object} data Data properties to add to the result object.
- */
- addResultData (uploadID, data) {
- if (!this._getUpload(uploadID)) {
- this.log(`Not setting result for an upload that has been removed: ${uploadID}`)
- return
- }
- const currentUploads = this.getState().currentUploads
- const currentUpload = Object.assign({}, currentUploads[uploadID], {
- result: Object.assign({}, currentUploads[uploadID].result, data)
- })
- this.setState({
- currentUploads: Object.assign({}, currentUploads, {
- [uploadID]: currentUpload
- })
- })
- }
- /**
- * Remove an upload, eg. if it has been canceled or completed.
- *
- * @param {string} uploadID The ID of the upload.
- */
- _removeUpload (uploadID) {
- const currentUploads = Object.assign({}, this.getState().currentUploads)
- delete currentUploads[uploadID]
- this.setState({
- currentUploads: currentUploads
- })
- }
- /**
- * Run an upload. This picks up where it left off in case the upload is being restored.
- *
- * @private
- */
- _runUpload (uploadID) {
- const uploadData = this.getState().currentUploads[uploadID]
- const fileIDs = uploadData.fileIDs
- const restoreStep = uploadData.step
- const steps = [
- ...this.preProcessors,
- ...this.uploaders,
- ...this.postProcessors
- ]
- let lastStep = Promise.resolve()
- steps.forEach((fn, step) => {
- // Skip this step if we are restoring and have already completed this step before.
- if (step < restoreStep) {
- return
- }
- lastStep = lastStep.then(() => {
- const { currentUploads } = this.getState()
- const currentUpload = Object.assign({}, currentUploads[uploadID], {
- step: step
- })
- this.setState({
- currentUploads: Object.assign({}, currentUploads, {
- [uploadID]: currentUpload
- })
- })
- // TODO give this the `currentUpload` object as its only parameter maybe?
- // Otherwise when more metadata may be added to the upload this would keep getting more parameters
- return fn(fileIDs, uploadID)
- }).then((result) => {
- return null
- })
- })
- // Not returning the `catch`ed promise, because we still want to return a rejected
- // promise from this method if the upload failed.
- lastStep.catch((err) => {
- this.emit('error', err)
- this._removeUpload(uploadID)
- })
- return lastStep.then(() => {
- const files = fileIDs.map((fileID) => this.getFile(fileID))
- const successful = files.filter((file) => file && !file.error)
- const failed = files.filter((file) => file && file.error)
- this.addResultData(uploadID, { successful, failed, uploadID })
- const { currentUploads } = this.getState()
- if (!currentUploads[uploadID]) {
- this.log(`Not setting result for an upload that has been removed: ${uploadID}`)
- return
- }
- const result = currentUploads[uploadID].result
- this.emit('complete', result)
- this._removeUpload(uploadID)
- return result
- })
- }
- /**
- * Start an upload for all the files that are not currently being uploaded.
- *
- * @return {Promise}
- */
- upload () {
- if (!this.plugins.uploader) {
- this.log('No uploader type plugins are used', 'warning')
- }
- let files = this.getState().files
- const onBeforeUploadResult = this.opts.onBeforeUpload(files)
- if (onBeforeUploadResult === false) {
- return Promise.reject(new Error('Not starting the upload because onBeforeUpload returned false'))
- }
- if (onBeforeUploadResult && typeof onBeforeUploadResult === 'object') {
- // warning after the change in 0.24
- if (onBeforeUploadResult.then) {
- throw new TypeError('onBeforeUpload() returned a Promise, but this is no longer supported. It must be synchronous.')
- }
- files = onBeforeUploadResult
- }
- return Promise.resolve()
- .then(() => this._checkMinNumberOfFiles(files))
- .then(() => {
- const { currentUploads } = this.getState()
- // get a list of files that are currently assigned to uploads
- const currentlyUploadingFiles = Object.keys(currentUploads).reduce((prev, curr) => prev.concat(currentUploads[curr].fileIDs), [])
- const waitingFileIDs = []
- Object.keys(files).forEach((fileID) => {
- const file = this.getFile(fileID)
- // if the file hasn't started uploading and hasn't already been assigned to an upload..
- if ((!file.progress.uploadStarted) && (currentlyUploadingFiles.indexOf(fileID) === -1)) {
- waitingFileIDs.push(file.id)
- }
- })
- const uploadID = this._createUpload(waitingFileIDs)
- return this._runUpload(uploadID)
- })
- .catch((err) => {
- const message = typeof err === 'object' ? err.message : err
- this.log(message)
- this.info(message, 'error', 4000)
- return Promise.reject(typeof err === 'object' ? err : new Error(err))
- })
- }
- }
- module.exports = function (opts) {
- return new Uppy(opts)
- }
- // Expose class constructor.
- module.exports.Uppy = Uppy
|