Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Nov 23, 2024
2 parents bdb0a0a + e7df42c commit 04fb0cb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions AiServer.ServiceInterface/Generation/ComfyProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,13 @@ public async Task<TransformResult> TransformMediaAsync(MediaProvider provider, M
{
var scaleWidth = args.ScaleWidth ?? -1;
var scaleHeight = args.ScaleHeight ?? -1;
filterComplex.Add($"scale={scaleWidth}:{scaleHeight}");
filterComplex.Add($"[0:v]scale={scaleWidth}:{scaleHeight}");
}

// Handle cropping
if (args is { CropX: not null, CropY: not null, CropWidth: not null, CropHeight: not null })
{
filterComplex.Add($"crop={args.CropWidth}:{args.CropHeight}:{args.CropX}:{args.CropY}");
filterComplex.Add($"[0:v]crop={args.CropWidth}:{args.CropHeight}:{args.CropX}:{args.CropY}");
}

if (args is { WatermarkInput: not null, WatermarkPosition: not null })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected override async Task<TransformResult> RunAsync(CreateMediaTransform req
log.LogInformation("Finished {TaskType} generation request {RefId} with {Provider} in {DurationMs}ms",
request.Request.TaskType, request.RefId, apiProviderInstance.Name, durationMs);
if (response.Outputs != null && response.Outputs.Count > 0)
await DownloadOutputsAsync(transformProvider, apiProviderInstance, response.Outputs, keyId);
await DownloadOutputsAsync(transformProvider, apiProviderInstance, response.Outputs, keyId, token);

ResponseStatus? error = null;
var outputs = new List<TransformFileOutput>();
Expand Down Expand Up @@ -308,10 +308,10 @@ private IImageFormat GetImageFormat(ImageOutputFormat format)

async Task DownloadOutputsAsync(IMediaTransformProvider aiProvider,
MediaProvider apiProvider,
IEnumerable<TransformFileOutput> outputs, string keyId)
IEnumerable<TransformFileOutput> outputs, string keyId, CancellationToken token = default)
{
var now = DateTime.UtcNow;
var downloadTasks = outputs.Select(x => DownloadOutputAsync(aiProvider, apiProvider, x, now, keyId));
var downloadTasks = outputs.Select(x => DownloadOutputAsync(aiProvider, apiProvider, x, now, keyId, token));
await Task.WhenAll(downloadTasks);
}

Expand All @@ -331,12 +331,12 @@ async Task SaveTransformResultAsync(TransformResult response, Stream data, strin

async Task DownloadOutputAsync(IMediaTransformProvider aiProvider,
MediaProvider apiProvider,
TransformFileOutput output, DateTime now, string keyId)
TransformFileOutput output, DateTime now, string keyId, CancellationToken token = default)
{
using var client = httpFactory.CreateClient();
try
{
var response = await aiProvider.DownloadOutputAsync(apiProvider, output);
var response = await aiProvider.DownloadOutputAsync(apiProvider, output, token);
response.EnsureSuccessStatusCode();

// Grab file extension from response
Expand All @@ -360,13 +360,13 @@ async Task DownloadOutputAsync(IMediaTransformProvider aiProvider,
_ => "mp4"
};

var imageBytes = await response.Content.ReadAsByteArrayAsync();
var imageBytes = await response.Content.ReadAsByteArrayAsync(token);
var sha256 = imageBytes.ComputeSha256();
output.FileName = $"{sha256}.{ext}";
var relativePath = $"{now:yyyy}/{now:MM}/{now:dd}/{keyId}/{output.FileName}";
var path = appConfig.ArtifactsPath.CombineWith(relativePath);
Path.GetDirectoryName(path).AssertDir();
await File.WriteAllBytesAsync(path, imageBytes);
await File.WriteAllBytesAsync(path, imageBytes, token);
output.Url = $"/artifacts/{relativePath}";
}
catch (Exception e)
Expand Down

0 comments on commit 04fb0cb

Please sign in to comment.