98 lines
2.9 KiB
C#
98 lines
2.9 KiB
C#
using Blahblah.FlowerStory.Server.Data;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
namespace Blahblah.FlowerStory.Server.Controller;
|
|
|
|
/// <summary>
|
|
/// 图片相关服务
|
|
/// </summary>
|
|
[Produces("image/png")]
|
|
[Route("photo")]
|
|
public class ImageController : BaseController
|
|
{
|
|
/// <inheritdoc/>
|
|
public ImageController(FlowerDatabase database, ILogger<BaseController>? logger = null) : base(database, logger)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// 请求用户头像
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// 请求示例:
|
|
///
|
|
/// GET /photo/avatar
|
|
/// Authorization: authorization id
|
|
///
|
|
/// </remarks>
|
|
/// <returns>认证通过则显示用户头像</returns>
|
|
/// <response code="200">返回头像</response>
|
|
/// <response code="401">认证失败</response>
|
|
/// <response code="404">未找到头像</response>
|
|
[Route("avatar", Name = "getAvatar")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[HttpGet]
|
|
public ActionResult GetUserAvatar()
|
|
{
|
|
var (result, token) = CheckToken();
|
|
if (result != null)
|
|
{
|
|
return result;
|
|
}
|
|
if (token == null)
|
|
{
|
|
return Unauthorized();
|
|
}
|
|
var avatar = QueryUserAvatar(token.UserId);
|
|
if (avatar == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
return File(avatar, "image/png");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 请求花草照片
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// 请求示例:
|
|
///
|
|
/// GET /photo/flower/{fid}/{name}
|
|
/// Authorization: authorization id
|
|
///
|
|
/// </remarks>
|
|
/// <returns>认证通过则显示花草照片</returns>
|
|
/// <response code="200">返回花草照片</response>
|
|
/// <response code="401">认证失败</response>
|
|
/// <response code="404">未找到花草照片</response>
|
|
[Route("flower/{fid}/{name}", Name = "getFlowerPhoto")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[HttpGet]
|
|
public async Task<ActionResult> GetFlowerPhoto([Required] int fid, [Required] string name)
|
|
{
|
|
var (result, token) = CheckToken();
|
|
if (result != null)
|
|
{
|
|
return result;
|
|
}
|
|
if (token == null)
|
|
{
|
|
return Unauthorized();
|
|
}
|
|
|
|
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "uploads", token.UserId.ToString(), fid.ToString(), name);
|
|
if (System.IO.File.Exists(path))
|
|
{
|
|
var data = await System.IO.File.ReadAllBytesAsync(path);
|
|
var ext = Path.GetExtension(path).ToLower();
|
|
return File(data, ext == ".png" ? "image/png" : "image/jpeg");
|
|
}
|
|
return NotFound();
|
|
}
|
|
}
|