Encoding added Requested Encoding to the request, made request async, some refactoring
This commit is contained in:
@@ -9,7 +9,7 @@ builder.Services.AddOpenApi();
|
||||
builder.Services.AddLogging();
|
||||
|
||||
string uploadsPath = builder.Configuration.GetSection("UploadsPath").Get<string>() ?? "./Uploads";
|
||||
if(!Path.IsPathRooted(uploadsPath)) uploadsPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Environment.ProcessPath), uploadsPath));
|
||||
uploadsPath = uploadsPath.AbsoluteOrProcessPath();
|
||||
Directory.CreateDirectory(uploadsPath); // Ensure the uploads directory exists
|
||||
|
||||
builder.Services.Configure<FFmpegOptions>(builder.Configuration.GetSection(FFmpegOptions.SectionName));
|
||||
@@ -26,42 +26,51 @@ app.UseHttpsRedirection();
|
||||
// Get a video file as multipart form data and schedule it for encoding
|
||||
// Get video encoding settings from query parameters
|
||||
// Returns the ID of the job handling the encoding
|
||||
app.MapPost("encode", context =>
|
||||
{
|
||||
app.MapPost("encode", async context => {
|
||||
// Disable request size limit
|
||||
context.Features.Get<IHttpMaxRequestBodySizeFeature>()?.MaxRequestBodySize = null;
|
||||
|
||||
var request = context.Request;
|
||||
if (!request.HasFormContentType) {
|
||||
context.Response.StatusCode = 400;
|
||||
return context.Response.WriteAsync("Invalid content type. Expected multipart/form-data.");
|
||||
await context.Response.WriteAsync("Invalid content type. Expected multipart/form-data.");
|
||||
return;
|
||||
}
|
||||
|
||||
var form = request.Form;
|
||||
var file = form.Files.GetFile("video"); // Contrary to what it seems, the "name" here is the form field name, not the file name
|
||||
|
||||
var encoderType = form["encoder"].FirstOrDefault();
|
||||
if(encoderType == null || !Enum.TryParse<EncoderType>(encoderType, true, out var parsedEncoderType)) {
|
||||
context.Response.StatusCode = 400;
|
||||
await context.Response.WriteAsync("Invalid or missing encoderType parameter.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Contrary to what it seems, the "name" here is the form field name, not the file name
|
||||
var file = form.Files.GetFile("video");
|
||||
if (file == null) {
|
||||
context.Response.StatusCode = 400;
|
||||
return context.Response.WriteAsync("No video file provided.");
|
||||
await context.Response.WriteAsync("No video file provided.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Save the file to a temporary location
|
||||
var jobGuid = Guid.NewGuid();
|
||||
var tempFilePath = Path.GetFullPath(Path.Combine(uploadsPath, jobGuid.ToString("D")+Path.GetExtension(file.FileName)));
|
||||
using (var stream = File.Create(tempFilePath))
|
||||
file.CopyTo(stream);
|
||||
await using (var stream = File.Create(tempFilePath)) file.CopyTo(stream);
|
||||
|
||||
var job = new EncodingJob(jobGuid, tempFilePath);
|
||||
// Create and enqueue the encoding job
|
||||
var job = new EncodingJob(jobGuid, tempFilePath, parsedEncoderType);
|
||||
var encSrv = context.RequestServices.GetService<EncoderService>();
|
||||
if (encSrv != null) encSrv.EnqueueJob(job);
|
||||
else {
|
||||
if (encSrv == null) {
|
||||
context.Response.StatusCode = 500;
|
||||
return context.Response.WriteAsync("Encoder service not available.");
|
||||
await context.Response.WriteAsync("Encoder service not available.");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
encSrv.EnqueueJob(job);
|
||||
context.Response.StatusCode = 200;
|
||||
return context.Response.WriteAsJsonAsync(new { JobId = jobGuid });
|
||||
}).WithFormOptions(multipartBodyLengthLimit: 1024L*1024L*1024L*64L);
|
||||
await context.Response.WriteAsJsonAsync(new { JobId = jobGuid });
|
||||
}).WithFormOptions(multipartBodyLengthLimit: 1_073_741_824L*64L);
|
||||
|
||||
app.MapGet("status", (context) => {
|
||||
var encSrv = context.RequestServices.GetService<EncoderService>();
|
||||
@@ -77,8 +86,7 @@ app.MapGet("status", (context) => {
|
||||
});
|
||||
|
||||
// Check the status of an encoding job by its ID
|
||||
app.MapGet("status/{jobId:guid}", (HttpContext context, Guid jobId) =>
|
||||
{
|
||||
app.MapGet("status/{jobId:guid}", (HttpContext context, Guid jobId) => {
|
||||
var encSrv = context.RequestServices.GetService<EncoderService>();
|
||||
if (encSrv == null) return Results.InternalServerError();
|
||||
|
||||
@@ -95,8 +103,7 @@ app.MapGet("status/{jobId:guid}", (HttpContext context, Guid jobId) =>
|
||||
});
|
||||
});
|
||||
|
||||
app.MapGet("file/{jobId:guid}", (HttpContext context, Guid jobId) =>
|
||||
{
|
||||
app.MapGet("file/{jobId:guid}", (HttpContext context, Guid jobId) => {
|
||||
var encSrv = context.RequestServices.GetService<EncoderService>();
|
||||
if (encSrv == null) return Results.InternalServerError();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user