Increment View count

misc fixes
This commit is contained in:
2025-04-15 23:07:54 -04:00
parent 1c9127ca19
commit d3a139feb5
5 changed files with 18 additions and 31 deletions

View File

@@ -23,4 +23,9 @@ public class AobaService(IMongoDatabase db)
{ {
return _media.UpdateOneAsync(m => m.Id == id, Builders<Media>.Update.Inc(m => m.ViewCount, 1)); return _media.UpdateOneAsync(m => m.Id == id, Builders<Media>.Update.Inc(m => m.ViewCount, 1));
} }
public Task IncrementFileViewCountAsync(ObjectId fileId)
{
return _media.UpdateOneAsync(m => m.MediaId == fileId, Builders<Media>.Update.Inc(m => m.ViewCount, 1));
}
} }

View File

@@ -1,45 +1,28 @@
using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using System.Text.Encodings.Web; using System.Text.Encodings.Web;
namespace AobaServer; namespace AobaServer;
internal class AobaAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions> internal class AobaAuthenticationHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder) : AuthenticationHandler<AuthenticationSchemeOptions>(options, logger, encoder)
{ {
public AobaAuthenticationHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder) : base(options, logger, encoder)
{
}
protected override Task<AuthenticateResult> HandleAuthenticateAsync() protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{ {
throw new System.NotImplementedException(); throw new NotImplementedException();
} }
protected override Task HandleChallengeAsync(AuthenticationProperties properties) protected override Task HandleChallengeAsync(AuthenticationProperties properties)
{ {
//Don't challenge API requests Response.StatusCode = StatusCodes.Status401Unauthorized;
if (OriginalPath.StartsWithSegments("/api")) Response.BodyWriter.Complete();
{
Response.StatusCode = StatusCodes.Status401Unauthorized;
Response.BodyWriter.Complete();
return Task.CompletedTask;
}
//Redirect to login page
Response.Redirect($"/auth/login?ReturnUrl={Uri.EscapeDataString(OriginalPath)}");
return Task.CompletedTask; return Task.CompletedTask;
} }
protected override Task HandleForbiddenAsync(AuthenticationProperties properties) protected override Task HandleForbiddenAsync(AuthenticationProperties properties)
{ {
//Don't show error page for api requests Response.StatusCode = StatusCodes.Status403Forbidden;
if (OriginalPath.StartsWithSegments("/api")) Response.BodyWriter.Complete();
{
Response.StatusCode = StatusCodes.Status403Forbidden;
Response.BodyWriter.Complete();
return Task.CompletedTask;
}
//Show Error page
Response.Redirect($"/error/{StatusCodes.Status403Forbidden}");
return Task.CompletedTask; return Task.CompletedTask;
} }
} }

View File

@@ -6,13 +6,13 @@ namespace AobaServer.Controllers.Api;
public class AuthApi : ControllerBase public class AuthApi : ControllerBase
{ {
[HttpGet("login")] [HttpGet("login")]
public async Task<IActionResult> LoginAsync() public Task<IActionResult> LoginAsync()
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
[HttpGet("register")] [HttpGet("register")]
public async Task<IActionResult> RegisterAsync() public Task<IActionResult> RegisterAsync()
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

View File

@@ -10,7 +10,7 @@ using MongoDB.Driver;
namespace AobaServer.Controllers; namespace AobaServer.Controllers;
[Route("/m")] [Route("/m")]
public class MediaController(MediaService mediaService, ILogger<MediaController> logger) : Controller public class MediaController(MediaService mediaService, AobaService aobaService, ILogger<MediaController> logger) : Controller
{ {
[HttpGet("{id}")] [HttpGet("{id}")]
[ResponseCache(Duration = int.MaxValue)] [ResponseCache(Duration = int.MaxValue)]
@@ -23,6 +23,7 @@ public class MediaController(MediaService mediaService, ILogger<MediaController>
return NotFound(); return NotFound();
} }
var mime = MimeTypesMap.GetMimeType(file.Value.FileInfo.Filename); var mime = MimeTypesMap.GetMimeType(file.Value.FileInfo.Filename);
_ = aobaService.IncrementFileViewCountAsync(id);
return File(file, mime, true); return File(file, mime, true);
} }

View File

@@ -42,9 +42,7 @@ builder.Services.AddAuthentication(options =>
OnMessageReceived = ctx => //Retreive token from cookie if not found in headers OnMessageReceived = ctx => //Retreive token from cookie if not found in headers
{ {
if (string.IsNullOrWhiteSpace(ctx.Token)) if (string.IsNullOrWhiteSpace(ctx.Token))
ctx.Token = ctx.Request.Cookies["token"]; ctx.Token = ctx.Request.Headers.Authorization.FirstOrDefault()?.Replace("Bearer ", "");
if (string.IsNullOrWhiteSpace(ctx.Token))
ctx.Token = ctx.Request.Headers["Authorization"].FirstOrDefault()?.Replace("Bearer ", "");
return Task.CompletedTask; return Task.CompletedTask;
}, },
OnAuthenticationFailed = ctx => OnAuthenticationFailed = ctx =>
@@ -59,7 +57,7 @@ builder.Services.AddAuthentication(options =>
return Task.CompletedTask; return Task.CompletedTask;
} }
}; };
}).AddScheme<AuthenticationSchemeOptions, AobaAuthenticationHandler>("Aoba", cfg => { }); }).AddScheme<AuthenticationSchemeOptions, AobaAuthenticationHandler>("Aoba", null);
builder.Services.AddAoba(); builder.Services.AddAoba();
builder.Services.Configure<FormOptions>(opt => builder.Services.Configure<FormOptions>(opt =>