plugin.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 { DEFAULT_CACHE_HEADER, 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. private cacheHeader: string;
  153. /**
  154. * @description
  155. * Set the plugin options.
  156. */
  157. static init(options: AssetServerOptions): Type<AssetServerPlugin> {
  158. AssetServerPlugin.options = options;
  159. return this;
  160. }
  161. /** @internal */
  162. static async configure(config: RuntimeVendureConfig) {
  163. const storageStrategyFactory =
  164. this.options.storageStrategyFactory || defaultAssetStorageStrategyFactory;
  165. this.assetStorage = await storageStrategyFactory(this.options);
  166. config.assetOptions.assetPreviewStrategy =
  167. this.options.previewStrategy ??
  168. new SharpAssetPreviewStrategy({
  169. maxWidth: this.options.previewMaxWidth,
  170. maxHeight: this.options.previewMaxHeight,
  171. });
  172. config.assetOptions.assetStorageStrategy = this.assetStorage;
  173. config.assetOptions.assetNamingStrategy =
  174. this.options.namingStrategy || new HashedAssetNamingStrategy();
  175. return config;
  176. }
  177. constructor(private processContext: ProcessContext) {}
  178. /** @internal */
  179. onApplicationBootstrap(): void | Promise<void> {
  180. if (this.processContext.isWorker) {
  181. return;
  182. }
  183. if (AssetServerPlugin.options.presets) {
  184. for (const preset of AssetServerPlugin.options.presets) {
  185. const existingIndex = this.presets.findIndex(p => p.name === preset.name);
  186. if (-1 < existingIndex) {
  187. this.presets.splice(existingIndex, 1, preset);
  188. } else {
  189. this.presets.push(preset);
  190. }
  191. }
  192. }
  193. // Configure Cache-Control header
  194. const { cacheHeader } = AssetServerPlugin.options;
  195. if (!cacheHeader) {
  196. this.cacheHeader = DEFAULT_CACHE_HEADER;
  197. } else {
  198. if (typeof cacheHeader === 'string') {
  199. this.cacheHeader = cacheHeader;
  200. } else {
  201. this.cacheHeader = [cacheHeader.restriction, `max-age: ${cacheHeader.maxAge}`]
  202. .filter(value => !!value)
  203. .join(', ');
  204. }
  205. }
  206. const cachePath = path.join(AssetServerPlugin.options.assetUploadDir, this.cacheDir);
  207. fs.ensureDirSync(cachePath);
  208. }
  209. configure(consumer: MiddlewareConsumer) {
  210. if (this.processContext.isWorker) {
  211. return;
  212. }
  213. Logger.info('Creating asset server middleware', loggerCtx);
  214. consumer.apply(this.createAssetServer()).forRoutes(AssetServerPlugin.options.route);
  215. registerPluginStartupMessage('Asset server', AssetServerPlugin.options.route);
  216. }
  217. /**
  218. * Creates the image server instance
  219. */
  220. private createAssetServer() {
  221. const assetServer = express.Router();
  222. assetServer.use(this.sendAsset(), this.generateTransformedImage());
  223. return assetServer;
  224. }
  225. /**
  226. * Reads the file requested and send the response to the browser.
  227. */
  228. private sendAsset() {
  229. return async (req: Request, res: Response, next: NextFunction) => {
  230. const key = this.getFileNameFromRequest(req);
  231. try {
  232. const file = await AssetServerPlugin.assetStorage.readFileToBuffer(key);
  233. let mimeType = this.getMimeType(key);
  234. if (!mimeType) {
  235. mimeType = (await fromBuffer(file))?.mime || 'application/octet-stream';
  236. }
  237. res.contentType(mimeType);
  238. res.setHeader('content-security-policy', `default-src 'self'`);
  239. res.setHeader('Cache-Control', this.cacheHeader);
  240. res.send(file);
  241. } catch (e) {
  242. const err = new Error('File not found');
  243. (err as any).status = 404;
  244. return next(err);
  245. }
  246. };
  247. }
  248. /**
  249. * If an exception was thrown by the first handler, then it may be because a transformed image
  250. * is being requested which does not yet exist. In this case, this handler will generate the
  251. * transformed image, save it to cache, and serve the result as a response.
  252. */
  253. private generateTransformedImage() {
  254. return async (err: any, req: Request, res: Response, next: NextFunction) => {
  255. if (err && (err.status === 404 || err.statusCode === 404)) {
  256. if (req.query) {
  257. const decodedReqPath = decodeURIComponent(req.path);
  258. Logger.debug(`Pre-cached Asset not found: ${decodedReqPath}`, loggerCtx);
  259. let file: Buffer;
  260. try {
  261. file = await AssetServerPlugin.assetStorage.readFileToBuffer(decodedReqPath);
  262. } catch (err) {
  263. res.status(404).send('Resource not found');
  264. return;
  265. }
  266. const image = await transformImage(file, req.query as any, this.presets || []);
  267. try {
  268. const imageBuffer = await image.toBuffer();
  269. const cachedFileName = this.getFileNameFromRequest(req);
  270. if (!req.query.cache || req.query.cache === 'true') {
  271. await AssetServerPlugin.assetStorage.writeFileFromBuffer(
  272. cachedFileName,
  273. imageBuffer,
  274. );
  275. Logger.debug(`Saved cached asset: ${cachedFileName}`, loggerCtx);
  276. }
  277. let mimeType = this.getMimeType(cachedFileName);
  278. if (!mimeType) {
  279. mimeType = (await fromBuffer(imageBuffer))?.mime || 'image/jpeg';
  280. }
  281. res.set('Content-Type', mimeType);
  282. res.setHeader('content-security-policy', `default-src 'self'`);
  283. res.send(imageBuffer);
  284. return;
  285. } catch (e) {
  286. Logger.error(e, loggerCtx, e.stack);
  287. res.status(500).send(e.message);
  288. return;
  289. }
  290. }
  291. }
  292. next();
  293. };
  294. }
  295. private getFileNameFromRequest(req: Request): string {
  296. const { w, h, mode, preset, fpx, fpy, format } = req.query;
  297. const focalPoint = fpx && fpy ? `_fpx${fpx}_fpy${fpy}` : '';
  298. const imageFormat = getValidFormat(format);
  299. let imageParamHash: string | null = null;
  300. if (w || h) {
  301. const width = w || '';
  302. const height = h || '';
  303. imageParamHash = this.md5(`_transform_w${width}_h${height}_m${mode}${focalPoint}${imageFormat}`);
  304. } else if (preset) {
  305. if (this.presets && !!this.presets.find(p => p.name === preset)) {
  306. imageParamHash = this.md5(`_transform_pre_${preset}${focalPoint}${imageFormat}`);
  307. }
  308. } else if (imageFormat) {
  309. imageParamHash = this.md5(`_transform_${imageFormat}`);
  310. }
  311. const decodedReqPath = decodeURIComponent(req.path);
  312. if (imageParamHash) {
  313. return path.join(this.cacheDir, this.addSuffix(decodedReqPath, imageParamHash, imageFormat));
  314. } else {
  315. return decodedReqPath;
  316. }
  317. }
  318. private md5(input: string): string {
  319. return createHash('md5').update(input).digest('hex');
  320. }
  321. private addSuffix(fileName: string, suffix: string, ext?: string): string {
  322. const originalExt = path.extname(fileName);
  323. const effectiveExt = ext ? `.${ext}` : originalExt;
  324. const baseName = path.basename(fileName, originalExt);
  325. const dirName = path.dirname(fileName);
  326. return path.join(dirName, `${baseName}${suffix}${effectiveExt}`);
  327. }
  328. /**
  329. * Attempt to get the mime type from the file name.
  330. */
  331. private getMimeType(fileName: string): string | undefined {
  332. const ext = path.extname(fileName);
  333. switch (ext) {
  334. case '.jpg':
  335. case '.jpeg':
  336. return 'image/jpeg';
  337. case '.png':
  338. return 'image/png';
  339. case '.gif':
  340. return 'image/gif';
  341. case '.svg':
  342. return 'image/svg+xml';
  343. case '.tiff':
  344. return 'image/tiff';
  345. case '.webp':
  346. return 'image/webp';
  347. }
  348. }
  349. }