All checks were successful
Build and Deploy / deploy (push) Successful in 5m14s
Co-authored-by: Cursor <cursoragent@cursor.com>
122 lines
4.4 KiB
C#
122 lines
4.4 KiB
C#
using Fake.Application;
|
||
using Fake.ObjectStorage;
|
||
using Microsoft.AspNetCore.Http;
|
||
using Oms.Application.Storage;
|
||
|
||
namespace Oms.Application.Shared;
|
||
|
||
/// <summary>
|
||
/// OMS 上传文件辅助(经 <see cref="IObjectStorage"/>)。
|
||
/// 返回值为 object key,对外展示请再调用 <see cref="OmsObjectStorageExtensions.GetPublicUrl"/>。
|
||
/// 用户直传请走 Fake.FileManagement(Presign/Sts + Confirm → FileId)。
|
||
/// </summary>
|
||
public static class OmsFileUploadHelper
|
||
{
|
||
private static readonly HashSet<string> ImageExtensions =
|
||
new(StringComparer.OrdinalIgnoreCase) { ".jpg", ".jpeg", ".png", ".webp", ".gif", ".bmp" };
|
||
|
||
private const long MaxImageBytes = 5 * 1024 * 1024;
|
||
|
||
public static Task<string> SaveRechargeScreenshotAsync(IFormFile? file, IObjectStorage storage) =>
|
||
SaveImageAsync(file, storage, "recharge", "充值截图");
|
||
|
||
public static Task<string> SaveWarehouseSkuImageAsync(IFormFile file, IObjectStorage storage) =>
|
||
SaveImageAsync(file, storage, "sku", "SKU 图片");
|
||
|
||
public static Task<string> SaveWarehouseParcelImageAsync(IFormFile file, IObjectStorage storage) =>
|
||
SaveImageAsync(file, storage, "parcel", "包裹商品图片");
|
||
|
||
public static Task<string> SavePaymentReceiveQrAsync(IFormFile file, IObjectStorage storage) =>
|
||
SaveImageAsync(file, storage, "payment-qr", "收款码图片");
|
||
|
||
private static async Task<string> SaveImageAsync(
|
||
IFormFile? file,
|
||
IObjectStorage storage,
|
||
string folder,
|
||
string label)
|
||
{
|
||
if (file == null || file.Length <= 0)
|
||
{
|
||
if (folder == "recharge")
|
||
{
|
||
return "0";
|
||
}
|
||
|
||
throw new BusinessException($"{label}不能为空");
|
||
}
|
||
|
||
if (file.Length > MaxImageBytes)
|
||
{
|
||
throw new BusinessException($"{label}大小不能超过 5MB");
|
||
}
|
||
|
||
var ext = Path.GetExtension(file.FileName);
|
||
if (string.IsNullOrWhiteSpace(ext) || !ImageExtensions.Contains(ext))
|
||
{
|
||
ext = GuessExtensionFromContentType(file.ContentType) ?? ".jpg";
|
||
}
|
||
|
||
if (!ImageExtensions.Contains(ext))
|
||
{
|
||
throw new BusinessException($"{label}仅支持 jpg、png、webp、gif、bmp 格式");
|
||
}
|
||
|
||
var monthFolder = DateTime.Now.ToString("yyyyMM");
|
||
var fileName = $"{Guid.NewGuid():N}{ext.ToLowerInvariant()}";
|
||
var relativeKey = $"uploads/{folder}/{monthFolder}/{fileName}";
|
||
|
||
await using var stream = file.OpenReadStream();
|
||
return await storage.SaveObjectAsync(stream, relativeKey, file.ContentType);
|
||
}
|
||
|
||
private static readonly HashSet<string> ExcelExtensions =
|
||
new(StringComparer.OrdinalIgnoreCase) { ".xlsx", ".xls" };
|
||
|
||
private const long MaxOrderTemplateBytes = 10 * 1024 * 1024;
|
||
|
||
/// <summary>
|
||
/// 保存下单自定义模板样例文件,返回 (objectKey, 原始文件名)。
|
||
/// </summary>
|
||
public static async Task<(string RelativePath, string FileName)> SaveOrderTemplateSampleAsync(
|
||
IFormFile file,
|
||
int customerId,
|
||
IObjectStorage storage)
|
||
{
|
||
if (file == null || file.Length <= 0)
|
||
{
|
||
throw new BusinessException("上传文件不能为空");
|
||
}
|
||
|
||
if (file.Length > MaxOrderTemplateBytes)
|
||
{
|
||
throw new BusinessException("模板文件大小不能超过 10MB");
|
||
}
|
||
|
||
var ext = Path.GetExtension(file.FileName);
|
||
if (string.IsNullOrWhiteSpace(ext) || !ExcelExtensions.Contains(ext))
|
||
{
|
||
throw new BusinessException("仅支持 .xlsx / .xls 文件");
|
||
}
|
||
|
||
var fileName = $"{Guid.NewGuid():N}{ext.ToLowerInvariant()}";
|
||
var relativeKey = $"uploads/order-templates/{customerId}/{fileName}";
|
||
|
||
await using var stream = file.OpenReadStream();
|
||
var key = await storage.SaveObjectAsync(
|
||
stream,
|
||
relativeKey,
|
||
file.ContentType ?? "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
||
return (key, Path.GetFileName(file.FileName));
|
||
}
|
||
|
||
private static string? GuessExtensionFromContentType(string? contentType) => contentType?.ToLowerInvariant() switch
|
||
{
|
||
"image/jpeg" or "image/jpg" => ".jpg",
|
||
"image/png" => ".png",
|
||
"image/webp" => ".webp",
|
||
"image/gif" => ".gif",
|
||
"image/bmp" => ".bmp",
|
||
_ => null
|
||
};
|
||
}
|