diff --git a/src/Oms.Application/Dtos/Channels/SalesProductDetailOutput.cs b/src/Oms.Application/Dtos/Channels/SalesProductDetailOutput.cs index 5e423cf..29119bc 100644 --- a/src/Oms.Application/Dtos/Channels/SalesProductDetailOutput.cs +++ b/src/Oms.Application/Dtos/Channels/SalesProductDetailOutput.cs @@ -98,6 +98,7 @@ public class SalesProductDetailOutput /// /// 官网线路封面图可访问 URL(预览用)。 /// + /// 封面同源预览地址(/channel/sales-product/cover/file,非 COS 直链)。 public string? CoverImageUrl { get; set; } /// diff --git a/src/Oms.Application/Dtos/Channels/SalesProductPortalCatalogOutput.cs b/src/Oms.Application/Dtos/Channels/SalesProductPortalCatalogOutput.cs index 78d7874..633d5f9 100644 --- a/src/Oms.Application/Dtos/Channels/SalesProductPortalCatalogOutput.cs +++ b/src/Oms.Application/Dtos/Channels/SalesProductPortalCatalogOutput.cs @@ -104,7 +104,7 @@ public class SalesProductPortalItemOutput public string? ProductDescription { get; set; } /// - /// 官网线路封面图可访问 URL(无图时为空)。 + /// 官网线路封面图同源地址(/channel/sales-product/cover/file,非 COS 直链;无图时为空)。 /// public string? CoverImageUrl { get; set; } diff --git a/src/Oms.Application/Services/Channels/SalesProductService.Portal.cs b/src/Oms.Application/Services/Channels/SalesProductService.Portal.cs index c22da94..7395728 100644 --- a/src/Oms.Application/Services/Channels/SalesProductService.Portal.cs +++ b/src/Oms.Application/Services/Channels/SalesProductService.Portal.cs @@ -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 diff --git a/src/Oms.Application/Services/Channels/SalesProductService.cs b/src/Oms.Application/Services/Channels/SalesProductService.cs index 4037854..9766203 100644 --- a/src/Oms.Application/Services/Channels/SalesProductService.cs +++ b/src/Oms.Application/Services/Channels/SalesProductService.cs @@ -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(); /// - /// 上传销售产品官网封面图,返回 object key 与可访问 URL(落库存 key)。 + /// 上传销售产品官网封面图(multipart 经后端落对象存储,不走 COS 浏览器直传)。 + /// 返回 object key(落库)与同源 /cover/file 相对地址(预览,非 COS 直链)。 /// /// 入参:封面图片文件(jpg/png/webp 等,≤5MB)。 [HttpPost("/channel/sales-product/upload-cover")] @@ -55,10 +57,31 @@ public partial class SalesProductService : ApplicationService public async Task 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 }; } + /// + /// 按 object key 输出销售产品封面图字节(同源代理,避免浏览器直连 COS)。官网匿名可读。 + /// + /// 入参:uploads/sales-product-cover/... object key。 + /// 缓存戳,可选。 + /// 取消令牌。 + [AllowAnonymous] + [HttpGet("/channel/sales-product/cover/file")] + public Task GetCoverImageFileAsync( + [FromQuery] string key, + [FromQuery] string? v = null, + CancellationToken cancellationToken = default) + { + _ = v; + return OmsManagedImageHelper.StreamByKeyAsync( + FileStorage, + key, + cancellationToken, + OmsManagedImageHelper.SalesProductCoverKeyPrefix); + } + /// /// 查询销售产品列表。 /// @@ -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; } diff --git a/src/Oms.Application/Shared/OmsManagedImageHelper.cs b/src/Oms.Application/Shared/OmsManagedImageHelper.cs new file mode 100644 index 0000000..4a7b035 --- /dev/null +++ b/src/Oms.Application/Shared/OmsManagedImageHelper.cs @@ -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; + +/// +/// 托管图片同源访问:存 object key,对外只签发 /xxx/file 相对路径(不签发 COS 直链)。 +/// +public static class OmsManagedImageHelper +{ + public const string SalesProductCoverFilePath = "/channel/sales-product/cover/file"; + public const string SalesProductCoverKeyPrefix = "uploads/sales-product-cover/"; + + /// + /// 生成浏览器可直开的同源相对路径;物理文件可在 COS/Local,但不把 cos.* 签发给前端。 + /// + 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); + + /// + /// 按 object key 输出图片字节(同源代理,避免浏览器直连 COS)。 + /// + public static async Task 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"; + } +}