feat(auth): add configuration options for user registration and anonymous image hosting

This commit is contained in:
shiyu
2025-06-10 16:42:39 +08:00
parent b5931de344
commit 853efaa2fe
6 changed files with 167 additions and 92 deletions

View File

@@ -9,12 +9,13 @@ using Foxel.Services.Storage;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Text.Json; // Added for JsonSerializer in GetTelegramFile if it were kept
using Foxel.Services.Configuration; // Added for IConfigService
namespace Foxel.Api;
[Authorize]
[Route("api/picture")]
public class PictureController(IPictureService pictureService, IStorageService storageService, ILogger<PictureController> logger) : BaseApiController
public class PictureController(IPictureService pictureService, IStorageService storageService, ILogger<PictureController> logger, IConfigService configuration) : BaseApiController
{
private readonly ILogger<PictureController> _logger = logger;
@@ -74,6 +75,16 @@ public class PictureController(IPictureService pictureService, IStorageService s
try
{
var userId = GetCurrentUserId();
if (userId == null)
{
var enableAnonymousUpload = configuration["AppSettings:EnableAnonymousImageHosting"];
if (string.Equals(enableAnonymousUpload, "false", StringComparison.OrdinalIgnoreCase))
{
return Error<PictureResponse>("匿名上传功能已关闭,请登录后操作", 403);
}
}
await using var stream = request.File.OpenReadStream();
var result = await pictureService.UploadPictureAsync(
request.File.FileName,