42 changed files with 2492 additions and 408 deletions
@ -1,38 +0,0 @@ |
|||
namespace Lion.AbpPro.FileManagement.Files; |
|||
|
|||
/// <summary>
|
|||
/// 创建文件
|
|||
/// </summary>
|
|||
public class CreateFileObjectInput |
|||
{ |
|||
/// <summary>
|
|||
/// 文件Provider
|
|||
/// </summary>
|
|||
[Required(ErrorMessage = "文件Provider不能为空")] |
|||
public string ProviderKey { get; set; } |
|||
/// <summary>
|
|||
/// 文件扩展名
|
|||
/// </summary>
|
|||
[Required(ErrorMessage = "文件扩展名不能为空")] |
|||
public string FileExtension { get; set; } |
|||
/// <summary>
|
|||
/// 二进制数据
|
|||
/// </summary>
|
|||
[Required(ErrorMessage = "二进制数据不能为空")] |
|||
public long Bytes { get; set; } |
|||
/// <summary>
|
|||
/// 文件大小
|
|||
/// </summary>
|
|||
[Required(ErrorMessage = "文件大小不能为空")] |
|||
public long FileSize { get; set; } |
|||
/// <summary>
|
|||
/// 文件名称
|
|||
/// </summary>
|
|||
[Required(ErrorMessage = "文件名称不能为空")] |
|||
public string ContentType { get; set; } |
|||
/// <summary>
|
|||
/// 文件名称
|
|||
/// </summary>
|
|||
[Required(ErrorMessage = "文件名称不能为空")] |
|||
public string FileName { get; set; } |
|||
} |
|||
@ -1,9 +0,0 @@ |
|||
namespace Lion.AbpPro.FileManagement.Files; |
|||
|
|||
public class GetFileObjectInput |
|||
{ |
|||
/// <summary>
|
|||
/// 文件Id
|
|||
/// </summary>
|
|||
public Guid Id { get; set; } |
|||
} |
|||
@ -1,44 +0,0 @@ |
|||
namespace Lion.AbpPro.FileManagement.Files; |
|||
|
|||
public class GetFileObjectOutput |
|||
{ |
|||
/// <summary>
|
|||
/// 文件Id
|
|||
/// </summary>
|
|||
public Guid Id { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 文件Provider
|
|||
/// </summary>
|
|||
public string ProviderKey { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 文件扩展名
|
|||
/// </summary>
|
|||
public string FileExtension { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 二进制数据
|
|||
/// </summary>
|
|||
public byte[] Bytes { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 文件大小
|
|||
/// </summary>
|
|||
public long FileSize { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 文件名称
|
|||
/// </summary>
|
|||
public string ContentType { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 文件名称
|
|||
/// </summary>
|
|||
public string FileName { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 创建时间
|
|||
/// </summary>
|
|||
public DateTime CreationTime { get; set; } |
|||
} |
|||
@ -1,43 +0,0 @@ |
|||
namespace Lion.AbpPro.FileManagement.Files; |
|||
|
|||
/// <summary>
|
|||
/// 更新文件
|
|||
/// </summary>
|
|||
public class UpdateFileInput |
|||
{ |
|||
/// <summary>
|
|||
/// 文件Id
|
|||
/// </summary>
|
|||
public Guid Id { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 文件Provider
|
|||
/// </summary>
|
|||
[Required(ErrorMessage = "文件Provider不能为空")] |
|||
public string ProviderKey { get; set; } |
|||
/// <summary>
|
|||
/// 文件扩展名
|
|||
/// </summary>
|
|||
[Required(ErrorMessage = "文件扩展名不能为空")] |
|||
public string FileExtension { get; set; } |
|||
/// <summary>
|
|||
/// 二进制数据
|
|||
/// </summary>
|
|||
[Required(ErrorMessage = "二进制数据不能为空")] |
|||
public long Bytes { get; set; } |
|||
/// <summary>
|
|||
/// 文件大小
|
|||
/// </summary>
|
|||
[Required(ErrorMessage = "文件大小不能为空")] |
|||
public long FileSize { get; set; } |
|||
/// <summary>
|
|||
/// 文件名称
|
|||
/// </summary>
|
|||
[Required(ErrorMessage = "文件名称不能为空")] |
|||
public string ContentType { get; set; } |
|||
/// <summary>
|
|||
/// 文件名称
|
|||
/// </summary>
|
|||
[Required(ErrorMessage = "文件名称不能为空")] |
|||
public string FileName { get; set; } |
|||
} |
|||
@ -1,6 +1,6 @@ |
|||
namespace Lion.AbpPro.FileManagement.Files; |
|||
|
|||
public class UploadOutput |
|||
public class UploadFileObjectOutput |
|||
{ |
|||
public Guid Id { get; set; } |
|||
|
|||
@ -0,0 +1,15 @@ |
|||
namespace Lion.AbpPro.FileManagement |
|||
{ |
|||
public class FileManagementConsts |
|||
{ |
|||
/// <summary>名称空间</summary>
|
|||
public const string NameSpace = "Lion.AbpPro.FileManagement"; |
|||
/// <summary>默认语言</summary>
|
|||
public const string DefaultCultureName = "zh-Hans"; |
|||
|
|||
public const int Kilobyte = 1024; |
|||
public const int Megabyte = Kilobyte * 1024; |
|||
public const long Gigabyte = Megabyte * 1024; |
|||
public const long Terabyte = Gigabyte * 1024; |
|||
} |
|||
} |
|||
@ -0,0 +1,45 @@ |
|||
namespace Lion.AbpPro.FileManagement.Files; |
|||
|
|||
public static class SizeHelper |
|||
{ |
|||
public static string BeautifySize(long size) |
|||
{ |
|||
if (size == 0 || size == 1) |
|||
{ |
|||
return $"{size} Byte"; |
|||
} |
|||
|
|||
if (size >= FileManagementConsts.Terabyte) |
|||
{ |
|||
var fixedSize = ((float)size / (float)FileManagementConsts.Terabyte); |
|||
return $"{FormatSize(fixedSize)} TB"; |
|||
} |
|||
|
|||
if (size >= FileManagementConsts.Gigabyte) |
|||
{ |
|||
var fixedSize = ((float)size / (float)FileManagementConsts.Gigabyte); |
|||
return $"{FormatSize(fixedSize)} GB"; |
|||
} |
|||
|
|||
if (size >= FileManagementConsts.Megabyte) |
|||
{ |
|||
var fixedSize = ((float)size / (float)FileManagementConsts.Megabyte); |
|||
return $"{FormatSize(fixedSize)} MB"; |
|||
} |
|||
|
|||
if (size >= FileManagementConsts.Kilobyte) |
|||
{ |
|||
var fixedSize = ((float)size / (float)FileManagementConsts.Kilobyte); |
|||
return $"{FormatSize(fixedSize)} KB"; |
|||
} |
|||
|
|||
return $"{size} B"; |
|||
} |
|||
|
|||
public static string FormatSize(float size) |
|||
{ |
|||
var s = $"{size:0.00}"; |
|||
|
|||
return s.EndsWith("00") ? ((int)size).ToString() : s; |
|||
} |
|||
} |
|||
@ -0,0 +1,6 @@ |
|||
namespace Lion.AbpPro.FileManagement.Files; |
|||
|
|||
[BlobContainerName("abp-file-management")] |
|||
public class AbpProFileManagementContainer |
|||
{ |
|||
} |
|||
@ -0,0 +1,13 @@ |
|||
namespace Lion.AbpPro.FileManagement.Files |
|||
{ |
|||
public class DomainFileManagementException : BusinessException |
|||
{ |
|||
public DomainFileManagementException(string code = null, string message = null, string details = null, Exception innerException = null, |
|||
LogLevel logLevel = LogLevel.Warning) : base(code, message, details, |
|||
innerException, |
|||
logLevel |
|||
) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,39 @@ |
|||
namespace Lion.AbpPro.FileManagement.Files; |
|||
|
|||
public class FileManager : DomainService |
|||
{ |
|||
private readonly IBlobContainer<AbpProFileManagementContainer> _blobContainer; |
|||
private readonly FileObjectManager _fileObjectManager; |
|||
|
|||
public FileManager(IBlobContainer<AbpProFileManagementContainer> blobContainer, FileObjectManager fileObjectManager) |
|||
{ |
|||
_blobContainer = blobContainer; |
|||
_fileObjectManager = fileObjectManager; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 创建文件
|
|||
/// </summary>
|
|||
public virtual async Task<FileObjectDto> CreateAsync( |
|||
Guid id, |
|||
string fileName, |
|||
long fileSize, |
|||
string contentType, |
|||
byte[] content, |
|||
bool overwrite = false |
|||
) |
|||
{ |
|||
var entity = await _fileObjectManager.CreateAsync(id, fileName, fileSize, contentType, content, overwrite); |
|||
await _blobContainer.SaveAsync(id.ToString(), content, true); |
|||
return entity; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 删除文件
|
|||
/// </summary>
|
|||
public virtual async Task DeleteAsync(Guid id) |
|||
{ |
|||
await _fileObjectManager.DeleteAsync(id); |
|||
await _blobContainer.DeleteAsync(id.ToString()); |
|||
} |
|||
} |
|||
@ -1,30 +0,0 @@ |
|||
using Lion.AbpPro.FileManagement.Files; |
|||
using Volo.Abp.Guids; |
|||
|
|||
namespace Lion.AbpPro.FileManagement.Provider; |
|||
|
|||
public class DatabaseFileProvider : IFileProvider, ITransientDependency |
|||
{ |
|||
public const string Name = nameof(DatabaseFileProvider); |
|||
private readonly FileObjectManager _fileObjectManager; |
|||
private readonly IGuidGenerator _guidGenerator; |
|||
|
|||
public DatabaseFileProvider(FileObjectManager fileObjectManager, IGuidGenerator guidGenerator) |
|||
{ |
|||
_fileObjectManager = fileObjectManager; |
|||
_guidGenerator = guidGenerator; |
|||
} |
|||
|
|||
public async Task<UpdateResult> UploadAsync(UpdateDto input) |
|||
{ |
|||
var id = _guidGenerator.Create(); |
|||
var fileName = id + "_" + input.FileName; |
|||
await _fileObjectManager.CreateAsync(id, fileName, input.Bytes, input.FileSize, input.ContentType, Name); |
|||
return new UpdateResult() |
|||
{ |
|||
Id = id, |
|||
FileName = fileName, |
|||
FilePath = fileName |
|||
}; |
|||
} |
|||
} |
|||
@ -1,6 +0,0 @@ |
|||
namespace Lion.AbpPro.FileManagement.Provider; |
|||
|
|||
public interface IFileProvider |
|||
{ |
|||
Task<UpdateResult> UploadAsync(UpdateDto input); |
|||
} |
|||
@ -1,5 +1,5 @@ |
|||
{ |
|||
"ConnectionStrings": { |
|||
"Default": "Data Source=localhost;Port=3306;Database=LionAbpPro9;uid=root;pwd=1q2w3E*;charset=utf8mb4;Allow User Variables=true;AllowLoadLocalInfile=true" |
|||
"Default": "Data Source=localhost;Port=3306;Database=LionAbpPro91;uid=root;pwd=1q2w3E*;charset=utf8mb4;Allow User Variables=true;AllowLoadLocalInfile=true" |
|||
} |
|||
} |
|||
File diff suppressed because it is too large
@ -0,0 +1,57 @@ |
|||
using Microsoft.EntityFrameworkCore.Migrations; |
|||
|
|||
#nullable disable |
|||
|
|||
namespace Lion.AbpPro.Migrations |
|||
{ |
|||
/// <inheritdoc />
|
|||
public partial class _103 : Migration |
|||
{ |
|||
/// <inheritdoc />
|
|||
protected override void Up(MigrationBuilder migrationBuilder) |
|||
{ |
|||
migrationBuilder.DropColumn( |
|||
name: "Bytes", |
|||
table: "AbpFileObjects"); |
|||
|
|||
migrationBuilder.DropColumn( |
|||
name: "FileExtension", |
|||
table: "AbpFileObjects"); |
|||
|
|||
migrationBuilder.DropColumn( |
|||
name: "ProviderKey", |
|||
table: "AbpFileObjects"); |
|||
} |
|||
|
|||
/// <inheritdoc />
|
|||
protected override void Down(MigrationBuilder migrationBuilder) |
|||
{ |
|||
migrationBuilder.AddColumn<byte[]>( |
|||
name: "Bytes", |
|||
table: "AbpFileObjects", |
|||
type: "longblob", |
|||
nullable: true, |
|||
comment: "二进制数据"); |
|||
|
|||
migrationBuilder.AddColumn<string>( |
|||
name: "FileExtension", |
|||
table: "AbpFileObjects", |
|||
type: "varchar(36)", |
|||
maxLength: 36, |
|||
nullable: false, |
|||
defaultValue: "", |
|||
comment: "文件扩展名") |
|||
.Annotation("MySql:CharSet", "utf8mb4"); |
|||
|
|||
migrationBuilder.AddColumn<string>( |
|||
name: "ProviderKey", |
|||
table: "AbpFileObjects", |
|||
type: "varchar(128)", |
|||
maxLength: 128, |
|||
nullable: false, |
|||
defaultValue: "", |
|||
comment: "文件Provider") |
|||
.Annotation("MySql:CharSet", "utf8mb4"); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue