plugin.ts 14 KB

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