87 lines
2.9 KiB
C#
87 lines
2.9 KiB
C#
using Encoder;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
//Settings
|
|
string? tmpFilePath = builder.Configuration.GetValue<string>("TempFilePath");
|
|
string? ffmpegPath = builder.Configuration.GetValue<string>("FfmpegPath");
|
|
//Services
|
|
builder.Services.AddOpenApi();
|
|
|
|
var encoderOptions = new EncoderServiceOptions { OutputPath = tmpFilePath };
|
|
builder.Services.AddSingleton<IEncoderService, EncoderService>(_ => new (encoderOptions));
|
|
|
|
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", context =>
|
|
{
|
|
var request = context.Request;
|
|
if (!request.HasFormContentType) {
|
|
context.Response.StatusCode = 400;
|
|
return context.Response.WriteAsync("Invalid content type. Expected multipart/form-data.");
|
|
}
|
|
|
|
var form = request.Form;
|
|
var file = form.Files.GetFile("video");
|
|
|
|
if (file == null) {
|
|
context.Response.StatusCode = 400;
|
|
return context.Response.WriteAsync("No video file provided.");
|
|
}
|
|
|
|
var job = new EncodingJob(Guid.NewGuid());
|
|
var encSrv = context.RequestServices.GetService<EncoderService>();
|
|
if (encSrv != null) encSrv.EnqueueJob(job);
|
|
else {
|
|
context.Response.StatusCode = 500;
|
|
return context.Response.WriteAsync("Encoder service not available.");
|
|
}
|
|
|
|
context.Response.StatusCode = 200;
|
|
return context.Response.WriteAsJsonAsync(new { JobId = job.Id });
|
|
});
|
|
|
|
// 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.Run(); |