42 lines
1.7 KiB
C#
42 lines
1.7 KiB
C#
using CsharpHttpHelper;
|
|
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Work.Services;
|
|
|
|
namespace Work.Store.Api;
|
|
|
|
/// <summary>图片缓存接口,返回批量保存失败的 MD5 列表。</summary>
|
|
public sealed class ImageCacheApi
|
|
{
|
|
private readonly ApiTransport _transport;
|
|
|
|
public ImageCacheApi(ApiTransport transport)
|
|
=> _transport = transport ?? throw new ArgumentNullException(nameof(transport));
|
|
|
|
public List<ImageCache> GetImageCaches(List<string> md5s)
|
|
=> _transport.GetRequired<List<ImageCache>>("ImageCache/GetImageCache", MethodType.Post, new { md5s });
|
|
|
|
public List<string> SaveImageCaches(int platform, IReadOnlyList<ImageCache> images)
|
|
{
|
|
if (images == null) throw new ArgumentNullException(nameof(images));
|
|
if (images.Count == 0) return new List<string>();
|
|
try
|
|
{
|
|
_transport.Send<object>("ImageCache/AddOrUpdate", MethodType.Post,
|
|
images.Select(image => new { md5 = image.Md5, Platform = platform, PlatformUrl = image.PlatformUrl }).ToArray());
|
|
return new List<string>();
|
|
}
|
|
catch (ApiException ex) when (ex.Code == 500)
|
|
{
|
|
// 兼容旧服务的部分失败响应;没有明确失败项时保持整体失败。
|
|
if (ex.ResponseData is JArray array && array.Count > 0 && array.All(item => item.Type == JTokenType.String))
|
|
return array.ToObject<List<string>>();
|
|
if (ex.ResponseData?.Type == JTokenType.String && !string.IsNullOrWhiteSpace((string)ex.ResponseData))
|
|
return new List<string> { (string)ex.ResponseData };
|
|
throw;
|
|
}
|
|
}
|
|
}
|