|
| 1 | +import { BasePlugin } from '@uppy/core' |
| 2 | +import { RateLimitedQueue } from '@uppy/utils/lib/RateLimitedQueue' |
| 3 | +import prettierBytes from '@transloadit/prettier-bytes' |
| 4 | +import CompressorJS from 'compressorjs/dist/compressor.common.js' |
| 5 | +import locale from './locale.js' |
| 6 | + |
| 7 | +export default class Compressor extends BasePlugin { |
| 8 | + #RateLimitedQueue |
| 9 | + |
| 10 | + constructor (uppy, opts) { |
| 11 | + super(uppy, opts) |
| 12 | + this.id = this.opts.id || 'Compressor' |
| 13 | + this.type = 'modifier' |
| 14 | + |
| 15 | + this.defaultLocale = locale |
| 16 | + |
| 17 | + const defaultOptions = { |
| 18 | + quality: 0.6, |
| 19 | + limit: 10, |
| 20 | + } |
| 21 | + |
| 22 | + this.opts = { ...defaultOptions, ...opts } |
| 23 | + |
| 24 | + this.#RateLimitedQueue = new RateLimitedQueue(this.opts.limit) |
| 25 | + |
| 26 | + this.i18nInit() |
| 27 | + |
| 28 | + this.prepareUpload = this.prepareUpload.bind(this) |
| 29 | + this.compress = this.compress.bind(this) |
| 30 | + } |
| 31 | + |
| 32 | + compress (blob) { |
| 33 | + return new Promise((resolve, reject) => { |
| 34 | + /* eslint-disable no-new */ |
| 35 | + new CompressorJS(blob, { |
| 36 | + ...this.opts, |
| 37 | + success: resolve, |
| 38 | + error: reject, |
| 39 | + }) |
| 40 | + }) |
| 41 | + } |
| 42 | + |
| 43 | + async prepareUpload (fileIDs) { |
| 44 | + let totalCompressedSize = 0 |
| 45 | + const compressAndApplyResult = this.#RateLimitedQueue.wrapPromiseFunction( |
| 46 | + async (file) => { |
| 47 | + try { |
| 48 | + const compressedBlob = await this.compress(file.data) |
| 49 | + this.uppy.log(`[Image Compressor] Image ${file.id} size before/after compression: ${file.data.size} / ${compressedBlob.size}`) |
| 50 | + totalCompressedSize += compressedBlob.size |
| 51 | + this.uppy.setFileState(file.id, { |
| 52 | + data: compressedBlob, |
| 53 | + size: compressedBlob.size, |
| 54 | + }) |
| 55 | + } catch (err) { |
| 56 | + this.uppy.log(`[Image Compressor] Failed to compress ${file.id}:`, 'warning') |
| 57 | + this.uppy.log(err, 'warning') |
| 58 | + } |
| 59 | + }, |
| 60 | + ) |
| 61 | + |
| 62 | + const promises = fileIDs.map((fileID) => { |
| 63 | + const file = this.uppy.getFile(fileID) |
| 64 | + this.uppy.emit('preprocess-progress', file, { |
| 65 | + mode: 'indeterminate', |
| 66 | + message: this.i18n('compressingImages'), |
| 67 | + }) |
| 68 | + |
| 69 | + // Some browsers (Firefox) add blobs with empty file type, when files are |
| 70 | + // added from a folder. Uppy auto-detects type from extension, but leaves the original blob intact. |
| 71 | + // However, Compressor.js failes when file has no type, so we set it here |
| 72 | + if (!file.data.type) { |
| 73 | + file.data = file.data.slice(0, file.data.size, file.type) |
| 74 | + } |
| 75 | + |
| 76 | + if (!file.type.startsWith('image/')) { |
| 77 | + return Promise.resolve() |
| 78 | + } |
| 79 | + |
| 80 | + return compressAndApplyResult(file) |
| 81 | + }) |
| 82 | + |
| 83 | + // Why emit `preprocess-complete` for all files at once, instead of |
| 84 | + // above when each is processed? |
| 85 | + // Because it leads to StatusBar showing a weird “upload 6 files” button, |
| 86 | + // while waiting for all the files to complete pre-processing. |
| 87 | + await Promise.all(promises) |
| 88 | + |
| 89 | + this.uppy.emit('compressor:complete') |
| 90 | + |
| 91 | + // Only show informer if Compressor mananged to save at least a kilobyte |
| 92 | + if (totalCompressedSize > 1024) { |
| 93 | + this.uppy.info( |
| 94 | + this.i18n('compressedX', { |
| 95 | + size: prettierBytes(totalCompressedSize), |
| 96 | + }), |
| 97 | + 'info', |
| 98 | + ) |
| 99 | + } |
| 100 | + |
| 101 | + for (const fileID of fileIDs) { |
| 102 | + const file = this.uppy.getFile(fileID) |
| 103 | + this.uppy.emit('preprocess-complete', file) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + install () { |
| 108 | + this.uppy.addPreProcessor(this.prepareUpload) |
| 109 | + } |
| 110 | + |
| 111 | + uninstall () { |
| 112 | + this.uppy.removePreProcessor(this.prepareUpload) |
| 113 | + } |
| 114 | +} |
0 commit comments