Socket
Socket
Sign inDemoInstall

@uppy/thumbnail-generator

Package Overview
Dependencies
Maintainers
5
Versions
86
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@uppy/thumbnail-generator - npm Package Compare versions

Comparing version 1.7.11 to 2.0.0-alpha.0

302

lib/index.js

@@ -0,24 +1,18 @@

"use strict";
var _class, _temp;
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
const {
UIPlugin
} = require('@uppy/core');
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
const Translator = require('@uppy/utils/lib/Translator');
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
const dataURItoBlob = require('@uppy/utils/lib/dataURItoBlob');
var _require = require('@uppy/core'),
Plugin = _require.Plugin;
const isObjectURL = require('@uppy/utils/lib/isObjectURL');
var Translator = require('@uppy/utils/lib/Translator');
const isPreviewSupported = require('@uppy/utils/lib/isPreviewSupported');
var dataURItoBlob = require('@uppy/utils/lib/dataURItoBlob');
var isObjectURL = require('@uppy/utils/lib/isObjectURL');
var isPreviewSupported = require('@uppy/utils/lib/isPreviewSupported');
var MathLog2 = require('math-log2'); // Polyfill for IE.
var exifr = require('exifr/dist/mini.legacy.umd.js');
const exifr = require('exifr/dist/mini.legacy.umd.js');
/**

@@ -29,29 +23,25 @@ * The Thumbnail Generator plugin

module.exports = (_temp = _class = /*#__PURE__*/function (_Plugin) {
_inheritsLoose(ThumbnailGenerator, _Plugin);
module.exports = (_temp = _class = class ThumbnailGenerator extends UIPlugin {
constructor(uppy, opts) {
super(uppy, opts);
function ThumbnailGenerator(uppy, opts) {
var _this;
_this = _Plugin.call(this, uppy, opts) || this;
_this.onFileAdded = function (file) {
this.onFileAdded = file => {
if (!file.preview && file.data && isPreviewSupported(file.type) && !file.isRemote) {
_this.addToQueue(file.id);
this.addToQueue(file.id);
}
};
_this.onCancelRequest = function (file) {
var index = _this.queue.indexOf(file.id);
this.onCancelRequest = file => {
const index = this.queue.indexOf(file.id);
if (index !== -1) {
_this.queue.splice(index, 1);
this.queue.splice(index, 1);
}
};
_this.onFileRemoved = function (file) {
var index = _this.queue.indexOf(file.id);
this.onFileRemoved = file => {
const index = this.queue.indexOf(file.id);
if (index !== -1) {
_this.queue.splice(index, 1);
this.queue.splice(index, 1);
} // Clean up object URLs.

@@ -65,11 +55,8 @@

_this.onRestored = function () {
var restoredFiles = _this.uppy.getFiles().filter(function (file) {
return file.isRestored;
});
restoredFiles.forEach(function (file) {
this.onRestored = () => {
const restoredFiles = this.uppy.getFiles().filter(file => file.isRestored);
restoredFiles.forEach(file => {
// Only add blob URLs; they are likely invalid after being restored.
if (!file.preview || isObjectURL(file.preview)) {
_this.addToQueue(file.id);
this.addToQueue(file.id);
}

@@ -79,23 +66,21 @@ });

_this.waitUntilAllProcessed = function (fileIDs) {
fileIDs.forEach(function (fileID) {
var file = _this.uppy.getFile(fileID);
_this.uppy.emit('preprocess-progress', file, {
this.waitUntilAllProcessed = fileIDs => {
fileIDs.forEach(fileID => {
const file = this.uppy.getFile(fileID);
this.uppy.emit('preprocess-progress', file, {
mode: 'indeterminate',
message: _this.i18n('generatingThumbnails')
message: this.i18n('generatingThumbnails')
});
});
var emitPreprocessCompleteForAll = function emitPreprocessCompleteForAll() {
fileIDs.forEach(function (fileID) {
var file = _this.uppy.getFile(fileID);
_this.uppy.emit('preprocess-complete', file);
const emitPreprocessCompleteForAll = () => {
fileIDs.forEach(fileID => {
const file = this.uppy.getFile(fileID);
this.uppy.emit('preprocess-complete', file);
});
};
return new Promise(function (resolve, reject) {
if (_this.queueProcessing) {
_this.uppy.once('thumbnail:all-generated', function () {
return new Promise((resolve, reject) => {
if (this.queueProcessing) {
this.uppy.once('thumbnail:all-generated', () => {
emitPreprocessCompleteForAll();

@@ -111,10 +96,10 @@ resolve();

_this.type = 'modifier';
_this.id = _this.opts.id || 'ThumbnailGenerator';
_this.title = 'Thumbnail Generator';
_this.queue = [];
_this.queueProcessing = false;
_this.defaultThumbnailDimension = 200;
_this.thumbnailType = _this.opts.thumbnailType || 'image/jpeg';
_this.defaultLocale = {
this.type = 'modifier';
this.id = this.opts.id || 'ThumbnailGenerator';
this.title = 'Thumbnail Generator';
this.queue = [];
this.queueProcessing = false;
this.defaultThumbnailDimension = 200;
this.thumbnailType = this.opts.thumbnailType || 'image/jpeg';
this.defaultLocale = {
strings: {

@@ -124,3 +109,3 @@ generatingThumbnails: 'Generating thumbnails...'

};
var defaultOptions = {
const defaultOptions = {
thumbnailWidth: null,

@@ -131,26 +116,10 @@ thumbnailHeight: null,

};
_this.opts = _extends({}, defaultOptions, opts);
this.opts = { ...defaultOptions,
...opts
};
if (_this.opts.lazy && _this.opts.waitForThumbnailsBeforeUpload) {
if (this.opts.lazy && this.opts.waitForThumbnailsBeforeUpload) {
throw new Error('ThumbnailGenerator: The `lazy` and `waitForThumbnailsBeforeUpload` options are mutually exclusive. Please ensure at most one of them is set to `true`.');
}
_this.i18nInit();
return _this;
}
var _proto = ThumbnailGenerator.prototype;
_proto.setOptions = function setOptions(newOpts) {
_Plugin.prototype.setOptions.call(this, newOpts);
this.i18nInit();
};
_proto.i18nInit = function i18nInit() {
this.translator = new Translator([this.defaultLocale, this.uppy.locale, this.opts.locale]);
this.i18n = this.translator.translate.bind(this.translator);
this.setPluginState(); // so that UI re-renders and we see the updated locale
}
/**

@@ -164,22 +133,14 @@ * Create a thumbnail for the given Uppy file object.

*/
;
_proto.createThumbnail = function createThumbnail(file, targetWidth, targetHeight) {
var _this2 = this;
// bug in the compatibility data
// eslint-disable-next-line compat/compat
var originalUrl = URL.createObjectURL(file.data);
var onload = new Promise(function (resolve, reject) {
var image = new Image();
createThumbnail(file, targetWidth, targetHeight) {
const originalUrl = URL.createObjectURL(file.data);
const onload = new Promise((resolve, reject) => {
const image = new Image();
image.src = originalUrl;
image.addEventListener('load', function () {
// bug in the compatibility data
// eslint-disable-next-line compat/compat
image.addEventListener('load', () => {
URL.revokeObjectURL(originalUrl);
resolve(image);
});
image.addEventListener('error', function (event) {
// bug in the compatibility data
// eslint-disable-next-line compat/compat
image.addEventListener('error', event => {
URL.revokeObjectURL(originalUrl);

@@ -189,19 +150,9 @@ reject(event.error || new Error('Could not create thumbnail'));

});
var orientationPromise = exifr.rotation(file.data).catch(function (_err) {
return 1;
});
return Promise.all([onload, orientationPromise]).then(function (_ref) {
var image = _ref[0],
orientation = _ref[1];
var dimensions = _this2.getProportionalDimensions(image, targetWidth, targetHeight, orientation.deg);
var rotatedImage = _this2.rotateImage(image, orientation);
var resizedImage = _this2.resizeImage(rotatedImage, dimensions.width, dimensions.height);
return _this2.canvasToBlob(resizedImage, _this2.thumbnailType, 80);
}).then(function (blob) {
// bug in the compatibility data
// eslint-disable-next-line compat/compat
const orientationPromise = exifr.rotation(file.data).catch(_err => 1);
return Promise.all([onload, orientationPromise]).then(([image, orientation]) => {
const dimensions = this.getProportionalDimensions(image, targetWidth, targetHeight, orientation.deg);
const rotatedImage = this.rotateImage(image, orientation);
const resizedImage = this.resizeImage(rotatedImage, dimensions.width, dimensions.height);
return this.canvasToBlob(resizedImage, this.thumbnailType, 80);
}).then(blob => {
return URL.createObjectURL(blob);

@@ -216,7 +167,7 @@ });

*/
;
_proto.getProportionalDimensions = function getProportionalDimensions(img, width, height, rotation) {
var aspect = img.width / img.height;
getProportionalDimensions(img, width, height, rotation) {
let aspect = img.width / img.height;
if (rotation === 90 || rotation === 270) {

@@ -228,3 +179,3 @@ aspect = img.height / img.width;

return {
width: width,
width,
height: Math.round(width / aspect)

@@ -237,3 +188,3 @@ };

width: Math.round(height * aspect),
height: height
height
};

@@ -251,13 +202,13 @@ }

*/
;
_proto.protect = function protect(image) {
protect(image) {
// https://stackoverflow.com/questions/6081483/maximum-size-of-a-canvas-element
var ratio = image.width / image.height;
var maxSquare = 5000000; // ios max canvas square
const ratio = image.width / image.height;
const maxSquare = 5000000; // ios max canvas square
var maxSize = 4096; // ie max canvas dimensions
const maxSize = 4096; // ie max canvas dimensions
var maxW = Math.floor(Math.sqrt(maxSquare * ratio));
var maxH = Math.floor(maxSquare / Math.sqrt(maxSquare * ratio));
let maxW = Math.floor(Math.sqrt(maxSquare * ratio));
let maxH = Math.floor(maxSquare / Math.sqrt(maxSquare * ratio));

@@ -275,3 +226,3 @@ if (maxW > maxSize) {

if (image.width > maxW) {
var canvas = document.createElement('canvas');
const canvas = document.createElement('canvas');
canvas.width = maxW;

@@ -290,9 +241,9 @@ canvas.height = maxH;

*/
;
_proto.resizeImage = function resizeImage(image, targetWidth, targetHeight) {
resizeImage(image, targetWidth, targetHeight) {
// Resizing in steps refactored to use a solution from
// https://blog.uploadcare.com/image-resize-in-browsers-is-broken-e38eed08df01
image = this.protect(image);
var steps = Math.ceil(MathLog2(image.width / targetWidth));
let steps = Math.ceil(Math.log2(image.width / targetWidth));

@@ -303,8 +254,8 @@ if (steps < 1) {

var sW = targetWidth * Math.pow(2, steps - 1);
var sH = targetHeight * Math.pow(2, steps - 1);
var x = 2;
let sW = targetWidth * Math.pow(2, steps - 1);
let sH = targetHeight * Math.pow(2, steps - 1);
const x = 2;
while (steps--) {
var canvas = document.createElement('canvas');
const canvas = document.createElement('canvas');
canvas.width = sW;

@@ -319,7 +270,7 @@ canvas.height = sH;

return image;
};
}
_proto.rotateImage = function rotateImage(image, translate) {
var w = image.width;
var h = image.height;
rotateImage(image, translate) {
let w = image.width;
let h = image.height;

@@ -331,6 +282,6 @@ if (translate.deg === 90 || translate.deg === 270) {

var canvas = document.createElement('canvas');
const canvas = document.createElement('canvas');
canvas.width = w;
canvas.height = h;
var context = canvas.getContext('2d');
const context = canvas.getContext('2d');
context.translate(w / 2, h / 2);

@@ -352,5 +303,5 @@

*/
;
_proto.canvasToBlob = function canvasToBlob(canvas, type, quality) {
canvasToBlob(canvas, type, quality) {
try {

@@ -365,5 +316,5 @@ canvas.getContext('2d').getImageData(0, 0, 1, 1);

if (canvas.toBlob) {
return new Promise(function (resolve) {
return new Promise(resolve => {
canvas.toBlob(resolve, type, quality);
}).then(function (blob) {
}).then(blob => {
if (blob === null) {

@@ -377,5 +328,5 @@ throw new Error('cannot read image, probably an svg with external resources');

return Promise.resolve().then(function () {
return Promise.resolve().then(() => {
return dataURItoBlob(canvas.toDataURL(type, quality), {});
}).then(function (blob) {
}).then(blob => {
if (blob === null) {

@@ -391,11 +342,11 @@ throw new Error('could not extract blob, probably an old browser');

*/
;
_proto.setPreviewURL = function setPreviewURL(fileID, preview) {
setPreviewURL(fileID, preview) {
this.uppy.setFileState(fileID, {
preview: preview
preview
});
};
}
_proto.addToQueue = function addToQueue(item) {
addToQueue(item) {
this.queue.push(item);

@@ -406,11 +357,9 @@

}
};
}
_proto.processQueue = function processQueue() {
var _this3 = this;
processQueue() {
this.queueProcessing = true;
if (this.queue.length > 0) {
var current = this.uppy.getFile(this.queue.shift());
const current = this.uppy.getFile(this.queue.shift());

@@ -422,6 +371,4 @@ if (!current) {

return this.requestThumbnail(current).catch(function (err) {}) // eslint-disable-line handle-callback-err
.then(function () {
return _this3.processQueue();
});
return this.requestThumbnail(current).catch(err => {}) // eslint-disable-line handle-callback-err
.then(() => this.processQueue());
}

@@ -432,20 +379,14 @@

this.uppy.emit('thumbnail:all-generated');
};
}
_proto.requestThumbnail = function requestThumbnail(file) {
var _this4 = this;
requestThumbnail(file) {
if (isPreviewSupported(file.type) && !file.isRemote) {
return this.createThumbnail(file, this.opts.thumbnailWidth, this.opts.thumbnailHeight).then(function (preview) {
_this4.setPreviewURL(file.id, preview);
_this4.uppy.log("[ThumbnailGenerator] Generated thumbnail for " + file.id);
_this4.uppy.emit('thumbnail:generated', _this4.uppy.getFile(file.id), preview);
}).catch(function (err) {
_this4.uppy.log("[ThumbnailGenerator] Failed thumbnail for " + file.id + ":", 'warning');
_this4.uppy.log(err, 'warning');
_this4.uppy.emit('thumbnail:error', _this4.uppy.getFile(file.id), err);
return this.createThumbnail(file, this.opts.thumbnailWidth, this.opts.thumbnailHeight).then(preview => {
this.setPreviewURL(file.id, preview);
this.uppy.log(`[ThumbnailGenerator] Generated thumbnail for ${file.id}`);
this.uppy.emit('thumbnail:generated', this.uppy.getFile(file.id), preview);
}).catch(err => {
this.uppy.log(`[ThumbnailGenerator] Failed thumbnail for ${file.id}:`, 'warning');
this.uppy.log(err, 'warning');
this.uppy.emit('thumbnail:error', this.uppy.getFile(file.id), err);
});

@@ -455,5 +396,5 @@ }

return Promise.resolve();
};
}
_proto.install = function install() {
install() {
this.uppy.on('file-removed', this.onFileRemoved);

@@ -472,5 +413,5 @@

}
};
}
_proto.uninstall = function uninstall() {
uninstall() {
this.uppy.off('file-removed', this.onFileRemoved);

@@ -489,5 +430,4 @@

}
};
}
return ThumbnailGenerator;
}(Plugin), _class.VERSION = "1.7.11", _temp);
}, _class.VERSION = "2.0.0-alpha.0", _temp);
{
"name": "@uppy/thumbnail-generator",
"description": "Uppy plugin that generates small previews of images to show on your upload UI.",
"version": "1.7.11",
"version": "2.0.0-alpha.0",
"license": "MIT",

@@ -25,5 +25,4 @@ "main": "lib/index.js",

"dependencies": {
"@uppy/utils": "^3.6.2",
"exifr": "^6.0.0",
"math-log2": "^1.0.1"
"@uppy/utils": "^4.0.0-alpha.0",
"exifr": "^6.0.0"
},

@@ -36,3 +35,3 @@ "devDependencies": {

},
"gitHead": "4b782ffbbb443672843d7b4096956bed3b11d612"
"gitHead": "113b627dd0ef5aa5d198dc309dda05da2117dfe5"
}

@@ -15,8 +15,8 @@ # @uppy/thumbnail-generator

```js
const Uppy = require('@uppy/core')
const ThumbnailGenerator = require('@uppy/thumbnail-generator')
import Uppy from '@uppy/core'
import ThumbnailGenerator from '@uppy/thumbnail-generator'
const uppy = new Uppy()
uppy.use(ThumbnailGenerator, {
thumbnailWidth: 200
thumbnailWidth: 200,
})

@@ -23,0 +23,0 @@ ```

@@ -1,2 +0,2 @@

const { Plugin } = require('@uppy/core')
const { UIPlugin } = require('@uppy/core')
const Translator = require('@uppy/utils/lib/Translator')

@@ -6,3 +6,2 @@ const dataURItoBlob = require('@uppy/utils/lib/dataURItoBlob')

const isPreviewSupported = require('@uppy/utils/lib/isPreviewSupported')
const MathLog2 = require('math-log2') // Polyfill for IE.
const exifr = require('exifr/dist/mini.legacy.umd.js')

@@ -14,3 +13,3 @@

module.exports = class ThumbnailGenerator extends Plugin {
module.exports = class ThumbnailGenerator extends UIPlugin {
static VERSION = require('../package.json').version

@@ -46,17 +45,4 @@

}
this.i18nInit()
}
setOptions (newOpts) {
super.setOptions(newOpts)
this.i18nInit()
}
i18nInit () {
this.translator = new Translator([this.defaultLocale, this.uppy.locale, this.opts.locale])
this.i18n = this.translator.translate.bind(this.translator)
this.setPluginState() // so that UI re-renders and we see the updated locale
}
/**

@@ -71,4 +57,2 @@ * Create a thumbnail for the given Uppy file object.

createThumbnail (file, targetWidth, targetHeight) {
// bug in the compatibility data
// eslint-disable-next-line compat/compat
const originalUrl = URL.createObjectURL(file.data)

@@ -80,4 +64,2 @@

image.addEventListener('load', () => {
// bug in the compatibility data
// eslint-disable-next-line compat/compat
URL.revokeObjectURL(originalUrl)

@@ -87,4 +69,2 @@ resolve(image)

image.addEventListener('error', (event) => {
// bug in the compatibility data
// eslint-disable-next-line compat/compat
URL.revokeObjectURL(originalUrl)

@@ -105,4 +85,2 @@ reject(event.error || new Error('Could not create thumbnail'))

.then(blob => {
// bug in the compatibility data
// eslint-disable-next-line compat/compat
return URL.createObjectURL(blob)

@@ -119,3 +97,3 @@ })

getProportionalDimensions (img, width, height, rotation) {
var aspect = img.width / img.height
let aspect = img.width / img.height
if (rotation === 90 || rotation === 270) {

@@ -152,9 +130,9 @@ aspect = img.height / img.width

var ratio = image.width / image.height
const ratio = image.width / image.height
var maxSquare = 5000000 // ios max canvas square
var maxSize = 4096 // ie max canvas dimensions
const maxSquare = 5000000 // ios max canvas square
const maxSize = 4096 // ie max canvas dimensions
var maxW = Math.floor(Math.sqrt(maxSquare * ratio))
var maxH = Math.floor(maxSquare / Math.sqrt(maxSquare * ratio))
let maxW = Math.floor(Math.sqrt(maxSquare * ratio))
let maxH = Math.floor(maxSquare / Math.sqrt(maxSquare * ratio))
if (maxW > maxSize) {

@@ -169,3 +147,3 @@ maxW = maxSize

if (image.width > maxW) {
var canvas = document.createElement('canvas')
const canvas = document.createElement('canvas')
canvas.width = maxW

@@ -191,12 +169,12 @@ canvas.height = maxH

var steps = Math.ceil(MathLog2(image.width / targetWidth))
let steps = Math.ceil(Math.log2(image.width / targetWidth))
if (steps < 1) {
steps = 1
}
var sW = targetWidth * Math.pow(2, steps - 1)
var sH = targetHeight * Math.pow(2, steps - 1)
var x = 2
let sW = targetWidth * Math.pow(2, steps - 1)
let sH = targetHeight * Math.pow(2, steps - 1)
const x = 2
while (steps--) {
var canvas = document.createElement('canvas')
const canvas = document.createElement('canvas')
canvas.width = sW

@@ -215,4 +193,4 @@ canvas.height = sH

rotateImage (image, translate) {
var w = image.width
var h = image.height
let w = image.width
let h = image.height

@@ -224,7 +202,7 @@ if (translate.deg === 90 || translate.deg === 270) {

var canvas = document.createElement('canvas')
const canvas = document.createElement('canvas')
canvas.width = w
canvas.height = h
var context = canvas.getContext('2d')
const context = canvas.getContext('2d')
context.translate(w / 2, h / 2)

@@ -231,0 +209,0 @@ if (translate.canvas) {

@@ -0,4 +1,4 @@

const { UIPlugin } = require('@uppy/core')
const emitter = require('namespace-emitter')
const ThumbnailGeneratorPlugin = require('./index')
const { Plugin } = require('@uppy/core')
const emitter = require('namespace-emitter')

@@ -29,3 +29,3 @@ const delay = duration => new Promise(resolve => setTimeout(resolve, duration))

const plugin = new ThumbnailGeneratorPlugin(new MockCore(), {})
expect(plugin instanceof Plugin).toEqual(true)
expect(plugin instanceof UIPlugin).toEqual(true)
})

@@ -32,0 +32,0 @@

@@ -1,7 +0,8 @@

import Uppy = require('@uppy/core')
/* eslint-disable */
import type { Locale } from '@uppy/core'
type ThumbnailGeneratorLocale = Uppy.Locale<
| 'generatingThumbnails'
type ThumbnailGeneratorLocale = Locale<
| 'generatingThumbnails'
>
export = ThumbnailGeneratorLocale
export default ThumbnailGeneratorLocale

@@ -1,19 +0,16 @@

import Uppy = require('@uppy/core')
import ThumbnailGeneratorLocale = require('./generatedLocale')
import type { PluginOptions, UIPlugin } from '@uppy/core'
declare module ThumbnailGenerator {
interface ThumbnailGeneratorOptions extends Uppy.PluginOptions {
import ThumbnailGeneratorLocale from './generatedLocale'
interface ThumbnailGeneratorOptions extends PluginOptions {
thumbnailWidth?: number,
thumbnailHeight?: number,
thumbnailType?: string,
thumbnailType?: string,
waitForThumbnailsBeforeUpload?: boolean,
lazy?: boolean,
locale?: ThumbnailGeneratorLocale,
}
}
declare class ThumbnailGenerator extends Uppy.Plugin<
ThumbnailGenerator.ThumbnailGeneratorOptions
> {}
declare class ThumbnailGenerator extends UIPlugin<ThumbnailGeneratorOptions> {}
export = ThumbnailGenerator
export default ThumbnailGenerator

@@ -1,2 +0,18 @@

import ThumbnailGenerator = require('../')
// TODO implement
import Uppy from '@uppy/core'
import ThumbnailGenerator from '..'
{
const uppy = new Uppy()
uppy.use(ThumbnailGenerator, {
thumbnailWidth: 100,
thumbnailHeight: 100,
thumbnailType: 'type',
waitForThumbnailsBeforeUpload: true,
lazy: true,
locale: {
strings: {
generatingThumbnails: '',
},
},
})
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc