Просмотр исходного кода

refactor: Move LocalAssetStorageStrategy into core

Michael Bromley 6 лет назад
Родитель
Сommit
eda204fda3

+ 0 - 1
packages/asset-server-plugin/index.ts

@@ -1,3 +1,2 @@
 export * from './src/plugin';
 export * from './src/sharp-asset-preview-strategy';
-export * from './src/local-asset-storage-strategy';

+ 12 - 3
packages/asset-server-plugin/src/plugin.ts

@@ -1,11 +1,10 @@
-import { AssetStorageStrategy, InjectorFn, VendureConfig, VendurePlugin } from '@vendure/core';
+import { AssetStorageStrategy, InjectorFn, LocalAssetStorageStrategy, VendureConfig, VendurePlugin } from '@vendure/core';
 import express, { NextFunction, Request, Response } from 'express';
 import { Server } from 'http';
 import proxy from 'http-proxy-middleware';
 import path from 'path';
 
 import { SharpAssetPreviewStrategy } from './sharp-asset-preview-strategy';
-import { LocalAssetStorageStrategy } from './local-asset-storage-strategy';
 import { transformImage } from './transform-image';
 
 /**
@@ -121,7 +120,7 @@ export class AssetServerPlugin implements VendurePlugin {
     }
 
     configure(config: Required<VendureConfig>) {
-        this.assetStorage = new LocalAssetStorageStrategy(this.options.assetUploadDir, this.options.route);
+        this.assetStorage = this.createAssetStorageStrategy();
         config.assetOptions.assetPreviewStrategy = new SharpAssetPreviewStrategy({
             maxWidth: this.options.previewMaxWidth || 1600,
             maxHeight: this.options.previewMaxHeight || 1600,
@@ -142,6 +141,16 @@ export class AssetServerPlugin implements VendurePlugin {
         return new Promise(resolve => { this.server.close(() => resolve()); });
     }
 
+    private createAssetStorageStrategy() {
+        const toAbsoluteUrlFn = (request: Request, identifier: string): string => {
+            if (!identifier) {
+                return '';
+            }
+            return `${request.protocol}://${request.get('host')}/${this.options.route}/${identifier}`;
+        };
+        return new LocalAssetStorageStrategy(this.options.assetUploadDir, toAbsoluteUrlFn);
+    }
+
     /**
      * Creates the image server instance
      */

+ 8 - 9
packages/asset-server-plugin/src/local-asset-storage-strategy.ts → packages/core/src/config/asset-storage-strategy/local-asset-storage-strategy.ts

@@ -1,16 +1,22 @@
-import { AssetStorageStrategy } from '@vendure/core';
 import { Request } from 'express';
 import { ReadStream } from 'fs';
 import fs from 'fs-extra';
 import path from 'path';
 import { Stream } from 'stream';
 
+import { AssetStorageStrategy } from './asset-storage-strategy';
+
 /**
  * A persistence strategy which saves files to the local file system.
  */
 export class LocalAssetStorageStrategy implements AssetStorageStrategy {
-    constructor(private readonly uploadPath: string, private readonly route: string) {
+    toAbsoluteUrl: ((reqest: Request, identifier: string) => string) | undefined;
+
+    constructor(private readonly uploadPath: string, private readonly toAbsoluteUrlFn?: (reqest: Request, identifier: string) => string) {
         this.ensureUploadPathExists(this.uploadPath);
+        if (toAbsoluteUrlFn) {
+            this.toAbsoluteUrl = toAbsoluteUrlFn;
+        }
     }
 
     writeFileFromStream(fileName: string, data: ReadStream): Promise<string> {
@@ -46,13 +52,6 @@ export class LocalAssetStorageStrategy implements AssetStorageStrategy {
         return Promise.resolve(readStream);
     }
 
-    toAbsoluteUrl(request: Request, identifier: string): string {
-        if (!identifier) {
-            return '';
-        }
-        return `${request.protocol}://${request.get('host')}/${this.route}/${identifier}`;
-    }
-
     private ensureUploadPathExists(uploadDir: string): void {
         if (!fs.existsSync(this.uploadPath)) {
             fs.mkdirSync(this.uploadPath);

+ 1 - 0
packages/core/src/config/index.ts

@@ -2,6 +2,7 @@ export * from './asset-naming-strategy/asset-naming-strategy';
 export * from './asset-naming-strategy/default-asset-naming-strategy';
 export * from './asset-preview-strategy/asset-preview-strategy';
 export * from './asset-storage-strategy/asset-storage-strategy';
+export * from './asset-storage-strategy/local-asset-storage-strategy';
 export * from './entity-id-strategy/auto-increment-id-strategy';
 export * from './config.service';
 export * from './entity-id-strategy/entity-id-strategy';

+ 1 - 1
packages/dev-server/nodemon-debug.json

@@ -1,5 +1,5 @@
 {
-  "watch": ["index.ts"],
+  "watch": ["index.ts", "dev-config.ts"],
   "ext": "ts",
   "ignore": ["src/**/*.spec.ts", "mock-data/**/*"],
   "exec": "node --inspect=5858 -r ts-node/register index.ts"