Initial files

This commit is contained in:
Samuele Lorefice
2025-12-01 19:32:50 +01:00
commit 39e6694bbe
14 changed files with 583 additions and 0 deletions

23
Encoder/Dockerfile Normal file
View File

@@ -0,0 +1,23 @@
FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS base
USER $APP_UID
WORKDIR /app
EXPOSE 8080
EXPOSE 8081
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["Encoder/Encoder.csproj", "Encoder/"]
RUN dotnet restore "Encoder/Encoder.csproj"
COPY . .
WORKDIR "/src/Encoder"
RUN dotnet build "./Encoder.csproj" -c $BUILD_CONFIGURATION -o /app/build
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./Encoder.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Encoder.dll"]

20
Encoder/Encoder.csproj Normal file
View File

@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.0-rc.1.25451.107"/>
</ItemGroup>
<ItemGroup>
<Content Include="..\.dockerignore">
<Link>.dockerignore</Link>
</Content>
</ItemGroup>
</Project>

6
Encoder/Encoder.http Normal file
View File

@@ -0,0 +1,6 @@
@Encoder_HostAddress = http://localhost:5257
GET {{Encoder_HostAddress}}/weatherforecast/
Accept: application/json
###

94
Encoder/Program.cs Normal file
View File

@@ -0,0 +1,94 @@
using System.Collections;
using Microsoft.AspNetCore.Http.HttpResults;
Queue<EncodingJob> JobQueue = new();
var builder = WebApplication.CreateBuilder(args);
//Settings
string tmpFilePath = builder.Configuration.GetValue<string>("TempFilePath") ?? Path.GetTempPath();
//Services
builder.Services.AddOpenApi();
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());
JobQueue.Enqueue(Job);
return context.Response.WriteAsJsonAsync(new { JobId = Job.Id });
});
// Check the status of an encoding job by its ID
app.MapGet("status/{jobId:guid}", (Guid jobId) =>
{
var job = JobQueue.FirstOrDefault(j => j.Id == jobId);
if (job == null) {
return Results.NotFound(new { Message = "Job not found." });
}
return Results.Ok(new {
JobId = job.Id,
Status = job.Status.ToString(),
CreatedAt = job.CreatedAt,
CompletedAt = job.CompletedAt
});
});
app.MapGet("file/{jobId:guid}", (Guid jobId) =>
{
var job = JobQueue.FirstOrDefault(j => j.Id == 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();
public enum JobStatus {
Pending,
InProgress,
Completed,
Failed
}
public record EncodingJob(Guid Id) {
public JobStatus Status { get; set; } = JobStatus.Pending;
public DateTime CreatedAt { get; init; } = DateTime.Now;
public DateTime? CompletedAt { get; set; } = null;
public string OrigFilePath { get; init; } = string.Empty;
public string EncodedFilePath { get; set; } = string.Empty;
}

View File

@@ -0,0 +1,23 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "http://localhost:5257",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:7134;http://localhost:5257",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Information"
}
}
}

10
Encoder/appsettings.json Normal file
View File

@@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"TemporaryFilesPath": "./Temp"
}