Skip to content

Commit 028b37a

Browse files
committed
Add convert image
1 parent 7afb1bd commit 028b37a

File tree

7 files changed

+379
-22
lines changed

7 files changed

+379
-22
lines changed

AiServer.ServiceInterface/AppExtensions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,12 @@ public static string ComputeSha256(this byte[] data)
8585
byte[] hashBytes = sha256.ComputeHash(data);
8686
return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
8787
}
88+
89+
public static string ComputeSha256(this Stream stream)
90+
{
91+
stream.Position = 0;
92+
using var sha256Hash = SHA256.Create();
93+
byte[] hashBytes = sha256Hash.ComputeHash(stream);
94+
return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
95+
}
8896
}

AiServer.ServiceInterface/MediaTransform/CreateMediaTransformCommand.cs

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

AiServer.ServiceInterface/VideoServices.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,9 @@ public static async Task<ArtifactGenerationResponse> ProcessSyncTransformAsync(t
376376

377377
// Wait for the job to complete max 1 minute
378378
var timeout = DateTime.UtcNow.AddMinutes(1);
379-
while (queuedJob?.Job?.State is not (BackgroundJobState.Completed or BackgroundJobState.Cancelled
380-
or BackgroundJobState.Failed) && DateTime.UtcNow < timeout)
379+
while (queuedJob?.Job?.State is not (
380+
BackgroundJobState.Completed or BackgroundJobState.Cancelled or BackgroundJobState.Failed)
381+
&& DateTime.UtcNow < timeout)
381382
{
382383
await Task.Delay(1000);
383384
queuedJob = jobs.GetJob(transformResponse.Id);

0 commit comments

Comments
 (0)