plugin.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. import { MiddlewareConsumer, NestModule, OnApplicationBootstrap } from '@nestjs/common';
  2. import { Type } from '@vendure/common/lib/shared-types';
  3. import {
  4. AssetStorageStrategy,
  5. Logger,
  6. PluginCommonModule,
  7. ProcessContext,
  8. registerPluginStartupMessage,
  9. RuntimeVendureConfig,
  10. VendurePlugin,
  11. } from '@vendure/core';
  12. import { createHash } from 'crypto';
  13. import express, { NextFunction, Request, Response } from 'express';
  14. import { fromBuffer } from 'file-type';
  15. import fs from 'fs-extra';
  16. import path from 'path';
  17. import { getValidFormat } from './common';
  18. import { loggerCtx } from './constants';
  19. import { defaultAssetStorageStrategyFactory } from './default-asset-storage-strategy-factory';
  20. import { HashedAssetNamingStrategy } from './hashed-asset-naming-strategy';
  21. import { SharpAssetPreviewStrategy } from './sharp-asset-preview-strategy';
  22. import { transformImage } from './transform-image';
  23. import { AssetServerOptions, ImageTransformPreset } from './types';
  24. /**
  25. * @description
  26. * The `AssetServerPlugin` serves assets (images and other files) from the local file system, and can also be configured to use
  27. * other storage strategies (e.g. {@link S3AssetStorageStrategy}. It can also perform on-the-fly image transformations
  28. * and caches the results for subsequent calls.
  29. *
  30. * ## Installation
  31. *
  32. * `yarn add \@vendure/asset-server-plugin`
  33. *
  34. * or
  35. *
  36. * `npm install \@vendure/asset-server-plugin`
  37. *
  38. * @example
  39. * ```ts
  40. * import { AssetServerPlugin } from '\@vendure/asset-server-plugin';
  41. *
  42. * const config: VendureConfig = {
  43. * // Add an instance of the plugin to the plugins array
  44. * plugins: [
  45. * AssetServerPlugin.init({
  46. * route: 'assets',
  47. * assetUploadDir: path.join(__dirname, 'assets'),
  48. * }),
  49. * ],
  50. * };
  51. * ```
  52. *
  53. * The full configuration is documented at [AssetServerOptions]({{< relref "asset-server-options" >}})
  54. *
  55. * ## Image transformation
  56. *
  57. * Asset preview images can be transformed (resized & cropped) on the fly by appending query parameters to the url:
  58. *
  59. * `http://localhost:3000/assets/some-asset.jpg?w=500&h=300&mode=resize`
  60. *
  61. * The above URL will return `some-asset.jpg`, resized to fit in the bounds of a 500px x 300px rectangle.
  62. *
  63. * ### Preview mode
  64. *
  65. * The `mode` parameter can be either `crop` or `resize`. See the [ImageTransformMode]({{< relref "image-transform-mode" >}}) docs for details.
  66. *
  67. * ### Focal point
  68. *
  69. * When cropping an image (`mode=crop`), Vendure will attempt to keep the most "interesting" area of the image in the cropped frame. It does this
  70. * by finding the area of the image with highest entropy (the busiest area of the image). However, sometimes this does not yield a satisfactory
  71. * result - part or all of the main subject may still be cropped out.
  72. *
  73. * This is where specifying the focal point can help. The focal point of the image may be specified by passing the `fpx` and `fpy` query parameters.
  74. * These are normalized coordinates (i.e. a number between 0 and 1), so the `fpx=0&fpy=0` corresponds to the top left of the image.
  75. *
  76. * For example, let's say there is a very wide landscape image which we want to crop to be square. The main subject is a house to the far left of the
  77. * image. The following query would crop it to a square with the house centered:
  78. *
  79. * `http://localhost:3000/assets/landscape.jpg?w=150&h=150&mode=crop&fpx=0.2&fpy=0.7`
  80. *
  81. * ### Format
  82. *
  83. * Since v1.7.0, the image format can be specified by adding the `format` query parameter:
  84. *
  85. * `http://localhost:3000/assets/some-asset.jpg?format=webp`
  86. *
  87. * This means that, no matter the format of your original asset files, you can use more modern formats in your storefront if the browser
  88. * supports them. Supported values for `format` are:
  89. *
  90. * * `jpeg` or `jpg`
  91. * * `png`
  92. * * `webp`
  93. * * `avif`
  94. *
  95. * The `format` parameter can also be combined with presets (see below).
  96. *
  97. * ### Transform presets
  98. *
  99. * Presets can be defined which allow a single preset name to be used instead of specifying the width, height and mode. Presets are
  100. * configured via the AssetServerOptions [presets property]({{< relref "asset-server-options" >}}#presets).
  101. *
  102. * For example, defining the following preset:
  103. *
  104. * ```ts
  105. * AssetServerPlugin.init({
  106. * // ...
  107. * presets: [
  108. * { name: 'my-preset', width: 85, height: 85, mode: 'crop' },
  109. * ],
  110. * }),
  111. * ```
  112. *
  113. * means that a request to:
  114. *
  115. * `http://localhost:3000/assets/some-asset.jpg?preset=my-preset`
  116. *
  117. * is equivalent to:
  118. *
  119. * `http://localhost:3000/assets/some-asset.jpg?w=85&h=85&mode=crop`
  120. *
  121. * The AssetServerPlugin comes pre-configured with the following presets:
  122. *
  123. * name | width | height | mode
  124. * -----|-------|--------|-----
  125. * tiny | 50px | 50px | crop
  126. * thumb | 150px | 150px | crop
  127. * small | 300px | 300px | resize
  128. * medium | 500px | 500px | resize
  129. * large | 800px | 800px | resize
  130. *
  131. * ### Caching
  132. * By default, the AssetServerPlugin will cache every transformed image, so that the transformation only needs to be performed a single time for
  133. * a given configuration. Caching can be disabled per-request by setting the `?cache=false` query parameter.
  134. *
  135. * @docsCategory AssetServerPlugin
  136. */
  137. @VendurePlugin({
  138. imports: [PluginCommonModule],
  139. configuration: config => AssetServerPlugin.configure(config),
  140. })
  141. export class AssetServerPlugin implements NestModule, OnApplicationBootstrap {
  142. private static assetStorage: AssetStorageStrategy;
  143. private readonly cacheDir = 'cache';
  144. private presets: ImageTransformPreset[] = [
  145. { name: 'tiny', width: 50, height: 50, mode: 'crop' },
  146. { name: 'thumb', width: 150, height: 150, mode: 'crop' },
  147. { name: 'small', width: 300, height: 300, mode: 'resize' },
  148. { name: 'medium', width: 500, height: 500, mode: 'resize' },
  149. { name: 'large', width: 800, height: 800, mode: 'resize' },
  150. ];
  151. private static options: AssetServerOptions;
  152. /**
  153. * @description
  154. * Set the plugin options.
  155. */
  156. static init(options: AssetServerOptions): Type<AssetServerPlugin> {
  157. AssetServerPlugin.options = options;
  158. return this;
  159. }
  160. /** @internal */
  161. static async configure(config: RuntimeVendureConfig) {
  162. const storageStrategyFactory =
  163. this.options.storageStrategyFactory || defaultAssetStorageStrategyFactory;
  164. this.assetStorage = await storageStrategyFactory(this.options);
  165. config.assetOptions.assetPreviewStrategy =
  166. this.options.previewStrategy ??
  167. new SharpAssetPreviewStrategy({
  168. maxWidth: this.options.previewMaxWidth,
  169. maxHeight: this.options.previewMaxHeight,
  170. });
  171. config.assetOptions.assetStorageStrategy = this.assetStorage;
  172. config.assetOptions.assetNamingStrategy =
  173. this.options.namingStrategy || new HashedAssetNamingStrategy();
  174. return config;
  175. }
  176. constructor(private processContext: ProcessContext) {}
  177. /** @internal */
  178. onApplicationBootstrap(): void {
  179. if (this.processContext.isWorker) {
  180. return;
  181. }
  182. if (AssetServerPlugin.options.presets) {
  183. for (const preset of AssetServerPlugin.options.presets) {
  184. const existingIndex = this.presets.findIndex(p => p.name === preset.name);
  185. if (-1 < existingIndex) {
  186. this.presets.splice(existingIndex, 1, preset);
  187. } else {
  188. this.presets.push(preset);
  189. }
  190. }
  191. }
  192. const cachePath = path.join(AssetServerPlugin.options.assetUploadDir, this.cacheDir);
  193. fs.ensureDirSync(cachePath);
  194. }
  195. configure(consumer: MiddlewareConsumer) {
  196. if (this.processContext.isWorker) {
  197. return;
  198. }
  199. Logger.info('Creating asset server middleware', loggerCtx);
  200. consumer.apply(this.createAssetServer()).forRoutes(AssetServerPlugin.options.route);
  201. registerPluginStartupMessage('Asset server', AssetServerPlugin.options.route);
  202. }
  203. /**
  204. * Creates the image server instance
  205. */
  206. private createAssetServer() {
  207. const assetServer = express.Router();
  208. assetServer.use(this.sendAsset(), this.generateTransformedImage());
  209. return assetServer;
  210. }
  211. /**
  212. * Reads the file requested and send the response to the browser.
  213. */
  214. private sendAsset() {
  215. return async (req: Request, res: Response, next: NextFunction) => {
  216. const key = this.getFileNameFromRequest(req);
  217. try {
  218. const file = await AssetServerPlugin.assetStorage.readFileToBuffer(key);
  219. let mimeType = this.getMimeType(key);
  220. if (!mimeType) {
  221. mimeType = (await fromBuffer(file))?.mime || 'application/octet-stream';
  222. }
  223. res.contentType(mimeType);
  224. res.setHeader('content-security-policy', `default-src 'self'`);
  225. res.send(file);
  226. } catch (e: any) {
  227. const err = new Error('File not found');
  228. (err as any).status = 404;
  229. return next(err);
  230. }
  231. };
  232. }
  233. /**
  234. * If an exception was thrown by the first handler, then it may be because a transformed image
  235. * is being requested which does not yet exist. In this case, this handler will generate the
  236. * transformed image, save it to cache, and serve the result as a response.
  237. */
  238. private generateTransformedImage() {
  239. return async (err: any, req: Request, res: Response, next: NextFunction) => {
  240. if (err && (err.status === 404 || err.statusCode === 404)) {
  241. if (req.query) {
  242. const decodedReqPath = decodeURIComponent(req.path);
  243. Logger.debug(`Pre-cached Asset not found: ${decodedReqPath}`, loggerCtx);
  244. let file: Buffer;
  245. try {
  246. file = await AssetServerPlugin.assetStorage.readFileToBuffer(decodedReqPath);
  247. } catch (err: any) {
  248. res.status(404).send('Resource not found');
  249. return;
  250. }
  251. const image = await transformImage(file, req.query as any, this.presets || []);
  252. try {
  253. const imageBuffer = await image.toBuffer();
  254. const cachedFileName = this.getFileNameFromRequest(req);
  255. if (!req.query.cache || req.query.cache === 'true') {
  256. await AssetServerPlugin.assetStorage.writeFileFromBuffer(
  257. cachedFileName,
  258. imageBuffer,
  259. );
  260. Logger.debug(`Saved cached asset: ${cachedFileName}`, loggerCtx);
  261. }
  262. let mimeType = this.getMimeType(cachedFileName);
  263. if (!mimeType) {
  264. mimeType = (await fromBuffer(imageBuffer))?.mime || 'image/jpeg';
  265. }
  266. res.set('Content-Type', mimeType);
  267. res.setHeader('content-security-policy', `default-src 'self'`);
  268. res.send(imageBuffer);
  269. return;
  270. } catch (e: any) {
  271. Logger.error(e, loggerCtx, e.stack);
  272. res.status(500).send(e.message);
  273. return;
  274. }
  275. }
  276. }
  277. next();
  278. };
  279. }
  280. private getFileNameFromRequest(req: Request): string {
  281. const { w, h, mode, preset, fpx, fpy, format } = req.query;
  282. const focalPoint = fpx && fpy ? `_fpx${fpx}_fpy${fpy}` : '';
  283. const imageFormat = getValidFormat(format);
  284. let imageParamHash: string | null = null;
  285. if (w || h) {
  286. const width = w || '';
  287. const height = h || '';
  288. imageParamHash = this.md5(`_transform_w${width}_h${height}_m${mode}${focalPoint}${imageFormat}`);
  289. } else if (preset) {
  290. if (this.presets && !!this.presets.find(p => p.name === preset)) {
  291. imageParamHash = this.md5(`_transform_pre_${preset}${focalPoint}${imageFormat}`);
  292. }
  293. }
  294. const decodedReqPath = decodeURIComponent(req.path);
  295. if (imageParamHash) {
  296. return path.join(this.cacheDir, this.addSuffix(decodedReqPath, imageParamHash, imageFormat));
  297. } else {
  298. return decodedReqPath;
  299. }
  300. }
  301. private md5(input: string): string {
  302. return createHash('md5').update(input).digest('hex');
  303. }
  304. private addSuffix(fileName: string, suffix: string, ext?: string): string {
  305. const originalExt = path.extname(fileName);
  306. const effectiveExt = ext ? `.${ext}` : originalExt;
  307. const baseName = path.basename(fileName, originalExt);
  308. const dirName = path.dirname(fileName);
  309. return path.join(dirName, `${baseName}${suffix}${effectiveExt}`);
  310. }
  311. /**
  312. * Attempt to get the mime type from the file name.
  313. */
  314. private getMimeType(fileName: string): string | undefined {
  315. const ext = path.extname(fileName);
  316. switch (ext) {
  317. case '.jpg':
  318. case '.jpeg':
  319. return 'image/jpeg';
  320. case '.png':
  321. return 'image/png';
  322. case '.gif':
  323. return 'image/gif';
  324. case '.svg':
  325. return 'image/svg+xml';
  326. case '.tiff':
  327. return 'image/tiff';
  328. case '.webp':
  329. return 'image/webp';
  330. }
  331. }
  332. }