增加ImageCache微信 图片缓存
This commit is contained in:
1 parent
d6cb5b966b
commit
d0f9f00b76
15 files changed
+518
-30
No files matched your search
@@ -0,0 +1,107 @@
|
||||
using Asp.Versioning;
|
||||
using MAX.Framework.Dtos;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MySqlX.XDevAPI.Common;
|
||||
using Store.Framework.Base;
|
||||
using Store.Framework.Models;
|
||||
using Store.ImageCache.Dtos;
|
||||
using Store.ImageCache.Services;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
||||
namespace KJD_Web.Controllers
|
||||
{
|
||||
[ApiVersionNeutral]
|
||||
[Route("api/ImageCache/[action]")]
|
||||
public class ImageCacheController : AppControllerBase
|
||||
{
|
||||
private readonly ImageCacheService _imageCacheService;
|
||||
private readonly ILogger<ImageCacheController> _logger;
|
||||
|
||||
public ImageCacheController(ImageCacheService imageCacheService, ILogger<ImageCacheController> logger)
|
||||
{
|
||||
if (imageCacheService == null)
|
||||
{
|
||||
throw new ArgumentNullException("imageCacheService");
|
||||
}
|
||||
|
||||
_imageCacheService = imageCacheService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 批量按 MD5 查询图片缓存。
|
||||
/// </summary>
|
||||
[HttpPost]
|
||||
public ApiResult<List<ImageCacheDto>> GetImageCache(ImageCacheBatchQueryRequest request)
|
||||
{
|
||||
List<ImageCacheDto> data = new List<ImageCacheDto>();
|
||||
try
|
||||
{
|
||||
if (request != null && request.Md5s != null)
|
||||
{
|
||||
data = _imageCacheService.GetByMd5s(request.Md5s, request.Platform);
|
||||
}
|
||||
return ApiResult.Ok(data);
|
||||
}
|
||||
catch (ArgumentException ex)
|
||||
{
|
||||
return ApiResult.Fail(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 按 MD5 和平台更新平台图片 URL。
|
||||
/// </summary>
|
||||
[HttpPost]
|
||||
public ApiResult<List<string>> AddOrUpdate(ImageCacheUpdateUrlRequest[] requests)
|
||||
{
|
||||
if (requests == null || requests.Length == 0)
|
||||
{
|
||||
return ApiResult.Fail("参数不能为空");
|
||||
}
|
||||
|
||||
var failedItems = new List<string>();
|
||||
foreach (var item in requests)
|
||||
{
|
||||
if (item == null)
|
||||
{
|
||||
return ApiResult.Fail("图片参数不能包含空项", ResultCode.ValidationError);
|
||||
}
|
||||
try
|
||||
{
|
||||
if (!_imageCacheService.AddOrUpdate(item.Md5, item.Platform, item.PlatformUrl))
|
||||
{
|
||||
failedItems.Add(item.Md5);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "AddOrUpdate 失败, Md5={Md5}", item.Md5);
|
||||
failedItems.Add(item.Md5);
|
||||
}
|
||||
}
|
||||
|
||||
return failedItems.Count == 0
|
||||
? ApiResult.Ok()
|
||||
: ApiResult.Fail("部分图片失败", failedItems);
|
||||
}
|
||||
public class ImageCacheBatchQueryRequest
|
||||
{
|
||||
public List<string> Md5s { get; set; }
|
||||
public byte? Platform { get; set; }
|
||||
}
|
||||
|
||||
public class ImageCacheUpdateUrlRequest
|
||||
{
|
||||
public string Md5 { get; set; }
|
||||
public byte Platform { get; set; }
|
||||
public string PlatformUrl { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user