Browse Source

feat(server) Add filetype variable to storage template (#1337)

* feat(server) Add filetype variable to storage template

* Remove console.log

* Added additional variable for full file type
Alex 2 years ago
parent
commit
0c582df962

+ 10 - 3
server/libs/storage/src/storage.service.ts

@@ -1,5 +1,5 @@
 import { APP_UPLOAD_LOCATION } from '@app/common';
-import { AssetEntity, SystemConfig } from '@app/infra';
+import { AssetEntity, AssetType, SystemConfig } from '@app/infra';
 import { ImmichConfigService, INITIAL_SYSTEM_CONFIG } from '@app/immich-config';
 import { Inject, Injectable, Logger } from '@nestjs/common';
 import { InjectRepository } from '@nestjs/typeorm';
@@ -128,19 +128,19 @@ export class StorageService {
   private validateStorageTemplate(templateString: string) {
     try {
       const template = this.compile(templateString);
-
       // test render an asset
       this.render(
         template,
         {
           createdAt: new Date().toISOString(),
           originalPath: '/upload/test/IMG_123.jpg',
+          type: AssetType.IMAGE,
         } as AssetEntity,
         'IMG_123',
         'jpg',
       );
     } catch (e) {
-      this.logger.warn(`Storage template validation failed: ${e}`);
+      this.logger.warn(`Storage template validation failed: ${JSON.stringify(e)}`);
       throw new Error(`Invalid storage template: ${e}`);
     }
   }
@@ -158,6 +158,9 @@ export class StorageService {
       ext,
     };
 
+    const fileType = asset.type == AssetType.IMAGE ? 'IMG' : 'VID';
+    const fileTypeFull = asset.type == AssetType.IMAGE ? 'IMAGE' : 'VIDEO';
+
     const dt = luxon.DateTime.fromISO(new Date(asset.createdAt).toISOString());
 
     const dateTokens = [
@@ -173,6 +176,10 @@ export class StorageService {
       substitutions[token] = dt.toFormat(token);
     }
 
+    // Support file type token
+    substitutions.filetype = fileType;
+    substitutions.filetypefull = fileTypeFull;
+
     return template(substitutions);
   }
 

+ 4 - 2
web/src/lib/components/admin-page/settings/storate-template/storage-template-settings.svelte

@@ -56,8 +56,10 @@
 		});
 
 		const substitutions: Record<string, string> = {
-			filename: 'IMG_10041123',
-			ext: 'jpeg'
+			filename: 'IMAGE_56437',
+			ext: 'jpeg',
+			filetype: 'IMG',
+			filetypefull: 'IMAGE'
 		};
 
 		const dt = luxon.DateTime.fromISO(new Date('2022-09-04T20:03:05.250').toISOString());

+ 8 - 0
web/src/lib/components/admin-page/settings/storate-template/supported-variables-panel.svelte

@@ -17,5 +17,13 @@
 				<li>{`{{ext}}`}</li>
 			</ul>
 		</div>
+
+		<div>
+			<p class="text-immich-primary font-medium dark:text-immich-dark-primary">FILE TYPE</p>
+			<ul>
+				<li>{`{{filetype}}`} - VID or IMG</li>
+				<li>{`{{filetypefull}}`} - VIDEO or IMAGE</li>
+			</ul>
+		</div>
 	</div>
 </div>