as is
This commit is contained in:
22
StopShopping.Services/Models/Req/NameUrlParams.cs
Normal file
22
StopShopping.Services/Models/Req/NameUrlParams.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace StopShopping.Services.Models.Req;
|
||||
|
||||
/// <summary>
|
||||
/// url查询参数
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public record NameUrlParams
|
||||
{
|
||||
/// <summary>
|
||||
/// 场景
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public UploadScences Scences { get; set; }
|
||||
/// <summary>
|
||||
/// 文件名
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
[Required]
|
||||
public string[] Names { get; set; } = [];
|
||||
}
|
||||
@@ -15,10 +15,9 @@ public record UploadParams
|
||||
/// <value></value>
|
||||
public UploadScences Scences { get; set; }
|
||||
/// <summary>
|
||||
/// 文件
|
||||
/// 文件,2m,图片格式
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
[Required]
|
||||
[ImageFileValidation(2 * 1024 * 1024)]
|
||||
public IFormFile? File { get; set; }
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ namespace StopShopping.Services.Models.Resp;
|
||||
/// <summary>
|
||||
/// 文件上传结果
|
||||
/// </summary>
|
||||
public class FileUpload
|
||||
public class FileUploadResp
|
||||
{
|
||||
/// <summary>
|
||||
/// 新名,上传后重命名
|
||||
14
StopShopping.Services/Models/Resp/NameUrlResp.cs
Normal file
14
StopShopping.Services/Models/Resp/NameUrlResp.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace StopShopping.Services.Models.Resp;
|
||||
|
||||
/// <summary>
|
||||
/// url查询响应
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public class NameUrlResp
|
||||
{
|
||||
/// <summary>
|
||||
/// 文件名:文件链接
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public KeyValuePair<string, string>[] NameUrls { get; set; } = [];
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
using FileSignatures;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace System.ComponentModel.DataAnnotations;
|
||||
|
||||
public class ImageFileValidationAttribute : ValidationAttribute
|
||||
{
|
||||
public ImageFileValidationAttribute(long length)
|
||||
{
|
||||
Length = length;
|
||||
}
|
||||
|
||||
public long Length { get; set; }
|
||||
|
||||
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
|
||||
{
|
||||
var fileInspector = validationContext.GetRequiredService<IFileFormatInspector>();
|
||||
if (null == fileInspector)
|
||||
{
|
||||
return new ValidationResult("未配置文件验证器");
|
||||
}
|
||||
if (value is IFormFile file)
|
||||
{
|
||||
var result = Validate(fileInspector, file);
|
||||
if (null != result)
|
||||
return result;
|
||||
}
|
||||
else if (value is IFormFileCollection files)
|
||||
{
|
||||
foreach (var f in files)
|
||||
{
|
||||
var result = Validate(fileInspector, f);
|
||||
if (null != result)
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return ValidationResult.Success;
|
||||
}
|
||||
|
||||
private ValidationResult? Validate(IFileFormatInspector fileInspector, IFormFile file)
|
||||
{
|
||||
if (file.Length > Length)
|
||||
return new ValidationResult("文件太大");
|
||||
var format = fileInspector.DetermineFileFormat(file.OpenReadStream());
|
||||
if (null == format)
|
||||
return new ValidationResult("文件格式不支持");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user