using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using MyHomePage.Api.Common;
using MyHomePage.Api.Services;
namespace MyHomePage.Api.Controllers;
/// 工具类 API:favicon 抓取等小工具(手动测试用 / 调试入口)。
[ApiController]
[Route("api/utility")]
public class UtilityController : ControllerBase
{
private readonly FaviconService _favicon;
public UtilityController(FaviconService favicon) => _favicon = favicon;
///
/// P31:手动触发 favicon 抓取(不影响正常创建流程)。
/// 任何失败(网络/404/SSRF)均返回 iconUrl=null,由调用方静默用默认。
///
[HttpPost("favicon")]
public async Task> FetchFavicon([FromBody] FaviconRequest request)
{
if (string.IsNullOrWhiteSpace(request.Url))
throw new BusinessException("URL 不能为空", 400);
var iconUrl = await _favicon.FetchAndSaveAsync(request.Url, HttpContext.RequestAborted);
return ApiResponse.Ok(new FaviconResultDto { Url = request.Url, IconUrl = iconUrl });
}
}
/// favicon 抓取请求 DTO
public class FaviconRequest
{
[Required] public string Url { get; set; } = string.Empty;
}
/// favicon 抓取结果 DTO
public class FaviconResultDto
{
public string Url { get; set; } = string.Empty;
public string? IconUrl { get; set; }
}