Made the service basically start working
This commit is contained in:
@@ -1,15 +1,21 @@
|
||||
using Encoder;
|
||||
using Microsoft.AspNetCore.Http.Features;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
//Settings
|
||||
string? tmpFilePath = builder.Configuration.GetValue<string>("TempFilePath");
|
||||
string? ffmpegPath = builder.Configuration.GetValue<string>("FfmpegPath");
|
||||
//Services
|
||||
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));
|
||||
Directory.CreateDirectory(uploadsPath); // Ensure the uploads directory exists
|
||||
|
||||
builder.Services.Configure<FFmpegOptions>(builder.Configuration.GetSection(FFmpegOptions.SectionName));
|
||||
builder.Services.AddSingleton<EncoderService>();
|
||||
builder.Services.AddHostedService<EncoderService>(p => p.GetRequiredService<EncoderService>());
|
||||
|
||||
var encoderOptions = new EncoderServiceOptions { OutputPath = tmpFilePath };
|
||||
builder.Services.AddSingleton<IEncoderService, EncoderService>(_ => new (encoderOptions));
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
@@ -20,8 +26,11 @@ 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", context =>
|
||||
{
|
||||
// Disable request size limit
|
||||
context.Features.Get<IHttpMaxRequestBodySizeFeature>()?.MaxRequestBodySize = null;
|
||||
|
||||
var request = context.Request;
|
||||
if (!request.HasFormContentType) {
|
||||
context.Response.StatusCode = 400;
|
||||
@@ -29,14 +38,20 @@ app.MapPost("encode", context =>
|
||||
}
|
||||
|
||||
var form = request.Form;
|
||||
var file = form.Files.GetFile("video");
|
||||
|
||||
var file = form.Files.GetFile("video"); // Contrary to what it seems, the "name" here is the form field name, not the file name
|
||||
|
||||
if (file == null) {
|
||||
context.Response.StatusCode = 400;
|
||||
return context.Response.WriteAsync("No video file provided.");
|
||||
}
|
||||
|
||||
var job = new EncodingJob(Guid.NewGuid());
|
||||
// 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);
|
||||
|
||||
var job = new EncodingJob(jobGuid, tempFilePath);
|
||||
var encSrv = context.RequestServices.GetService<EncoderService>();
|
||||
if (encSrv != null) encSrv.EnqueueJob(job);
|
||||
else {
|
||||
@@ -45,7 +60,20 @@ app.MapPost("encode", context =>
|
||||
}
|
||||
|
||||
context.Response.StatusCode = 200;
|
||||
return context.Response.WriteAsJsonAsync(new { JobId = job.Id });
|
||||
return context.Response.WriteAsJsonAsync(new { JobId = jobGuid });
|
||||
}).WithFormOptions(multipartBodyLengthLimit: 1024L*1024L*1024L*64L);
|
||||
|
||||
app.MapGet("status", (context) => {
|
||||
var encSrv = context.RequestServices.GetService<EncoderService>();
|
||||
if (encSrv == null) {
|
||||
context.Response.StatusCode = 500;
|
||||
return context.Response.WriteAsync("Encoder service not available.");
|
||||
}
|
||||
|
||||
var jobs = encSrv.GetJobs().ToArray();
|
||||
|
||||
context.Response.StatusCode = 200;
|
||||
return context.Response.WriteAsJsonAsync(jobs);
|
||||
});
|
||||
|
||||
// Check the status of an encoding job by its ID
|
||||
@@ -84,4 +112,6 @@ app.MapGet("file/{jobId:guid}", (HttpContext context, Guid jobId) =>
|
||||
return Results.File(fileBytes, "video/mp4", Path.GetFileName(filePath), enableRangeProcessing:true);
|
||||
});
|
||||
|
||||
app.MapOpenApi("openapi.json");
|
||||
|
||||
app.Run();
|
||||
Reference in New Issue
Block a user