@@ -135,18 +135,20 @@ private async Task<TransformResult> ConvertImageAsync(MediaTransformArgs args, s
135135 using var inputImage = await Image . LoadAsync ( inputFile , token ) ;
136136
137137 var format = ( ImageOutputFormat ) args . ImageOutputFormat ;
138+ var ext = format . ToString ( ) . ToLower ( ) ;
138139 var outputFormat = GetImageFormat ( format ) ;
139140 var outputStream = new MemoryStream ( ) ;
140141 await inputImage . SaveAsync ( outputStream , outputFormat , cancellationToken : token ) ;
142+
143+ var name = args . ImageFileName ? . LastLeftPart ( '.' ) ?? outputStream . ComputeSha256 ( ) ;
144+ var fileName = $ "{ name } .{ ext } ";
145+
141146 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 } "
150152 }
151153 ]
152154 } ;
@@ -384,6 +386,40 @@ private async Task HandleFileUploadsAsync(BackgroundJob job, MediaTransformArgs
384386 if ( fileUploads == null )
385387 return ;
386388
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+
387423 foreach ( var file in fileUploads )
388424 {
389425 if ( ! supportedFiles . Contains ( file . Key ) )
@@ -394,23 +430,24 @@ private async Task HandleFileUploadsAsync(BackgroundJob job, MediaTransformArgs
394430 await fs . CopyToAsync ( ms , token ) ;
395431 ms . Position = 0 ;
396432 fs . Close ( ) ;
433+
397434 switch ( file . Key )
398435 {
399436 case "image" :
400437 argInstance . ImageInput = ms ;
401- argInstance . ImageFileName = Path . GetFileName ( file . Value ) ;
438+ argInstance . ImageFileName = GetFileName ( file , ToSet ( [ nameof ( MediaTransformArgs . ImageFileName ) ] ) ) ;
402439 break ;
403440 case "video" :
404441 argInstance . VideoInput = ms ;
405- argInstance . VideoFileName = Path . GetFileName ( file . Value ) ;
442+ argInstance . VideoFileName = GetFileName ( file , ToSet ( [ nameof ( MediaTransformArgs . VideoFileName ) ] ) ) ;
406443 break ;
407444 case "watermark" :
408445 argInstance . WatermarkInput = ms ;
409- argInstance . WatermarkFileName = Path . GetFileName ( file . Value ) ;
446+ argInstance . WatermarkFileName = GetFileName ( file , ToSet ( [ nameof ( MediaTransformArgs . WatermarkFileName ) ] ) ) ;
410447 break ;
411448 case "audio" :
412449 argInstance . AudioInput = ms ;
413- argInstance . AudioFileName = Path . GetFileName ( file . Value ) ;
450+ argInstance . AudioFileName = GetFileName ( file , ToSet ( [ nameof ( MediaTransformArgs . AudioFileName ) ] ) ) ;
414451 break ;
415452 }
416453 }
0 commit comments