@vendure/asset-server-plugin 1.6.4 → 1.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/src/common.d.ts +2 -1
- package/lib/src/common.js +17 -1
- package/lib/src/common.js.map +1 -1
- package/lib/src/plugin.d.ts +16 -0
- package/lib/src/plugin.js +40 -14
- package/lib/src/plugin.js.map +1 -1
- package/lib/src/sharp-asset-preview-strategy.d.ts +91 -5
- package/lib/src/sharp-asset-preview-strategy.js +56 -9
- package/lib/src/sharp-asset-preview-strategy.js.map +1 -1
- package/lib/src/transform-image.js +18 -0
- package/lib/src/transform-image.js.map +1 -1
- package/lib/src/types.d.ts +12 -1
- package/package.json +6 -6
package/lib/src/common.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { Request } from 'express';
|
|
2
|
-
import { AssetServerOptions } from './types';
|
|
2
|
+
import { AssetServerOptions, ImageTransformFormat } from './types';
|
|
3
3
|
export declare function getAssetUrlPrefixFn(options: AssetServerOptions): ((request: Request, identifier: string) => string) | ((...args: any[]) => string);
|
|
4
|
+
export declare function getValidFormat(format?: unknown): ImageTransformFormat | undefined;
|
package/lib/src/common.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getAssetUrlPrefixFn = void 0;
|
|
3
|
+
exports.getValidFormat = exports.getAssetUrlPrefixFn = void 0;
|
|
4
4
|
const constants_1 = require("@vendure/core/dist/common/constants");
|
|
5
5
|
function getAssetUrlPrefixFn(options) {
|
|
6
6
|
const { assetUrlPrefix, route } = options;
|
|
@@ -23,4 +23,20 @@ function getAssetUrlPrefixFn(options) {
|
|
|
23
23
|
throw new Error(`The assetUrlPrefix option was of an unexpected type: ${JSON.stringify(assetUrlPrefix)}`);
|
|
24
24
|
}
|
|
25
25
|
exports.getAssetUrlPrefixFn = getAssetUrlPrefixFn;
|
|
26
|
+
function getValidFormat(format) {
|
|
27
|
+
if (typeof format !== 'string') {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
switch (format) {
|
|
31
|
+
case 'jpg':
|
|
32
|
+
case 'jpeg':
|
|
33
|
+
case 'png':
|
|
34
|
+
case 'webp':
|
|
35
|
+
case 'avif':
|
|
36
|
+
return format;
|
|
37
|
+
default:
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.getValidFormat = getValidFormat;
|
|
26
42
|
//# sourceMappingURL=common.js.map
|
package/lib/src/common.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"common.js","sourceRoot":"","sources":["../../src/common.ts"],"names":[],"mappings":";;;AAAA,mEAA0E;AAK1E,SAAgB,mBAAmB,CAAC,OAA2B;IAC3D,MAAM,EAAE,cAAc,EAAE,KAAK,EAAE,GAAG,OAAO,CAAC;IAC1C,IAAI,cAAc,IAAI,IAAI,EAAE;QACxB,OAAO,CAAC,OAAgB,EAAE,UAAkB,EAAE,EAAE;;YAC5C,MAAM,QAAQ,GAAG,MAAA,OAAO,CAAC,OAAO,CAAC,mBAAmB,CAAC,mCAAI,OAAO,CAAC,QAAQ,CAAC;YAC1E,OAAO,GAAG,QAAQ,MAAM,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,KAAK,GAAG,CAAC;QAC5D,CAAC,CAAC;KACL;IACD,IAAI,OAAO,cAAc,KAAK,QAAQ,EAAE;QACpC,OAAO,CAAC,GAAG,IAAW,EAAE,EAAE,CAAC,cAAc,CAAC;KAC7C;IACD,IAAI,OAAO,cAAc,KAAK,UAAU,EAAE;QACtC,OAAO,CAAC,OAAgB,EAAE,UAAkB,EAAE,EAAE;YAC5C,MAAM,GAAG,GAAI,OAAe,CAAC,+BAAmB,CAAC,CAAC;YAClD,OAAO,cAAc,CAAC,GAAG,EAAE,UAAU,CAAC,CAAC;QAC3C,CAAC,CAA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AC;QACX,KAAK,MAAM,CAAC;QACZ,KAAK,KAAK,CAAC;QACX,KAAK,MAAM,CAAC;QACZ,KAAK,MAAM;YACP,OAAO,MAAM,CAAC;QAClB;YACI,OAAO,SAAS,CAAC;KACxB;AACL,CAAC;AAdD,wCAcC"}
|
package/lib/src/plugin.d.ts
CHANGED
|
@@ -59,6 +59,22 @@ import { AssetServerOptions } from './types';
|
|
|
59
59
|
*
|
|
60
60
|
* `http://localhost:3000/assets/landscape.jpg?w=150&h=150&mode=crop&fpx=0.2&fpy=0.7`
|
|
61
61
|
*
|
|
62
|
+
* ### Format
|
|
63
|
+
*
|
|
64
|
+
* Since v1.7.0, the image format can be specified by adding the `format` query parameter:
|
|
65
|
+
*
|
|
66
|
+
* `http://localhost:3000/assets/some-asset.jpg?format=webp`
|
|
67
|
+
*
|
|
68
|
+
* This means that, no matter the format of your original asset files, you can use more modern formats in your storefront if the browser
|
|
69
|
+
* supports them. Supported values for `format` are:
|
|
70
|
+
*
|
|
71
|
+
* * `jpeg` or `jpg`
|
|
72
|
+
* * `png`
|
|
73
|
+
* * `webp`
|
|
74
|
+
* * `avif`
|
|
75
|
+
*
|
|
76
|
+
* The `format` parameter can also be combined with presets (see below).
|
|
77
|
+
*
|
|
62
78
|
* ### Transform presets
|
|
63
79
|
*
|
|
64
80
|
* Presets can be defined which allow a single preset name to be used instead of specifying the width, height and mode. Presets are
|
package/lib/src/plugin.js
CHANGED
|
@@ -20,6 +20,7 @@ const express_1 = __importDefault(require("express"));
|
|
|
20
20
|
const file_type_1 = require("file-type");
|
|
21
21
|
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
22
22
|
const path_1 = __importDefault(require("path"));
|
|
23
|
+
const common_1 = require("./common");
|
|
23
24
|
const constants_1 = require("./constants");
|
|
24
25
|
const default_asset_storage_strategy_factory_1 = require("./default-asset-storage-strategy-factory");
|
|
25
26
|
const hashed_asset_naming_strategy_1 = require("./hashed-asset-naming-strategy");
|
|
@@ -82,6 +83,22 @@ const transform_image_1 = require("./transform-image");
|
|
|
82
83
|
*
|
|
83
84
|
* `http://localhost:3000/assets/landscape.jpg?w=150&h=150&mode=crop&fpx=0.2&fpy=0.7`
|
|
84
85
|
*
|
|
86
|
+
* ### Format
|
|
87
|
+
*
|
|
88
|
+
* Since v1.7.0, the image format can be specified by adding the `format` query parameter:
|
|
89
|
+
*
|
|
90
|
+
* `http://localhost:3000/assets/some-asset.jpg?format=webp`
|
|
91
|
+
*
|
|
92
|
+
* This means that, no matter the format of your original asset files, you can use more modern formats in your storefront if the browser
|
|
93
|
+
* supports them. Supported values for `format` are:
|
|
94
|
+
*
|
|
95
|
+
* * `jpeg` or `jpg`
|
|
96
|
+
* * `png`
|
|
97
|
+
* * `webp`
|
|
98
|
+
* * `avif`
|
|
99
|
+
*
|
|
100
|
+
* The `format` parameter can also be combined with presets (see below).
|
|
101
|
+
*
|
|
85
102
|
* ### Transform presets
|
|
86
103
|
*
|
|
87
104
|
* Presets can be defined which allow a single preset name to be used instead of specifying the width, height and mode. Presets are
|
|
@@ -144,12 +161,14 @@ let AssetServerPlugin = AssetServerPlugin_1 = class AssetServerPlugin {
|
|
|
144
161
|
}
|
|
145
162
|
/** @internal */
|
|
146
163
|
static async configure(config) {
|
|
164
|
+
var _a;
|
|
147
165
|
const storageStrategyFactory = this.options.storageStrategyFactory || default_asset_storage_strategy_factory_1.defaultAssetStorageStrategyFactory;
|
|
148
166
|
this.assetStorage = await storageStrategyFactory(this.options);
|
|
149
|
-
config.assetOptions.assetPreviewStrategy =
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
167
|
+
config.assetOptions.assetPreviewStrategy =
|
|
168
|
+
(_a = this.options.previewStrategy) !== null && _a !== void 0 ? _a : new sharp_asset_preview_strategy_1.SharpAssetPreviewStrategy({
|
|
169
|
+
maxWidth: this.options.previewMaxWidth,
|
|
170
|
+
maxHeight: this.options.previewMaxHeight,
|
|
171
|
+
});
|
|
153
172
|
config.assetOptions.assetStorageStrategy = this.assetStorage;
|
|
154
173
|
config.assetOptions.assetNamingStrategy =
|
|
155
174
|
this.options.namingStrategy || new hashed_asset_naming_strategy_1.HashedAssetNamingStrategy();
|
|
@@ -221,6 +240,7 @@ let AssetServerPlugin = AssetServerPlugin_1 = class AssetServerPlugin {
|
|
|
221
240
|
*/
|
|
222
241
|
generateTransformedImage() {
|
|
223
242
|
return async (err, req, res, next) => {
|
|
243
|
+
var _a;
|
|
224
244
|
if (err && (err.status === 404 || err.statusCode === 404)) {
|
|
225
245
|
if (req.query) {
|
|
226
246
|
const decodedReqPath = decodeURIComponent(req.path);
|
|
@@ -236,12 +256,16 @@ let AssetServerPlugin = AssetServerPlugin_1 = class AssetServerPlugin {
|
|
|
236
256
|
const image = await transform_image_1.transformImage(file, req.query, this.presets || []);
|
|
237
257
|
try {
|
|
238
258
|
const imageBuffer = await image.toBuffer();
|
|
259
|
+
const cachedFileName = this.getFileNameFromRequest(req);
|
|
239
260
|
if (!req.query.cache || req.query.cache === 'true') {
|
|
240
|
-
const cachedFileName = this.getFileNameFromRequest(req);
|
|
241
261
|
await AssetServerPlugin_1.assetStorage.writeFileFromBuffer(cachedFileName, imageBuffer);
|
|
242
262
|
core_1.Logger.debug(`Saved cached asset: ${cachedFileName}`, constants_1.loggerCtx);
|
|
243
263
|
}
|
|
244
|
-
|
|
264
|
+
let mimeType = this.getMimeType(cachedFileName);
|
|
265
|
+
if (!mimeType) {
|
|
266
|
+
mimeType = ((_a = (await file_type_1.fromBuffer(imageBuffer))) === null || _a === void 0 ? void 0 : _a.mime) || 'image/jpeg';
|
|
267
|
+
}
|
|
268
|
+
res.set('Content-Type', mimeType);
|
|
245
269
|
res.setHeader('content-security-policy', `default-src 'self'`);
|
|
246
270
|
res.send(imageBuffer);
|
|
247
271
|
return;
|
|
@@ -257,22 +281,23 @@ let AssetServerPlugin = AssetServerPlugin_1 = class AssetServerPlugin {
|
|
|
257
281
|
};
|
|
258
282
|
}
|
|
259
283
|
getFileNameFromRequest(req) {
|
|
260
|
-
const { w, h, mode, preset, fpx, fpy } = req.query;
|
|
284
|
+
const { w, h, mode, preset, fpx, fpy, format } = req.query;
|
|
261
285
|
const focalPoint = fpx && fpy ? `_fpx${fpx}_fpy${fpy}` : '';
|
|
286
|
+
const imageFormat = common_1.getValidFormat(format);
|
|
262
287
|
let imageParamHash = null;
|
|
263
288
|
if (w || h) {
|
|
264
289
|
const width = w || '';
|
|
265
290
|
const height = h || '';
|
|
266
|
-
imageParamHash = this.md5(`_transform_w${width}_h${height}_m${mode}${focalPoint}`);
|
|
291
|
+
imageParamHash = this.md5(`_transform_w${width}_h${height}_m${mode}${focalPoint}${imageFormat}`);
|
|
267
292
|
}
|
|
268
293
|
else if (preset) {
|
|
269
294
|
if (this.presets && !!this.presets.find(p => p.name === preset)) {
|
|
270
|
-
imageParamHash = this.md5(`_transform_pre_${preset}${focalPoint}`);
|
|
295
|
+
imageParamHash = this.md5(`_transform_pre_${preset}${focalPoint}${imageFormat}`);
|
|
271
296
|
}
|
|
272
297
|
}
|
|
273
298
|
const decodedReqPath = decodeURIComponent(req.path);
|
|
274
299
|
if (imageParamHash) {
|
|
275
|
-
return path_1.default.join(this.cacheDir, this.addSuffix(decodedReqPath, imageParamHash));
|
|
300
|
+
return path_1.default.join(this.cacheDir, this.addSuffix(decodedReqPath, imageParamHash, imageFormat));
|
|
276
301
|
}
|
|
277
302
|
else {
|
|
278
303
|
return decodedReqPath;
|
|
@@ -281,11 +306,12 @@ let AssetServerPlugin = AssetServerPlugin_1 = class AssetServerPlugin {
|
|
|
281
306
|
md5(input) {
|
|
282
307
|
return crypto_1.createHash('md5').update(input).digest('hex');
|
|
283
308
|
}
|
|
284
|
-
addSuffix(fileName, suffix) {
|
|
285
|
-
const
|
|
286
|
-
const
|
|
309
|
+
addSuffix(fileName, suffix, ext) {
|
|
310
|
+
const originalExt = path_1.default.extname(fileName);
|
|
311
|
+
const effectiveExt = ext ? `.${ext}` : originalExt;
|
|
312
|
+
const baseName = path_1.default.basename(fileName, originalExt);
|
|
287
313
|
const dirName = path_1.default.dirname(fileName);
|
|
288
|
-
return path_1.default.join(dirName, `${baseName}${suffix}${
|
|
314
|
+
return path_1.default.join(dirName, `${baseName}${suffix}${effectiveExt}`);
|
|
289
315
|
}
|
|
290
316
|
/**
|
|
291
317
|
* Attempt to get the mime type from the file name.
|
package/lib/src/plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../src/plugin.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAEA,wCAQuB;AACvB,mCAAoC;AACpC,sDAAmE;AACnE,yCAAuC;AACvC,wDAA0B;AAC1B,gDAAwB;AAExB,2CAAwC;AACxC,qGAA8F;AAC9F,iFAA2E;AAC3E,iFAA2E;AAC3E,uDAAmD;AAGnD
|
|
1
|
+
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../src/plugin.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAEA,wCAQuB;AACvB,mCAAoC;AACpC,sDAAmE;AACnE,yCAAuC;AACvC,wDAA0B;AAC1B,gDAAwB;AAExB,qCAA0C;AAC1C,2CAAwC;AACxC,qGAA8F;AAC9F,iFAA2E;AAC3E,iFAA2E;AAC3E,uDAAmD;AAGnD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgHG;AAKH,IAAa,iBAAiB,yBAA9B,MAAa,iBAAiB;IAsC1B,YAAoB,cAA8B;QAA9B,mBAAc,GAAd,cAAc,CAAgB;QApCjC,aAAQ,GAAG,OAAO,CAAC;QAC5B,YAAO,GAA2B;YACtC,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE;YACrD,EAAE,IAAI,EAAE,OAAO,EAAE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,MAAM;gBACP,OAAO,WAAW,CAAC;YACvB,KAAK,MAAM;gBACP,OAAO,eAAe,CAAC;YAC3B,KAAK,OAAO;gBACR,OAAO,YAAY,CAAC;YACxB,KAAK,OAAO;gBACR,OAAO,YAAY,CAAC;SAC3B;IACL,CAAC;CACJ,CAAA;AA7MY,iBAAiB;IAJ7B,oBAAa,CAAC;QACX,OAAO,EAAE,CAAC,yBAAkB,CAAC;QAC7B,aAAa,EAAE,MAAM,CAAC,EAAE,CAAC,mBAAiB,CAAC,SAAS,CAAC,MAAM,CAAC;KAC/D,CAAC;qCAuCsC,qBAAc;GAtCzC,iBAAiB,CA6M7B;AA7MY,8CAAiB"}
|
|
@@ -1,12 +1,98 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { AssetPreviewStrategy, RequestContext } from '@vendure/core';
|
|
3
|
+
import sharp from 'sharp';
|
|
4
|
+
/**
|
|
5
|
+
* @description
|
|
6
|
+
* This {@link AssetPreviewStrategy} uses the [Sharp library](https://sharp.pixelplumbing.com/) to generate
|
|
7
|
+
* preview images of uploaded binary files. For non-image binaries, a generic "file" icon with the mime type
|
|
8
|
+
* overlay will be generated.
|
|
9
|
+
*
|
|
10
|
+
* @docsCategory AssetServerPlugin
|
|
11
|
+
* @docsPage SharpAssetPreviewStrategy
|
|
12
|
+
*/
|
|
13
|
+
interface SharpAssetPreviewConfig {
|
|
14
|
+
/**
|
|
15
|
+
* @description
|
|
16
|
+
* The max height in pixels of a generated preview image.
|
|
17
|
+
*
|
|
18
|
+
* @default 1600
|
|
19
|
+
*/
|
|
20
|
+
maxHeight?: number;
|
|
21
|
+
/**
|
|
22
|
+
* @description
|
|
23
|
+
* The max width in pixels of a generated preview image.
|
|
24
|
+
*
|
|
25
|
+
* @default 1600
|
|
26
|
+
*/
|
|
27
|
+
maxWidth?: number;
|
|
28
|
+
/**
|
|
29
|
+
* @description
|
|
30
|
+
* Set Sharp's options for encoding jpeg files: https://sharp.pixelplumbing.com/api-output#jpeg
|
|
31
|
+
*
|
|
32
|
+
* @since 1.7.0
|
|
33
|
+
*/
|
|
34
|
+
jpegOptions?: sharp.JpegOptions;
|
|
35
|
+
/**
|
|
36
|
+
* @description
|
|
37
|
+
* Set Sharp's options for encoding png files: https://sharp.pixelplumbing.com/api-output#png
|
|
38
|
+
*
|
|
39
|
+
* @since 1.7.0
|
|
40
|
+
*/
|
|
41
|
+
pngOptions?: sharp.PngOptions;
|
|
42
|
+
/**
|
|
43
|
+
* @description
|
|
44
|
+
* Set Sharp's options for encoding webp files: https://sharp.pixelplumbing.com/api-output#webp
|
|
45
|
+
*
|
|
46
|
+
* @since 1.7.0
|
|
47
|
+
*/
|
|
48
|
+
webpOptions?: sharp.WebpOptions;
|
|
49
|
+
/**
|
|
50
|
+
* @description
|
|
51
|
+
* Set Sharp's options for encoding gif files: https://sharp.pixelplumbing.com/api-output#gif
|
|
52
|
+
*
|
|
53
|
+
* @since 1.7.0
|
|
54
|
+
*/
|
|
55
|
+
gifOptions?: sharp.GifOptions;
|
|
56
|
+
/**
|
|
57
|
+
* @description
|
|
58
|
+
* Set Sharp's options for encoding avif files: https://sharp.pixelplumbing.com/api-output#avif
|
|
59
|
+
*
|
|
60
|
+
* @since 1.7.0
|
|
61
|
+
*/
|
|
62
|
+
avifOptions?: sharp.AvifOptions;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* @description
|
|
66
|
+
* This {@link AssetPreviewStrategy} uses the [Sharp library](https://sharp.pixelplumbing.com/) to generate
|
|
67
|
+
* preview images of uploaded binary files. For non-image binaries, a generic "file" icon with the mime type
|
|
68
|
+
* overlay will be generated.
|
|
69
|
+
*
|
|
70
|
+
* By default, this strategy will produce previews up to maximum dimensions of 1600 x 1600 pixels. The created
|
|
71
|
+
* preview images will match the input format - so a source file in jpeg format will output a jpeg preview,
|
|
72
|
+
* a webp source file will output a webp preview, and so on.
|
|
73
|
+
*
|
|
74
|
+
* The settings for the outputs will default to Sharp's defaults (https://sharp.pixelplumbing.com/api-output).
|
|
75
|
+
* However, it is possible to pass your own configurations to control the output of each format:
|
|
76
|
+
*
|
|
77
|
+
* ```TypeScript
|
|
78
|
+
* AssetServerPlugin.init({
|
|
79
|
+
* previewStrategy: new SharpAssetPreviewStrategy({
|
|
80
|
+
* jpegOptions: { quality: 95 },
|
|
81
|
+
* webpOptions: { quality: 95 },
|
|
82
|
+
* }),
|
|
83
|
+
* }),
|
|
84
|
+
* ```
|
|
85
|
+
*
|
|
86
|
+
* @docsCategory AssetServerPlugin
|
|
87
|
+
* @docsPage SharpAssetPreviewStrategy
|
|
88
|
+
* @docsWeight 0
|
|
89
|
+
*/
|
|
3
90
|
export declare class SharpAssetPreviewStrategy implements AssetPreviewStrategy {
|
|
4
|
-
private
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
maxWidth: number;
|
|
8
|
-
});
|
|
91
|
+
private readonly defaultConfig;
|
|
92
|
+
private readonly config;
|
|
93
|
+
constructor(config?: SharpAssetPreviewConfig);
|
|
9
94
|
generatePreviewImage(ctx: RequestContext, mimeType: string, data: Buffer): Promise<Buffer>;
|
|
10
95
|
private generateMimeTypeOverlay;
|
|
11
96
|
private generateBinaryFilePreview;
|
|
12
97
|
}
|
|
98
|
+
export {};
|
|
@@ -9,9 +9,44 @@ const core_1 = require("@vendure/core");
|
|
|
9
9
|
const path_1 = __importDefault(require("path"));
|
|
10
10
|
const sharp_1 = __importDefault(require("sharp"));
|
|
11
11
|
const constants_1 = require("./constants");
|
|
12
|
+
/**
|
|
13
|
+
* @description
|
|
14
|
+
* This {@link AssetPreviewStrategy} uses the [Sharp library](https://sharp.pixelplumbing.com/) to generate
|
|
15
|
+
* preview images of uploaded binary files. For non-image binaries, a generic "file" icon with the mime type
|
|
16
|
+
* overlay will be generated.
|
|
17
|
+
*
|
|
18
|
+
* By default, this strategy will produce previews up to maximum dimensions of 1600 x 1600 pixels. The created
|
|
19
|
+
* preview images will match the input format - so a source file in jpeg format will output a jpeg preview,
|
|
20
|
+
* a webp source file will output a webp preview, and so on.
|
|
21
|
+
*
|
|
22
|
+
* The settings for the outputs will default to Sharp's defaults (https://sharp.pixelplumbing.com/api-output).
|
|
23
|
+
* However, it is possible to pass your own configurations to control the output of each format:
|
|
24
|
+
*
|
|
25
|
+
* ```TypeScript
|
|
26
|
+
* AssetServerPlugin.init({
|
|
27
|
+
* previewStrategy: new SharpAssetPreviewStrategy({
|
|
28
|
+
* jpegOptions: { quality: 95 },
|
|
29
|
+
* webpOptions: { quality: 95 },
|
|
30
|
+
* }),
|
|
31
|
+
* }),
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @docsCategory AssetServerPlugin
|
|
35
|
+
* @docsPage SharpAssetPreviewStrategy
|
|
36
|
+
* @docsWeight 0
|
|
37
|
+
*/
|
|
12
38
|
class SharpAssetPreviewStrategy {
|
|
13
39
|
constructor(config) {
|
|
14
|
-
this.
|
|
40
|
+
this.defaultConfig = {
|
|
41
|
+
maxHeight: 1600,
|
|
42
|
+
maxWidth: 1600,
|
|
43
|
+
jpegOptions: {},
|
|
44
|
+
pngOptions: {},
|
|
45
|
+
webpOptions: {},
|
|
46
|
+
gifOptions: {},
|
|
47
|
+
avifOptions: {},
|
|
48
|
+
};
|
|
49
|
+
this.config = Object.assign(Object.assign({}, this.defaultConfig), (config !== null && config !== void 0 ? config : {}));
|
|
15
50
|
}
|
|
16
51
|
async generatePreviewImage(ctx, mimeType, data) {
|
|
17
52
|
var _a;
|
|
@@ -19,20 +54,32 @@ class SharpAssetPreviewStrategy {
|
|
|
19
54
|
const { maxWidth, maxHeight } = this.config;
|
|
20
55
|
if (assetType === generated_types_1.AssetType.IMAGE) {
|
|
21
56
|
try {
|
|
22
|
-
const image = sharp_1.default(data);
|
|
57
|
+
const image = sharp_1.default(data).rotate();
|
|
23
58
|
const metadata = await image.metadata();
|
|
24
59
|
const width = metadata.width || 0;
|
|
25
60
|
const height = metadata.height || 0;
|
|
26
61
|
if (maxWidth < width || maxHeight < height) {
|
|
27
|
-
|
|
62
|
+
image.resize(maxWidth, maxHeight, { fit: 'inside' });
|
|
63
|
+
}
|
|
64
|
+
if (mimeType === 'image/svg+xml') {
|
|
65
|
+
// Convert the SVG to a raster for the preview
|
|
66
|
+
return image.toBuffer();
|
|
28
67
|
}
|
|
29
68
|
else {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
69
|
+
switch (metadata.format) {
|
|
70
|
+
case 'jpeg':
|
|
71
|
+
case 'jpg':
|
|
72
|
+
return image.jpeg(this.config.jpegOptions).toBuffer();
|
|
73
|
+
case 'png':
|
|
74
|
+
return image.png(this.config.pngOptions).toBuffer();
|
|
75
|
+
case 'webp':
|
|
76
|
+
return image.webp(this.config.webpOptions).toBuffer();
|
|
77
|
+
case 'gif':
|
|
78
|
+
return image.gif(this.config.jpegOptions).toBuffer();
|
|
79
|
+
case 'avif':
|
|
80
|
+
return image.avif(this.config.avifOptions).toBuffer();
|
|
81
|
+
default:
|
|
82
|
+
return image.toBuffer();
|
|
36
83
|
}
|
|
37
84
|
}
|
|
38
85
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sharp-asset-preview-strategy.js","sourceRoot":"","sources":["../../src/sharp-asset-preview-strategy.ts"],"names":[],"mappings":";;;;;;AAAA,yEAAgE;AAChE,wCAA2F;AAC3F,gDAAwB;AACxB,kDAA0B;AAE1B,2CAAwC;
|
|
1
|
+
{"version":3,"file":"sharp-asset-preview-strategy.js","sourceRoot":"","sources":["../../src/sharp-asset-preview-strategy.ts"],"names":[],"mappings":";;;;;;AAAA,yEAAgE;AAChE,wCAA2F;AAC3F,gDAAwB;AACxB,kDAA0B;AAE1B,2CAAwC;AA+DxC;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAa,yBAAyB;IAYlC,YAAY,MAAgC;QAX3B,kBAAa,GAAsC;YAChE,SAAS,EAAE,IAAI;YACf,QAAQ,EAAE,IAAI;Y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uBAAuB,CAAC,QAAQ,CAAC;gBAC7C,OAAO,EAAE,eAAK,CAAC,OAAO,CAAC,MAAM;aAChC;SACJ,CAAC;aACD,QAAQ,EAAE,CAAC;IACpB,CAAC;CACJ;AA7FD,8DA6FC"}
|
|
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.resizeToFocalPoint = exports.transformImage = void 0;
|
|
7
7
|
const sharp_1 = __importDefault(require("sharp"));
|
|
8
|
+
const common_1 = require("./common");
|
|
8
9
|
/**
|
|
9
10
|
* Applies transforms to the given image according to the query params passed.
|
|
10
11
|
*/
|
|
@@ -14,6 +15,7 @@ async function transformImage(originalImage, queryParams, presets) {
|
|
|
14
15
|
let mode = queryParams.mode || 'crop';
|
|
15
16
|
const fpx = +queryParams.fpx || undefined;
|
|
16
17
|
const fpy = +queryParams.fpy || undefined;
|
|
18
|
+
const imageFormat = common_1.getValidFormat(queryParams.format);
|
|
17
19
|
if (queryParams.preset) {
|
|
18
20
|
const matchingPreset = presets.find(p => p.name === queryParams.preset);
|
|
19
21
|
if (matchingPreset) {
|
|
@@ -30,6 +32,7 @@ async function transformImage(originalImage, queryParams, presets) {
|
|
|
30
32
|
options.fit = 'inside';
|
|
31
33
|
}
|
|
32
34
|
const image = sharp_1.default(originalImage);
|
|
35
|
+
applyFormat(image, imageFormat);
|
|
33
36
|
if (fpx && fpy && targetWidth && targetHeight && mode === 'crop') {
|
|
34
37
|
const metadata = await image.metadata();
|
|
35
38
|
if (metadata.width && metadata.height) {
|
|
@@ -42,6 +45,21 @@ async function transformImage(originalImage, queryParams, presets) {
|
|
|
42
45
|
return image.resize(targetWidth, targetHeight, options);
|
|
43
46
|
}
|
|
44
47
|
exports.transformImage = transformImage;
|
|
48
|
+
function applyFormat(image, format) {
|
|
49
|
+
switch (format) {
|
|
50
|
+
case 'jpg':
|
|
51
|
+
case 'jpeg':
|
|
52
|
+
return image.jpeg();
|
|
53
|
+
case 'png':
|
|
54
|
+
return image.png();
|
|
55
|
+
case 'webp':
|
|
56
|
+
return image.webp();
|
|
57
|
+
case 'avif':
|
|
58
|
+
return image.avif();
|
|
59
|
+
default:
|
|
60
|
+
return image;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
45
63
|
/**
|
|
46
64
|
* Resize an image but keep it centered on the focal point.
|
|
47
65
|
* Based on the method outlined in https://github.com/lovell/sharp/issues/1198#issuecomment-384591756
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transform-image.js","sourceRoot":"","sources":["../../src/transform-image.ts"],"names":[],"mappings":";;;;;;AAAA,kDAAqD;
|
|
1
|
+
{"version":3,"file":"transform-image.js","sourceRoot":"","sources":["../../src/transform-image.ts"],"names":[],"mappings":";;;;;;AAAA,kDAAqD;AAErD,qCAA0C;AAM1C;;GAEG;AACI,KAAK,UAAU,cAAc,CAChC,aAAqB,EACrB,WAAmC,EACnC,OAA+B;IAE/B,IAAI,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,IAAI,SAAS,CAAC;IAC1D,IAAI,YAAY,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,IAAI,SAAS,CAAC;IAC3D,IAAI,IAAI,GAAG,WAAW,CAAC,IAAI,IAAI,MAAM,CAAC;IACtC,MAAM,GAAG,GAAG,CAAC,WAAW,CAAC,GAAG,IAAI,SAAS,CAAC;IAC1C,MAAM,GAAG,GAAG,CAAC,WAAW,CAAC,GAAG,IAAI,SAAS,CAAC;IAC1C,MAAM,WAAW,GAAG,uBAAc,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;IACvD,IAAI,WAAW,CAAC,MAAM,EAAE;QACpB,MAAM,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,WAAW,CAAC,MAAM,CAAC,CA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CAAC;AAClB,CAAC;AAED;;GAEG;AACH,SAAS,KAAK,CAAC,GAAW,EAAE,GAAW,EAAE,KAAa;IAClD,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,GAAG,CAAC,CAAC;AAC/C,CAAC"}
|
package/lib/src/types.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { AssetNamingStrategy, AssetStorageStrategy, RequestContext } from '@vendure/core';
|
|
1
|
+
import { AssetNamingStrategy, AssetPreviewStrategy, AssetStorageStrategy, RequestContext } from '@vendure/core';
|
|
2
|
+
export declare type ImageTransformFormat = 'jpg' | 'jpeg' | 'png' | 'webp' | 'avif';
|
|
2
3
|
/**
|
|
3
4
|
* @description
|
|
4
5
|
* Specifies the way in which an asset preview image will be resized to fit in the
|
|
@@ -66,6 +67,7 @@ export interface AssetServerOptions {
|
|
|
66
67
|
* The max width in pixels of a generated preview image.
|
|
67
68
|
*
|
|
68
69
|
* @default 1600
|
|
70
|
+
* @deprecated Use `previewStrategy: new SharpAssetPreviewStrategy({ maxWidth })` instead
|
|
69
71
|
*/
|
|
70
72
|
previewMaxWidth?: number;
|
|
71
73
|
/**
|
|
@@ -73,6 +75,7 @@ export interface AssetServerOptions {
|
|
|
73
75
|
* The max height in pixels of a generated preview image.
|
|
74
76
|
*
|
|
75
77
|
* @default 1600
|
|
78
|
+
* @deprecated Use `previewStrategy: new SharpAssetPreviewStrategy({ maxHeight })` instead
|
|
76
79
|
*/
|
|
77
80
|
previewMaxHeight?: number;
|
|
78
81
|
/**
|
|
@@ -87,6 +90,14 @@ export interface AssetServerOptions {
|
|
|
87
90
|
* @default HashedAssetNamingStrategy
|
|
88
91
|
*/
|
|
89
92
|
namingStrategy?: AssetNamingStrategy;
|
|
93
|
+
/**
|
|
94
|
+
* @description
|
|
95
|
+
* Defines how previews are generated for a given Asset binary. By default, this uses
|
|
96
|
+
* the {@link SharpAssetPreviewStrategy}
|
|
97
|
+
*
|
|
98
|
+
* @since 1.7.0
|
|
99
|
+
*/
|
|
100
|
+
previewStrategy?: AssetPreviewStrategy;
|
|
90
101
|
/**
|
|
91
102
|
* @description
|
|
92
103
|
* A function which can be used to configure an {@link AssetStorageStrategy}. This is useful e.g. if you wish to store your assets
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vendure/asset-server-plugin",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.1",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"types": "lib/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -23,9 +23,9 @@
|
|
|
23
23
|
"@types/express": "^4.17.8",
|
|
24
24
|
"@types/fs-extra": "^9.0.8",
|
|
25
25
|
"@types/node-fetch": "^2.5.8",
|
|
26
|
-
"@types/sharp": "^0.
|
|
27
|
-
"@vendure/common": "^1.
|
|
28
|
-
"@vendure/core": "^1.
|
|
26
|
+
"@types/sharp": "^0.30.4",
|
|
27
|
+
"@vendure/common": "^1.7.1",
|
|
28
|
+
"@vendure/core": "^1.7.1",
|
|
29
29
|
"aws-sdk": "^2.856.0",
|
|
30
30
|
"express": "^4.17.1",
|
|
31
31
|
"node-fetch": "^2.6.1",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"file-type": "^16.2.0",
|
|
37
37
|
"fs-extra": "^10.0.0",
|
|
38
|
-
"sharp": "~0.
|
|
38
|
+
"sharp": "~0.30.7"
|
|
39
39
|
},
|
|
40
|
-
"gitHead": "
|
|
40
|
+
"gitHead": "2fbdc409ba7f1bef12eee1d511ce953b8a0bf0dd"
|
|
41
41
|
}
|