124 lines
4.5 KiB
C#
124 lines
4.5 KiB
C#
using Encoder;
|
|
using Microsoft.AspNetCore.Http.Features;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
//Services
|
|
builder.Services.AddOpenApi();
|
|
builder.Services.AddLogging();
|
|
|
|
string uploadsPath = builder.Configuration.GetSection("UploadsPath").Get<string>() ?? "./Uploads";
|
|
uploadsPath = uploadsPath.AbsoluteOrProcessPath();
|
|
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 app = builder.Build();
|
|
|
|
if (app.Environment.IsDevelopment()) { app.MapOpenApi(); }
|
|
|
|
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", 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);
|
|
var encSrv = context.RequestServices.GetService<EncoderService>();
|
|
if (encSrv == null) {
|
|
context.Response.StatusCode = 500;
|
|
await context.Response.WriteAsync("Encoder service not available.");
|
|
return;
|
|
}
|
|
|
|
encSrv.EnqueueJob(job);
|
|
context.Response.StatusCode = 200;
|
|
await context.Response.WriteAsJsonAsync(new { JobId = jobGuid });
|
|
}).WithFormOptions(multipartBodyLengthLimit: 1_073_741_824L*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
|
|
app.MapGet("status/{jobId:guid}", (HttpContext context, Guid jobId) => {
|
|
var encSrv = context.RequestServices.GetService<EncoderService>();
|
|
if (encSrv == null) return Results.InternalServerError();
|
|
|
|
var job = encSrv.GetJobStatus(jobId);
|
|
if (job == null) {
|
|
return Results.NotFound(new { Message = "Job not found." });
|
|
}
|
|
|
|
return Results.Ok(new {
|
|
JobId = job.Id,
|
|
Status = job.Status.ToString(),
|
|
job.CreatedAt,
|
|
job.CompletedAt
|
|
});
|
|
});
|
|
|
|
app.MapGet("file/{jobId:guid}", (HttpContext context, Guid jobId) => {
|
|
var encSrv = context.RequestServices.GetService<EncoderService>();
|
|
if (encSrv == null) return Results.InternalServerError();
|
|
|
|
var job = encSrv.GetJobStatus(jobId);
|
|
if (job == null) return Results.NotFound(new { Message = "Job not found." });
|
|
|
|
if (job.Status != JobStatus.Completed) return Results.BadRequest(new { Message = "Job is not completed yet." });
|
|
|
|
var filePath = job.EncodedFilePath;
|
|
if (!File.Exists(filePath)) return Results.NotFound(new { Message = "Encoded file not found." });
|
|
|
|
var fileBytes = File.ReadAllBytes(filePath);
|
|
return Results.File(fileBytes, "video/mp4", Path.GetFileName(filePath), enableRangeProcessing:true);
|
|
});
|
|
|
|
app.MapOpenApi("openapi.json");
|
|
|
|
app.Run(); |