49 lines
1.7 KiB
C#
49 lines
1.7 KiB
C#
using Fragalytics.Web;
|
|
using Fragalytics.Web.Models;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace Fragalytics.RenderWorker.Services;
|
|
|
|
public sealed class SampleCopyRenderBackend : IClipRenderBackend
|
|
{
|
|
private readonly FragalyticsOptions _options;
|
|
|
|
public SampleCopyRenderBackend(IOptions<FragalyticsOptions> options)
|
|
{
|
|
_options = options.Value;
|
|
}
|
|
|
|
public string Name => RenderBackendNames.SampleCopy;
|
|
|
|
public async Task<RenderedClipResult> RenderAsync(ClipJobRecord clipJob, CancellationToken cancellationToken)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(_options.RenderWorker.SampleClipPath))
|
|
{
|
|
throw new InvalidOperationException("Fragalytics:RenderWorker:SampleClipPath is not configured.");
|
|
}
|
|
|
|
var sampleClipPath = Path.GetFullPath(_options.RenderWorker.SampleClipPath);
|
|
if (!File.Exists(sampleClipPath))
|
|
{
|
|
throw new FileNotFoundException("Configured sample clip was not found.", sampleClipPath);
|
|
}
|
|
|
|
var tempDirectory = Path.GetFullPath(_options.RenderWorker.TempDirectory);
|
|
Directory.CreateDirectory(tempDirectory);
|
|
|
|
var extension = Path.GetExtension(sampleClipPath);
|
|
var outputPath = Path.Combine(tempDirectory, $"clipjob_{clipJob.Id}_{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}{extension}");
|
|
|
|
await using var input = File.OpenRead(sampleClipPath);
|
|
await using var output = File.Create(outputPath);
|
|
await input.CopyToAsync(output, cancellationToken);
|
|
|
|
return new RenderedClipResult
|
|
{
|
|
FilePath = outputPath,
|
|
FileName = Path.GetFileName(sampleClipPath),
|
|
ContentType = ClipMediaFormats.InferContentType(extension)
|
|
};
|
|
}
|
|
}
|