@@ -135,18 +135,20 @@ private async Task<TransformResult> ConvertImageAsync(MediaTransformArgs args, s
135
135
using var inputImage = await Image . LoadAsync ( inputFile , token ) ;
136
136
137
137
var format = ( ImageOutputFormat ) args . ImageOutputFormat ;
138
+ var ext = format . ToString ( ) . ToLower ( ) ;
138
139
var outputFormat = GetImageFormat ( format ) ;
139
140
var outputStream = new MemoryStream ( ) ;
140
141
await inputImage . SaveAsync ( outputStream , outputFormat , cancellationToken : token ) ;
142
+
143
+ var name = args . ImageFileName ? . LastLeftPart ( '.' ) ?? outputStream . ComputeSha256 ( ) ;
144
+ var fileName = $ "{ name } .{ ext } ";
145
+
141
146
outputStream . Position = 0 ;
142
- var response = new TransformResult
143
- {
144
- Outputs =
145
- [
146
- new ( )
147
- {
148
- FileName = $ "{ keyId } .png",
149
- Url = $ "/artifacts/{ keyId } .png"
147
+ var response = new TransformResult {
148
+ Outputs = [
149
+ new ( ) {
150
+ FileName = fileName ,
151
+ Url = $ "/artifacts/{ fileName } "
150
152
}
151
153
]
152
154
} ;
@@ -384,6 +386,40 @@ private async Task HandleFileUploadsAsync(BackgroundJob job, MediaTransformArgs
384
386
if ( fileUploads == null )
385
387
return ;
386
388
389
+ static HashSet < string > ToSet ( string [ ] keys ) => new ( keys , StringComparer . OrdinalIgnoreCase ) ;
390
+
391
+ static string ? GetStringValue ( Dictionary < string , object > ? obj , HashSet < string > keys )
392
+ {
393
+ if ( obj != null )
394
+ {
395
+ foreach ( var entry in obj )
396
+ {
397
+ if ( entry . Value is Dictionary < string , object > childObj )
398
+ return GetStringValue ( childObj , keys ) ;
399
+ if ( entry . Value is string value )
400
+ {
401
+ if ( keys . Contains ( entry . Key ) )
402
+ return value ;
403
+ }
404
+ }
405
+ }
406
+ return null ;
407
+ }
408
+
409
+ string GetFileName ( KeyValuePair < string , string > file , HashSet < string > keys )
410
+ {
411
+ try
412
+ {
413
+ var obj = JSON . parse ( job . RequestBody ) ;
414
+ return GetStringValue ( obj as Dictionary < string , object > , keys )
415
+ ?? Path . GetFileName ( file . Value ) ;
416
+ }
417
+ catch ( Exception e )
418
+ {
419
+ return Path . GetFileName ( file . Value ) ;
420
+ }
421
+ }
422
+
387
423
foreach ( var file in fileUploads )
388
424
{
389
425
if ( ! supportedFiles . Contains ( file . Key ) )
@@ -394,23 +430,24 @@ private async Task HandleFileUploadsAsync(BackgroundJob job, MediaTransformArgs
394
430
await fs . CopyToAsync ( ms , token ) ;
395
431
ms . Position = 0 ;
396
432
fs . Close ( ) ;
433
+
397
434
switch ( file . Key )
398
435
{
399
436
case "image" :
400
437
argInstance . ImageInput = ms ;
401
- argInstance . ImageFileName = Path . GetFileName ( file . Value ) ;
438
+ argInstance . ImageFileName = GetFileName ( file , ToSet ( [ nameof ( MediaTransformArgs . ImageFileName ) ] ) ) ;
402
439
break ;
403
440
case "video" :
404
441
argInstance . VideoInput = ms ;
405
- argInstance . VideoFileName = Path . GetFileName ( file . Value ) ;
442
+ argInstance . VideoFileName = GetFileName ( file , ToSet ( [ nameof ( MediaTransformArgs . VideoFileName ) ] ) ) ;
406
443
break ;
407
444
case "watermark" :
408
445
argInstance . WatermarkInput = ms ;
409
- argInstance . WatermarkFileName = Path . GetFileName ( file . Value ) ;
446
+ argInstance . WatermarkFileName = GetFileName ( file , ToSet ( [ nameof ( MediaTransformArgs . WatermarkFileName ) ] ) ) ;
410
447
break ;
411
448
case "audio" :
412
449
argInstance . AudioInput = ms ;
413
- argInstance . AudioFileName = Path . GetFileName ( file . Value ) ;
450
+ argInstance . AudioFileName = GetFileName ( file , ToSet ( [ nameof ( MediaTransformArgs . AudioFileName ) ] ) ) ;
414
451
break ;
415
452
}
416
453
}
0 commit comments