3 changed files with 165 additions and 165 deletions
@ -1,22 +1,22 @@ |
|||
namespace LINGYUN.Abp.OssManagement.Settings |
|||
{ |
|||
public class AbpOssManagementSettingNames |
|||
{ |
|||
public const string GroupName = "Abp.OssManagement"; |
|||
/// <summary>
|
|||
/// 下载分包大小
|
|||
/// </summary>
|
|||
public const string DownloadPackageSize = GroupName + ".DownloadPackageSize"; |
|||
/// <summary>
|
|||
/// 文件限制长度
|
|||
/// </summary>
|
|||
public const string FileLimitLength = GroupName + ".FileLimitLength"; |
|||
/// <summary>
|
|||
/// 允许的文件扩展名类型
|
|||
/// </summary>
|
|||
public const string AllowFileExtensions = GroupName + ".AllowFileExtensions"; |
|||
|
|||
public const int DefaultFileLimitLength = 100; |
|||
public const string DefaultAllowFileExtensions = "dll,zip,rar,txt,log,xml,config,json,jpeg,jpg,png,bmp,ico,xlsx,xltx,xls,xlt,docs,dots,doc,dot,pptx,potx,ppt,pot,chm"; |
|||
} |
|||
} |
|||
namespace LINGYUN.Abp.OssManagement.Settings |
|||
{ |
|||
public class AbpOssManagementSettingNames |
|||
{ |
|||
public const string GroupName = "Abp.OssManagement"; |
|||
/// <summary>
|
|||
/// 下载分包大小
|
|||
/// </summary>
|
|||
public const string DownloadPackageSize = GroupName + ".DownloadPackageSize"; |
|||
/// <summary>
|
|||
/// 文件限制长度
|
|||
/// </summary>
|
|||
public const string FileLimitLength = GroupName + ".FileLimitLength"; |
|||
/// <summary>
|
|||
/// 允许的文件扩展名类型
|
|||
/// </summary>
|
|||
public const string AllowFileExtensions = GroupName + ".AllowFileExtensions"; |
|||
|
|||
public const long DefaultFileLimitLength = 100L; |
|||
public const string DefaultAllowFileExtensions = "dll,zip,rar,txt,log,xml,config,json,jpeg,jpg,png,bmp,ico,xlsx,xltx,xls,xlt,docs,dots,doc,dot,pptx,potx,ppt,pot,chm"; |
|||
} |
|||
} |
|||
|
|||
@ -1,96 +1,96 @@ |
|||
using LINGYUN.Abp.OssManagement.Localization; |
|||
using LINGYUN.Abp.OssManagement.Settings; |
|||
using Microsoft.Extensions.Caching.Memory; |
|||
using Microsoft.Extensions.Localization; |
|||
using System; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.IO; |
|||
using Volo.Abp.Settings; |
|||
|
|||
namespace LINGYUN.Abp.OssManagement |
|||
{ |
|||
public class FileValidater : IFileValidater, ISingletonDependency |
|||
{ |
|||
private readonly IMemoryCache _cache; |
|||
private readonly ISettingProvider _settingProvider; |
|||
private readonly IServiceProvider _serviceProvider; |
|||
private readonly IStringLocalizer _stringLocalizer; |
|||
|
|||
public FileValidater( |
|||
IMemoryCache cache, |
|||
ISettingProvider settingProvider, |
|||
IServiceProvider serviceProvider, |
|||
IStringLocalizer<AbpOssManagementResource> stringLocalizer) |
|||
{ |
|||
_cache = cache; |
|||
_settingProvider = settingProvider; |
|||
_serviceProvider = serviceProvider; |
|||
_stringLocalizer = stringLocalizer; |
|||
} |
|||
|
|||
public virtual async Task ValidationAsync(UploadOssObjectInput input) |
|||
{ |
|||
var validation = await GetByCacheItemAsync(); |
|||
if (validation.SizeLimit * 1024 * 1024 < input.TotalSize) |
|||
{ |
|||
throw new UserFriendlyException(_stringLocalizer["UploadFileSizeBeyondLimit", validation.SizeLimit]); |
|||
} |
|||
var fileExtensionName = FileHelper.GetExtension(input.FileName); |
|||
if (!validation.AllowedExtensions |
|||
.Any(fe => fe.Equals(fileExtensionName, StringComparison.CurrentCultureIgnoreCase))) |
|||
{ |
|||
throw new UserFriendlyException(_stringLocalizer["NotAllowedFileExtensionName", fileExtensionName]); |
|||
} |
|||
} |
|||
|
|||
protected virtual async Task<FileValidation> GetByCacheItemAsync() |
|||
{ |
|||
var fileValidation = _cache.Get<FileValidation>(FileValidation.CacheKey); |
|||
if (fileValidation == null) |
|||
{ |
|||
fileValidation = await GetBySettingAsync(); |
|||
_cache.Set(FileValidation.CacheKey, |
|||
fileValidation, |
|||
new MemoryCacheEntryOptions |
|||
{ |
|||
AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(2) |
|||
}); |
|||
} |
|||
return fileValidation; |
|||
} |
|||
|
|||
protected virtual async Task<FileValidation> GetBySettingAsync() |
|||
{ |
|||
var fileSizeLimited = await _settingProvider |
|||
.GetAsync( |
|||
AbpOssManagementSettingNames.FileLimitLength, |
|||
AbpOssManagementSettingNames.DefaultFileLimitLength); |
|||
var fileAllowExtension = await _settingProvider |
|||
.GetOrDefaultAsync(AbpOssManagementSettingNames.AllowFileExtensions, _serviceProvider); |
|||
|
|||
return new FileValidation(fileSizeLimited, fileAllowExtension.Split(',')); |
|||
} |
|||
} |
|||
|
|||
public class FileValidation |
|||
{ |
|||
public const string CacheKey = "Abp.OssManagement.FileValidation"; |
|||
public int SizeLimit { get; set; } |
|||
public string[] AllowedExtensions { get; set; } |
|||
public FileValidation() |
|||
{ |
|||
|
|||
} |
|||
|
|||
public FileValidation( |
|||
int sizeLimit, |
|||
string[] allowedExtensions) |
|||
{ |
|||
SizeLimit = sizeLimit; |
|||
AllowedExtensions = allowedExtensions; |
|||
} |
|||
} |
|||
} |
|||
using LINGYUN.Abp.OssManagement.Localization; |
|||
using LINGYUN.Abp.OssManagement.Settings; |
|||
using Microsoft.Extensions.Caching.Memory; |
|||
using Microsoft.Extensions.Localization; |
|||
using System; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.IO; |
|||
using Volo.Abp.Settings; |
|||
|
|||
namespace LINGYUN.Abp.OssManagement |
|||
{ |
|||
public class FileValidater : IFileValidater, ISingletonDependency |
|||
{ |
|||
private readonly IMemoryCache _cache; |
|||
private readonly ISettingProvider _settingProvider; |
|||
private readonly IServiceProvider _serviceProvider; |
|||
private readonly IStringLocalizer _stringLocalizer; |
|||
|
|||
public FileValidater( |
|||
IMemoryCache cache, |
|||
ISettingProvider settingProvider, |
|||
IServiceProvider serviceProvider, |
|||
IStringLocalizer<AbpOssManagementResource> stringLocalizer) |
|||
{ |
|||
_cache = cache; |
|||
_settingProvider = settingProvider; |
|||
_serviceProvider = serviceProvider; |
|||
_stringLocalizer = stringLocalizer; |
|||
} |
|||
|
|||
public virtual async Task ValidationAsync(UploadOssObjectInput input) |
|||
{ |
|||
var validation = await GetByCacheItemAsync(); |
|||
if (validation.SizeLimit * 1024 * 1024 < input.TotalSize) |
|||
{ |
|||
throw new UserFriendlyException(_stringLocalizer["UploadFileSizeBeyondLimit", validation.SizeLimit]); |
|||
} |
|||
var fileExtensionName = FileHelper.GetExtension(input.FileName); |
|||
if (!validation.AllowedExtensions |
|||
.Any(fe => fe.Equals(fileExtensionName, StringComparison.CurrentCultureIgnoreCase))) |
|||
{ |
|||
throw new UserFriendlyException(_stringLocalizer["NotAllowedFileExtensionName", fileExtensionName]); |
|||
} |
|||
} |
|||
|
|||
protected virtual async Task<FileValidation> GetByCacheItemAsync() |
|||
{ |
|||
var fileValidation = _cache.Get<FileValidation>(FileValidation.CacheKey); |
|||
if (fileValidation == null) |
|||
{ |
|||
fileValidation = await GetBySettingAsync(); |
|||
_cache.Set(FileValidation.CacheKey, |
|||
fileValidation, |
|||
new MemoryCacheEntryOptions |
|||
{ |
|||
AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(2) |
|||
}); |
|||
} |
|||
return fileValidation; |
|||
} |
|||
|
|||
protected virtual async Task<FileValidation> GetBySettingAsync() |
|||
{ |
|||
var fileSizeLimited = await _settingProvider |
|||
.GetAsync( |
|||
AbpOssManagementSettingNames.FileLimitLength, |
|||
AbpOssManagementSettingNames.DefaultFileLimitLength); |
|||
var fileAllowExtension = await _settingProvider |
|||
.GetOrDefaultAsync(AbpOssManagementSettingNames.AllowFileExtensions, _serviceProvider); |
|||
|
|||
return new FileValidation(fileSizeLimited, fileAllowExtension.Split(',')); |
|||
} |
|||
} |
|||
|
|||
public class FileValidation |
|||
{ |
|||
public const string CacheKey = "Abp.OssManagement.FileValidation"; |
|||
public long SizeLimit { get; set; } |
|||
public string[] AllowedExtensions { get; set; } |
|||
public FileValidation() |
|||
{ |
|||
|
|||
} |
|||
|
|||
public FileValidation( |
|||
long sizeLimit, |
|||
string[] allowedExtensions) |
|||
{ |
|||
SizeLimit = sizeLimit; |
|||
AllowedExtensions = allowedExtensions; |
|||
} |
|||
} |
|||
} |
|||
|
|||
@ -1,47 +1,47 @@ |
|||
using Microsoft.AspNetCore.Http; |
|||
using System.ComponentModel.DataAnnotations; |
|||
|
|||
namespace LINGYUN.Abp.OssManagement |
|||
{ |
|||
public class UploadOssObjectInput |
|||
{ |
|||
public string Bucket { get; set; } |
|||
public string Path { get; set; } |
|||
|
|||
#region 配合Uplaoder 分块传输
|
|||
/// <summary>
|
|||
/// 文件名
|
|||
/// </summary>
|
|||
[Required] |
|||
public string FileName { get; set; } |
|||
/// <summary>
|
|||
/// 常规块大小
|
|||
/// </summary>
|
|||
[Required] |
|||
public int ChunkSize { get; set; } |
|||
/// <summary>
|
|||
/// 当前块大小
|
|||
/// </summary>
|
|||
[Required] |
|||
public int CurrentChunkSize { get; set; } |
|||
/// <summary>
|
|||
/// 当前上传中块的索引
|
|||
/// </summary>
|
|||
[Required] |
|||
public int ChunkNumber { get; set; } |
|||
/// <summary>
|
|||
/// 块总数
|
|||
/// </summary>
|
|||
[Required] |
|||
public int TotalChunks { get; set; } |
|||
/// <summary>
|
|||
/// 总文件大小
|
|||
/// </summary>
|
|||
[Required] |
|||
public int TotalSize { get; set; } |
|||
|
|||
#endregion
|
|||
|
|||
public IFormFile File { get; set; } |
|||
} |
|||
} |
|||
using Microsoft.AspNetCore.Http; |
|||
using System.ComponentModel.DataAnnotations; |
|||
|
|||
namespace LINGYUN.Abp.OssManagement |
|||
{ |
|||
public class UploadOssObjectInput |
|||
{ |
|||
public string Bucket { get; set; } |
|||
public string Path { get; set; } |
|||
|
|||
#region 配合Uplaoder 分块传输
|
|||
/// <summary>
|
|||
/// 文件名
|
|||
/// </summary>
|
|||
[Required] |
|||
public string FileName { get; set; } |
|||
/// <summary>
|
|||
/// 常规块大小
|
|||
/// </summary>
|
|||
[Required] |
|||
public long ChunkSize { get; set; } |
|||
/// <summary>
|
|||
/// 当前块大小
|
|||
/// </summary>
|
|||
[Required] |
|||
public long CurrentChunkSize { get; set; } |
|||
/// <summary>
|
|||
/// 当前上传中块的索引
|
|||
/// </summary>
|
|||
[Required] |
|||
public int ChunkNumber { get; set; } |
|||
/// <summary>
|
|||
/// 块总数
|
|||
/// </summary>
|
|||
[Required] |
|||
public int TotalChunks { get; set; } |
|||
/// <summary>
|
|||
/// 总文件大小
|
|||
/// </summary>
|
|||
[Required] |
|||
public long TotalSize { get; set; } |
|||
|
|||
#endregion
|
|||
|
|||
public IFormFile File { get; set; } |
|||
} |
|||
} |
|||
|
|||
Loading…
Reference in new issue