fix: 销售产品封面图改为同源 file 代理,不再签发 COS 直链
All checks were successful
Build and Deploy / deploy (push) Successful in 1m19s

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
zzh 2026-11-26 13:58:43 +08:00
parent b31efc7b56
commit 9ad741e355
5 changed files with 193 additions and 5 deletions

View File

@ -98,6 +98,7 @@ public class SalesProductDetailOutput
/// <summary>
/// 官网线路封面图可访问 URL(预览用)。
/// </summary>
/// <summary>封面同源预览地址(/channel/sales-product/cover/file,非 COS 直链)。</summary>
public string? CoverImageUrl { get; set; }
/// <summary>

View File

@ -104,7 +104,7 @@ public class SalesProductPortalItemOutput
public string? ProductDescription { get; set; }
/// <summary>
/// 官网线路封面图可访问 URL(无图时为空)。
/// 官网线路封面图同源地址(/channel/sales-product/cover/file,非 COS 直链;无图时为空)。
/// </summary>
public string? CoverImageUrl { get; set; }

View File

@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Oms.Application.Dtos.Channels;
using Oms.Application.Shared;
using Oms.Application.Storage;
using Oms.Domain.ChannelsAggregate.Entities;
using Oms.Domain.Shared.Enums;
@ -122,7 +123,7 @@ public partial class SalesProductService
ProductDescription = p.ProductDescription,
CoverImageUrl = string.IsNullOrWhiteSpace(p.CoverImage)
? null
: FileStorage.GetPublicUrl(p.CoverImage),
: OmsManagedImageHelper.BuildSalesProductCoverUrl(p.CoverImage),
DestRegions = destRegions,
CoverageCountries = coverageCountries.Take(PortalCoverageCountryLimit).ToList(),
CargoTags = cargoTags

View File

@ -1,6 +1,7 @@
using System.Linq.Expressions;
using Fake.Application;
using Fake.ObjectStorage;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Oms.Application.Dtos.Channels;
@ -47,7 +48,8 @@ public partial class SalesProductService : ApplicationService
private IObjectStorage FileStorage => LazyServiceProvider.GetRequiredService<IObjectStorage>();
/// <summary>
/// 上传销售产品官网封面图,返回 object key 与可访问 URL(落库存 key)。
/// 上传销售产品官网封面图(multipart 经后端落对象存储,不走 COS 浏览器直传)。
/// 返回 object key(落库)与同源 /cover/file 相对地址(预览,非 COS 直链)。
/// </summary>
/// <param name="file">入参:封面图片文件(jpg/png/webp 等,≤5MB)。</param>
[HttpPost("/channel/sales-product/upload-cover")]
@ -55,10 +57,31 @@ public partial class SalesProductService : ApplicationService
public async Task<object> UploadCoverImageAsync(IFormFile file)
{
var key = await OmsFileUploadHelper.SaveSalesProductCoverAsync(file, FileStorage);
var url = FileStorage.GetPublicUrl(key);
var url = OmsManagedImageHelper.BuildSalesProductCoverUrl(key);
return new { key, url, fileUrl = key };
}
/// <summary>
/// 按 object key 输出销售产品封面图字节(同源代理,避免浏览器直连 COS)。官网匿名可读。
/// </summary>
/// <param name="key">入参:uploads/sales-product-cover/... object key。</param>
/// <param name="v">缓存戳,可选。</param>
/// <param name="cancellationToken">取消令牌。</param>
[AllowAnonymous]
[HttpGet("/channel/sales-product/cover/file")]
public Task<FileContentResult> GetCoverImageFileAsync(
[FromQuery] string key,
[FromQuery] string? v = null,
CancellationToken cancellationToken = default)
{
_ = v;
return OmsManagedImageHelper.StreamByKeyAsync(
FileStorage,
key,
cancellationToken,
OmsManagedImageHelper.SalesProductCoverKeyPrefix);
}
/// <summary>
/// 查询销售产品列表。
/// </summary>
@ -99,7 +122,7 @@ public partial class SalesProductService : ApplicationService
var detail = MapDetailOutput(entity, auditUserNameMap);
detail.CoverImageUrl = string.IsNullOrWhiteSpace(entity.CoverImage)
? null
: FileStorage.GetPublicUrl(entity.CoverImage);
: OmsManagedImageHelper.BuildSalesProductCoverUrl(entity.CoverImage);
return detail;
}

View File

@ -0,0 +1,163 @@
using System.Text.RegularExpressions;
using Fake.Application;
using Fake.ObjectStorage;
using Microsoft.AspNetCore.Mvc;
using Oms.Application.Storage;
namespace Oms.Application.Shared;
/// <summary>
/// 托管图片同源访问:存 object key,对外只签发 /xxx/file 相对路径(不签发 COS 直链)。
/// </summary>
public static class OmsManagedImageHelper
{
public const string SalesProductCoverFilePath = "/channel/sales-product/cover/file";
public const string SalesProductCoverKeyPrefix = "uploads/sales-product-cover/";
/// <summary>
/// 生成浏览器可直开的同源相对路径;物理文件可在 COS/Local,但不把 cos.* 签发给前端。
/// </summary>
public static string BuildSameOriginUrlByKey(string path, string? objectKey)
{
if (string.IsNullOrWhiteSpace(objectKey) || objectKey == "0")
{
return string.Empty;
}
string logicalKey;
try
{
logicalKey = FileStoragePath.NormalizeKey(objectKey);
}
catch
{
return string.Empty;
}
if (string.IsNullOrWhiteSpace(logicalKey))
{
return string.Empty;
}
var stamp = Path.GetFileNameWithoutExtension(logicalKey.Replace('\\', '/'));
if (string.IsNullOrWhiteSpace(stamp))
{
stamp = Math.Abs(logicalKey.GetHashCode(StringComparison.Ordinal)).ToString("x");
}
return $"{path}?key={Uri.EscapeDataString(logicalKey)}&v={Uri.EscapeDataString(stamp)}";
}
public static string BuildSalesProductCoverUrl(string? objectKey) =>
BuildSameOriginUrlByKey(SalesProductCoverFilePath, objectKey);
/// <summary>
/// 按 object key 输出图片字节(同源代理,避免浏览器直连 COS)。
/// </summary>
public static async Task<FileContentResult> StreamByKeyAsync(
IObjectStorage storage,
string? objectKey,
CancellationToken cancellationToken,
string? requiredKeyPrefix = null)
{
if (string.IsNullOrWhiteSpace(objectKey) || objectKey == "0")
{
throw new BusinessException("图片未配置");
}
string logicalKey;
try
{
logicalKey = FileStoragePath.NormalizeKey(objectKey);
}
catch
{
throw new BusinessException("图片路径无效");
}
if (!string.IsNullOrWhiteSpace(requiredKeyPrefix)
&& !logicalKey.StartsWith(requiredKeyPrefix, StringComparison.OrdinalIgnoreCase))
{
throw new BusinessException("图片路径无效");
}
foreach (var candidate in FileStoragePath.ExpandReadKeys(logicalKey))
{
try
{
if (!await storage.ExistsAsync(candidate, cancellationToken))
{
continue;
}
await using var stream = await storage.GetAsync(candidate, cancellationToken);
using var ms = new MemoryStream();
await stream.CopyToAsync(ms, cancellationToken);
var bytes = ms.ToArray();
if (bytes.Length <= 0)
{
continue;
}
return new FileContentResult(bytes, GuessImageMime(candidate))
{
FileDownloadName = Path.GetFileName(candidate.Replace('\\', '/'))
};
}
catch (UnauthorizedAccessException)
{
}
catch (DirectoryNotFoundException)
{
}
catch (FileNotFoundException)
{
}
catch (IOException)
{
}
}
throw new BusinessException("图片不存在或无法读取");
}
public static string GuessImageMime(string pathOrKey)
{
if (Regex.IsMatch(pathOrKey, @"\.png(\?|#|$)", RegexOptions.IgnoreCase))
{
return "image/png";
}
if (Regex.IsMatch(pathOrKey, @"\.jpe?g(\?|#|$)", RegexOptions.IgnoreCase))
{
return "image/jpeg";
}
if (Regex.IsMatch(pathOrKey, @"\.webp(\?|#|$)", RegexOptions.IgnoreCase))
{
return "image/webp";
}
if (Regex.IsMatch(pathOrKey, @"\.gif(\?|#|$)", RegexOptions.IgnoreCase))
{
return "image/gif";
}
if (Regex.IsMatch(pathOrKey, @"\.bmp(\?|#|$)", RegexOptions.IgnoreCase))
{
return "image/bmp";
}
if (Regex.IsMatch(pathOrKey, @"\.svg(\?|#|$)", RegexOptions.IgnoreCase))
{
return "image/svg+xml";
}
if (Regex.IsMatch(pathOrKey, @"\.ico(\?|#|$)", RegexOptions.IgnoreCase))
{
return "image/x-icon";
}
return "application/octet-stream";
}
}