implement media serving
This commit is contained in:
@@ -8,7 +8,6 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.3" />
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.3" />
|
||||||
<PackageReference Include="MongoDB.Bson" Version="3.3.0" />
|
|
||||||
<PackageReference Include="MaybeError" Version="1.0.5" />
|
<PackageReference Include="MaybeError" Version="1.0.5" />
|
||||||
<PackageReference Include="MongoDB.Analyzer" Version="1.5.0" />
|
<PackageReference Include="MongoDB.Analyzer" Version="1.5.0" />
|
||||||
<PackageReference Include="MongoDB.Driver" Version="2.30.0" />
|
<PackageReference Include="MongoDB.Driver" Version="2.30.0" />
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ public class AobaService(IMongoDatabase db)
|
|||||||
{
|
{
|
||||||
private readonly IMongoCollection<Media> _media = db.GetCollection<Media>("media");
|
private readonly IMongoCollection<Media> _media = db.GetCollection<Media>("media");
|
||||||
|
|
||||||
|
|
||||||
public async Task<Media?> GetMediaAsync(ObjectId id)
|
public async Task<Media?> GetMediaAsync(ObjectId id)
|
||||||
{
|
{
|
||||||
return await _media.Find(m => m.Id == id).FirstOrDefaultAsync();
|
return await _media.Find(m => m.Id == id).FirstOrDefaultAsync();
|
||||||
@@ -19,4 +18,9 @@ public class AobaService(IMongoDatabase db)
|
|||||||
{
|
{
|
||||||
return _media.InsertOneAsync(media);
|
return _media.InsertOneAsync(media);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Task IncrementViewCountAsync(ObjectId id)
|
||||||
|
{
|
||||||
|
return _media.UpdateOneAsync(m => m.Id == id, Builders<Media>.Update.Inc(m => m.ViewCount, 1));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
global using MaybeError;
|
global using MaybeError;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
using MongoDB.Driver;
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@@ -12,7 +14,13 @@ public static class Extensions
|
|||||||
{
|
{
|
||||||
public static IServiceCollection AddAoba(this IServiceCollection services)
|
public static IServiceCollection AddAoba(this IServiceCollection services)
|
||||||
{
|
{
|
||||||
|
var dbClient = new MongoClient("mongodb://NinoIna:27017");
|
||||||
|
var db = dbClient.GetDatabase("Aoba");
|
||||||
|
|
||||||
|
services.AddSingleton(dbClient);
|
||||||
|
services.AddSingleton<IMongoDatabase>(db);
|
||||||
services.AddSingleton<AobaService>();
|
services.AddSingleton<AobaService>();
|
||||||
|
services.AddSingleton<MediaService>();
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,11 @@
|
|||||||
using AobaV2.Models;
|
using AobaV2.Models;
|
||||||
|
|
||||||
|
using MaybeError.Errors;
|
||||||
|
|
||||||
using MongoDB.Bson;
|
using MongoDB.Bson;
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
using MongoDB.Driver.GridFS;
|
using MongoDB.Driver.GridFS;
|
||||||
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace AobaCore;
|
namespace AobaCore;
|
||||||
public class MediaService(IMongoDatabase db, AobaService aobaService)
|
public class MediaService(IMongoDatabase db, AobaService aobaService)
|
||||||
@@ -18,8 +15,20 @@ public class MediaService(IMongoDatabase db, AobaService aobaService)
|
|||||||
public async Task<Maybe<Media>> UploadMediaAsync(Stream data, string filename, ObjectId owner, CancellationToken cancellationToken = default)
|
public async Task<Maybe<Media>> UploadMediaAsync(Stream data, string filename, ObjectId owner, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var fileId = await _gridFs.UploadFromStreamAsync(filename, data, cancellationToken: cancellationToken);
|
var fileId = await _gridFs.UploadFromStreamAsync(filename, data, cancellationToken: cancellationToken);
|
||||||
var media = new Media(fileId, filename, owner);
|
var media = new Media(fileId, filename, owner);
|
||||||
await aobaService.AddMediaAsync(media);
|
await aobaService.AddMediaAsync(media);
|
||||||
return media;
|
return media;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<Maybe<GridFSDownloadStream, ExceptionError<GridFSException>>> GetMediaStreamAsync(ObjectId id, bool seekable = false)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return await _gridFs.OpenDownloadStreamAsync(id, new GridFSDownloadOptions { Seekable = seekable });
|
||||||
|
}
|
||||||
|
catch (GridFSException ex)
|
||||||
|
{
|
||||||
|
return new ExceptionError<GridFSException>(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,11 @@
|
|||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using System.Text.Encodings.Web;
|
using System.Text.Encodings.Web;
|
||||||
|
|
||||||
namespace AobaV2;
|
namespace AobaServer;
|
||||||
|
|
||||||
internal class AobaAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
|
internal class AobaAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
|
||||||
{
|
{
|
||||||
public AobaAuthenticationHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock)
|
public AobaAuthenticationHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder) : base(options, logger, encoder)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net9.0</TargetFramework>
|
<TargetFramework>net9.0</TargetFramework>
|
||||||
@@ -10,14 +10,11 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Isopoh.Cryptography.Argon2" Version="2.0.0" />
|
<PackageReference Include="Isopoh.Cryptography.Argon2" Version="2.0.0" />
|
||||||
<PackageReference Include="MaybeError" Version="1.0.5" />
|
<PackageReference Include="MaybeError" Version="1.0.6" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.3" />
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.4" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="9.0.3" />
|
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.8.0" />
|
||||||
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.7.0" />
|
|
||||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.2" />
|
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.2" />
|
||||||
<PackageReference Include="Microsoft.Web.LibraryManager.Build" Version="3.0.71" />
|
<PackageReference Include="MimeTypesMap" Version="1.0.9" />
|
||||||
<PackageReference Include="MongoDB.Analyzer" Version="1.5.0" />
|
|
||||||
<PackageReference Include="MongoDB.Bson" Version="3.3.0" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
1
AobaServer/Auth.json
Normal file
1
AobaServer/Auth.json
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{"Issuer":"aobaV2","Audience":"aoba","SecureKey":"iOx/85/SdBG4xji/aNzNMNpwOIZgPgr3hul4ddDrcp8jHxVL4uGPKvOBjVfOqG0IOMDZvaxOjieKdqdJnU9eNA=="}
|
||||||
@@ -1,14 +1,15 @@
|
|||||||
using MongoDB.Bson.IO;
|
using MongoDB.Bson.IO;
|
||||||
|
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace AobaV2;
|
namespace AobaServer;
|
||||||
|
|
||||||
public class AuthInfo
|
public class AuthInfo
|
||||||
{
|
{
|
||||||
public required string Issuer;
|
public required string Issuer { get; set; }
|
||||||
public required string Audience;
|
public required string Audience { get; set; }
|
||||||
public required byte[] SecureKey;
|
public required byte[] SecureKey { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Save this auth into in a json format to the sepcified file
|
/// Save this auth into in a json format to the sepcified file
|
||||||
@@ -53,7 +54,7 @@ public class AuthInfo
|
|||||||
if (File.Exists(path))
|
if (File.Exists(path))
|
||||||
{
|
{
|
||||||
var loaded = Load(path);
|
var loaded = Load(path);
|
||||||
if(loaded != null)
|
if (loaded != null)
|
||||||
return loaded;
|
return loaded;
|
||||||
}
|
}
|
||||||
var info = Create(issuer, audience);
|
var info = Create(issuer, audience);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace AobaV2.Controllers.Api;
|
namespace AobaServer.Controllers.Api;
|
||||||
|
|
||||||
[Route("/api/auth")]
|
[Route("/api/auth")]
|
||||||
public class AuthApi : ControllerBase
|
public class AuthApi : ControllerBase
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace AobaV2.Controllers;
|
namespace AobaServer.Controllers;
|
||||||
|
|
||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
[Route("auth")]
|
[Route("auth")]
|
||||||
|
|||||||
@@ -1,27 +1,44 @@
|
|||||||
using AobaCore;
|
using AobaCore;
|
||||||
|
|
||||||
|
using HeyRed.Mime;
|
||||||
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
using MongoDB.Bson;
|
using MongoDB.Bson;
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
|
|
||||||
namespace AobaV2.Controllers;
|
namespace AobaServer.Controllers;
|
||||||
|
|
||||||
[Route("/m")]
|
[Route("/m")]
|
||||||
public class MediaController(MediaService media) : Controller
|
public class MediaController(MediaService mediaService, ILogger<MediaController> logger) : Controller
|
||||||
{
|
{
|
||||||
[HttpGet("{id}")]
|
[HttpGet("{id}")]
|
||||||
public IActionResult Media(ObjectId id)
|
[ResponseCache(Duration = int.MaxValue)]
|
||||||
|
public async Task<IActionResult> MediaAsync(ObjectId id)
|
||||||
{
|
{
|
||||||
return View();
|
var file = await mediaService.GetMediaStreamAsync(id);
|
||||||
|
if (file.HasError)
|
||||||
|
{
|
||||||
|
logger.LogError(file.Error.Exception, "Failed to load media stream");
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
var mime = MimeTypesMap.GetMimeType(file.Value.FileInfo.Filename);
|
||||||
|
return File(file, mime, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Redirect legacy media urls to the new url
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id"></param>
|
||||||
|
/// <param name="rest"></param>
|
||||||
|
/// <param name="aoba"></param>
|
||||||
|
/// <returns></returns>
|
||||||
[HttpGet("/i/{id}/{*rest}")]
|
[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, [FromServices] AobaService aoba)
|
||||||
{
|
{
|
||||||
var media = await aoba.GetMediaAsync(id);
|
var media = await aoba.GetMediaAsync(id);
|
||||||
if (media == null)
|
if (media == null)
|
||||||
return NotFound();
|
return NotFound();
|
||||||
return LocalRedirectPermanent($"/m/{media.Id}/{rest}");
|
return LocalRedirectPermanent($"/m/{media.MediaId}/{rest}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ ARG BUILD_CONFIGURATION=Release
|
|||||||
WORKDIR /src
|
WORKDIR /src
|
||||||
COPY ["AobaV2/AobaV2.csproj", "AobaV2/"]
|
COPY ["AobaV2/AobaV2.csproj", "AobaV2/"]
|
||||||
RUN dotnet restore "./AobaV2/AobaV2.csproj"
|
RUN dotnet restore "./AobaV2/AobaV2.csproj"
|
||||||
RUN npm install
|
|
||||||
COPY . .
|
COPY . .
|
||||||
WORKDIR "/src/AobaV2"
|
WORKDIR "/src/AobaV2"
|
||||||
RUN dotnet build "./AobaV2.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
RUN dotnet build "./AobaV2.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||||
using MongoDB.Bson;
|
using MongoDB.Bson;
|
||||||
|
|
||||||
namespace AobaV2.Models;
|
namespace AobaServer.Models;
|
||||||
|
|
||||||
public class BsonIdModelBinderProvider : IModelBinderProvider
|
public class BsonIdModelBinderProvider : IModelBinderProvider
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
using AobaCore;
|
using AobaCore;
|
||||||
|
|
||||||
using AobaV2;
|
using AobaServer;
|
||||||
using AobaV2.Models;
|
using AobaServer.Models;
|
||||||
|
|
||||||
using Microsoft.AspNetCore.Authentication;
|
using Microsoft.AspNetCore.Authentication;
|
||||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
||||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||||
using Microsoft.AspNetCore.Http.Features;
|
using Microsoft.AspNetCore.Http.Features;
|
||||||
using Microsoft.IdentityModel.Tokens;
|
using Microsoft.IdentityModel.Tokens;
|
||||||
@@ -12,8 +11,7 @@ using Microsoft.IdentityModel.Tokens;
|
|||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
builder.Services
|
builder.Services.AddControllers(opt => opt.ModelBinderProviders.Add(new BsonIdModelBinderProvider()));
|
||||||
.AddControllers(opt => opt.ModelBinderProviders.Add(new BsonIdModelBinderProvider()));
|
|
||||||
|
|
||||||
|
|
||||||
var authInfo = AuthInfo.LoadOrCreate("Auth.json", "aobaV2", "aoba");
|
var authInfo = AuthInfo.LoadOrCreate("Auth.json", "aobaV2", "aoba");
|
||||||
@@ -89,12 +87,8 @@ app.UseRouting();
|
|||||||
app.UseAuthentication();
|
app.UseAuthentication();
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
|
|
||||||
app.MapStaticAssets();
|
|
||||||
|
|
||||||
app.MapControllerRoute(
|
app.MapControllers();
|
||||||
name: "default",
|
|
||||||
pattern: "{controller=Home}/{action=Index}/{id?}")
|
|
||||||
.WithStaticAssets();
|
|
||||||
|
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
|||||||
Reference in New Issue
Block a user