AobaService

use transactions
Added gRPC
This commit is contained in:
2025-04-30 23:26:24 -04:00
parent 4f976fb7af
commit 2a6ef8ca20
10 changed files with 128 additions and 59 deletions

View File

@@ -9,21 +9,29 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Grpc.AspNetCore" Version="2.71.0" />
<PackageReference Include="Grpc.Tools" Version="2.71.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Isopoh.Cryptography.Argon2" Version="2.0.0" />
<PackageReference Include="MaybeError" Version="1.0.6" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.4" />
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.8.0" />
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.9.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.2" />
<PackageReference Include="MimeTypesMap" Version="1.0.9" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.12.0" />
<PackageReference Include="OpenTelemetry.Exporter.Prometheus.AspNetCore" Version="1.9.0-beta.2" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.12.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.11.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.11.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AobaCore\AobaCore.csproj" />
</ItemGroup>
<ItemGroup>
<Protobuf Include="Proto\Aoba.proto"></Protobuf>
</ItemGroup>
</Project>

View File

@@ -10,20 +10,24 @@ using MongoDB.Driver;
namespace AobaServer.Controllers;
[Route("/m")]
public class MediaController(MediaService mediaService, AobaService aobaService, ILogger<MediaController> logger) : Controller
public class MediaController(AobaService aobaService, ILogger<MediaController> logger) : Controller
{
[HttpGet("{id}")]
[ResponseCache(Duration = int.MaxValue)]
public async Task<IActionResult> MediaAsync(ObjectId id)
public async Task<IActionResult> MediaAsync(ObjectId id, [FromServices] MongoClient client, CancellationToken cancellationToken)
{
var file = await mediaService.GetMediaStreamAsync(id);
using var session = await client.StartSessionAsync(cancellationToken: cancellationToken);
session.StartTransaction();
var file = await aobaService.GetFileStreamAsync(id, cancellationToken: cancellationToken);
if (file.HasError)
{
await session.AbortTransactionAsync(cancellationToken: cancellationToken);
logger.LogError(file.Error.Exception, "Failed to load media stream");
return NotFound();
}
await session.CommitTransactionAsync(cancellationToken: cancellationToken);
var mime = MimeTypesMap.GetMimeType(file.Value.FileInfo.Filename);
_ = aobaService.IncrementFileViewCountAsync(id);
_ = aobaService.IncrementFileViewCountAsync(id, cancellationToken);
return File(file, mime, true);
}
@@ -35,9 +39,9 @@ public class MediaController(MediaService mediaService, AobaService aobaService,
/// <param name="aoba"></param>
/// <returns></returns>
[HttpGet("/i/{id}/{*rest}")]
public async Task<IActionResult> LegacyRedirectAsync(ObjectId id, string rest, [FromServices] AobaService aoba)
public async Task<IActionResult> LegacyRedirectAsync(ObjectId id, string rest, CancellationToken cancellationToken)
{
var media = await aoba.GetMediaAsync(id);
var media = await aobaService.GetMediaAsync(id, cancellationToken);
if (media == null)
return NotFound();
return LocalRedirectPermanent($"/m/{media.MediaId}/{rest}");

View File

@@ -4,6 +4,7 @@ using AobaServer;
using AobaServer.Auth;
using AobaServer.Middleware;
using AobaServer.Models;
using AobaServer.Services;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
@@ -16,6 +17,7 @@ var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers(opt => opt.ModelBinderProviders.Add(new BsonIdModelBinderProvider()));
builder.Services.AddObersability(builder.Configuration);
builder.Services.AddGrpc();
var authInfo = AuthInfo.LoadOrCreate("Auth.json", "aobaV2", "aoba");
builder.Services.AddSingleton(authInfo);
@@ -92,6 +94,9 @@ app.UseAuthorization();
app.MapControllers();
app.MapObserability();
app.MapGrpcService<AobaRpcService>();
app.Run();

View File

@@ -0,0 +1,30 @@
syntax = "proto3";
service AobaRPC {
rpc GetMedia (Id) returns (MediaModel);
}
message Id{
string idString = 1;
}
message MediaModel {
int32 version = 1;
Id id = 2;
string mediaId = 3;
string fileName = 4;
MediaType mediaType = 5;
string ext = 6;
int32 viewCount = 7;
Id owner = 8;
}
enum MediaType{
Image = 0;
Audio = 1;
Video = 2;
Text = 3;
Code = 4;
Raw = 5;
}

View File

@@ -0,0 +1,13 @@
using AobaCore;
using Grpc.Core;
namespace AobaServer.Services;
public class AobaRpcService(AobaService aobaService) : AobaRPC.AobaRPCBase
{
public override Task<MediaModel> GetMedia(Id request, ServerCallContext context)
{
return base.GetMedia(request, context);
}
}