Resolved ffmpeg remaining as a zombie process

This commit is contained in:
Samuele Lorefice
2025-12-15 20:39:38 +01:00
parent aafc3dab99
commit 24df58056c
4 changed files with 66 additions and 46 deletions

View File

@@ -1,6 +1,7 @@
using System.Net.Http.Headers;
using Encoder;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.WebUtilities;
var builder = WebApplication.CreateBuilder(args);
@@ -29,37 +30,8 @@ app.UseHttpsRedirection();
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;
await context.Response.WriteAsync("Invalid content type. Expected multipart/form-data.");
return;
}
var form = request.Form;
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;
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)));
await using (var stream = File.Create(tempFilePath)) file.CopyTo(stream);
// Create and enqueue the encoding job
var job = new EncodingJob(jobGuid, tempFilePath, parsedEncoderType);
// Fail fast, if EncoderService is not available don't bother processing the request
var encSrv = context.RequestServices.GetService<EncoderService>();
if (encSrv == null) {
context.Response.StatusCode = 500;
@@ -67,7 +39,52 @@ app.MapPost("encode", async context => {
return;
}
// Parse multipart form data manually to handle large file uploads
var request = context.Request;
if (!request.HasFormContentType) {
context.Response.StatusCode = 400;
await context.Response.WriteAsync("Invalid content type. Expected multipart/form-data.");
return;
}
Guid jobGuid = Guid.NewGuid();
EncoderType encoderType = EncoderType.H264;
string tempFilePath = Path.Combine(uploadsPath, jobGuid.ToString("D"));
var boundary = Utils.GetBoundary(request.ContentType!);
var reader = new MultipartReader(boundary!, context.Request.Body);
MultipartSection? section;
while ((section = await reader.ReadNextSectionAsync()) != null) {
if(section.ContentDisposition is null) continue;
var contentDisposition = ContentDispositionHeaderValue.Parse(section.ContentDisposition);
switch (contentDisposition.Name) {
case "encoder": {
string encoderTypeStr = await section.ReadAsStringAsync();
if (!Enum.TryParse<EncoderType>(encoderTypeStr, true, out encoderType)) {
context.Response.StatusCode = 400;
await context.Response.WriteAsync("Invalid or missing encoderType parameter.");
return;
}
continue;
}
case "video" when string.IsNullOrWhiteSpace(contentDisposition.FileName): {
context.Response.StatusCode = 400;
await context.Response.WriteAsync("No video file provided.");
return;
}
case "video": {
tempFilePath += Path.GetExtension(contentDisposition.FileName);
await using var fstream = File.Create(tempFilePath);
await section.Body.CopyToAsync(fstream);
continue;
}
}
}
var job = new EncodingJob(jobGuid, tempFilePath, encoderType);
encSrv.EnqueueJob(job);
context.Response.StatusCode = 200;
await context.Response.WriteAsJsonAsync(new { JobId = jobGuid });
}).WithFormOptions(multipartBodyLengthLimit: 1_073_741_824L*64L);