Browse Source

feat(asset-server-plugin): Allow url prefix to be set in options

Michael Bromley 6 years ago
parent
commit
c0ea09287c
1 changed files with 10 additions and 1 deletions
  1. 10 1
      packages/asset-server-plugin/src/plugin.ts

+ 10 - 1
packages/asset-server-plugin/src/plugin.ts

@@ -67,6 +67,15 @@ export interface AssetServerOptions {
      * The local directory to which assets will be uploaded.
      */
     assetUploadDir: string;
+    /**
+     * @description
+     * The complete URL prefix of the asset files. For example, "https://demo.vendure.io/assets/"
+     *
+     * If not provided, the plugin will attempt to guess based off the incoming
+     * request and the configured route. However, in all but the simplest cases,
+     * this guess may not yield correct results.
+     */
+    assetUrlPrefix?: 'string';
     /**
      * @description
      * The max width in pixels of a generated preview image.
@@ -221,10 +230,10 @@ export class AssetServerPlugin implements VendurePlugin {
 
     private createAssetStorageStrategy() {
         const toAbsoluteUrlFn = (request: Request, identifier: string): string => {
-            const prefix = `${request.protocol}://${request.get('host')}/${this.options.route}/`;
             if (!identifier) {
                 return '';
             }
+            const prefix = this.options.assetUrlPrefix || `${request.protocol}://${request.get('host')}/${this.options.route}/`;
             return identifier.startsWith(prefix) ? identifier : `${prefix}${identifier}`;
         };
         return new LocalAssetStorageStrategy(this.options.assetUploadDir, toAbsoluteUrlFn);