Skip to content

Commit

Permalink
feat: 完善文件模块
Browse files Browse the repository at this point in the history
  • Loading branch information
WangJunZzz committed Feb 19, 2025
1 parent 611dcc1 commit 62fcb7c
Show file tree
Hide file tree
Showing 42 changed files with 2,492 additions and 408 deletions.
2 changes: 2 additions & 0 deletions aspnet-core/Directory.Build.Volo.targets
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<PackageReference Update="Volo.Abp.Identity.AspNetCore" Version="9.0.4"/>
<PackageReference Update="Volo.Abp.Caching" Version="9.0.4"/>
<PackageReference Update="Volo.Abp.BlobStoring.Aliyun" Version="9.0.4"/>
<PackageReference Update="Volo.Abp.BlobStoring" Version="9.0.4"/>
<PackageReference Update="Volo.Abp.BlobStoring.FileSystem" Version="9.0.4"/>
<PackageReference Update="Volo.Abp.BackgroundJobs" Version="9.0.4"/>
<PackageReference Update="Volo.Abp.Authorization.Abstractions" Version="9.0.4"/>
<PackageReference Update="Volo.Abp.BackgroundJobs.HangFire" Version="9.0.4"/>
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Content;

namespace Lion.AbpPro.FileManagement.Files;

Expand All @@ -18,17 +19,16 @@ public interface IFileAppService : IApplicationService
/// <summary>
/// 上传文件
/// </summary>
Task<List<UploadOutput>> UploadAsync(List<IFormFile> files);
Task UploadAsync(List<IFormFile> files);

/// <summary>
/// 删除文件
/// </summary>
Task DeleteAsync(DeleteFileObjectInput input);

Task<GetFileObjectOutput> GetAsync(GetFileObjectInput input);

/// <summary>
/// 下载文件
/// </summary>
Task<FileContentResult> DownloadAsync(DownloadFileObjectInput input);
Task<RemoteStreamContent> DownloadAsync(DownloadFileObjectInput input);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,26 @@ public class PageFileObjectOutput
/// 文件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; }


public string BeautifySize => SizeHelper.BeautifySize(FileSize);

/// <summary>
/// 文件名称
/// </summary>
public string ContentType { get; set; }

/// <summary>
/// 文件名称
/// </summary>
public string FileName { get; set; }

/// <summary>
/// 创建时间
/// </summary>
public DateTime CreationTime { get; set; }
public DateTime CreationTime { get; set; }
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Lion.AbpPro.FileManagement.Files;

public class UploadOutput
public class UploadFileObjectOutput
{
public Guid Id { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ public class FileManagementPermissionDefinitionProvider : PermissionDefinitionPr
{
public override void Define(IPermissionDefinitionContext context)
{
var abpIdentityGroup = context.GetGroup("AbpIdentity");

var fileManagement = abpIdentityGroup.AddPermission(FileManagementPermissions.FileManagement.Default,
L("Permission:FileManagement"));
var fileManagementGroup = context.AddGroup(FileManagementPermissions.GroupName, L("Permission:FileManagement"));
var filePermission = fileManagementGroup.AddPermission(FileManagementPermissions.FileManagement.Default, L("Permission:FileManagement:File"));
filePermission.AddChild(FileManagementPermissions.FileManagement.Upload, L("Permission:FileManagement:File:Upload"));
filePermission.AddChild(FileManagementPermissions.FileManagement.Download, L("Permission:FileManagement:File:Download"));
filePermission.AddChild(FileManagementPermissions.FileManagement.Delete, L("Permission:FileManagement:File:Delete"));
}

private static LocalizableString L(string name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ namespace Lion.AbpPro.FileManagement.Permissions;

public class FileManagementPermissions
{
public const string GroupName = "AbpIdentity";
public const string GroupName = "FileManagement";

public static class FileManagement
{
public const string Default = GroupName + ".FileManagement";
public const string Default = GroupName + ".File";
public const string Upload = Default + ".Upload";
public const string Down = Default + ".Down";
public const string Download = Default + ".Download";
public const string Delete = Default + ".Delete";
}

public static string[] GetAll()
{
return ReflectionHelper.GetPublicConstantsRecursively(typeof(FileManagementPermissions));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ public class FileManagementApplicationAutoMapperProfile : Profile
public FileManagementApplicationAutoMapperProfile()
{
CreateMap<Files.FileObjectDto, PageFileObjectOutput>();
CreateMap<Files.FileObjectDto, GetFileObjectOutput>();
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
using Lion.AbpPro.FileManagement.Provider;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace Lion.AbpPro.FileManagement.Files;

/// <summary>
/// 文件
/// </summary>
[Authorize]
[Authorize(FileManagementPermissions.FileManagement.Default)]
public class FileAppService : ApplicationService, IFileAppService
{
private readonly FileObjectManager _fileObjectManager;
private readonly IFileProvider _fileProvider;
private readonly FileManager _fileManager;
private readonly IBlobContainer<AbpProFileManagementContainer> _blobContainer;

public FileAppService(FileObjectManager fileObjectManager, IFileProvider fileProvider)
public FileAppService(FileObjectManager fileObjectManager, IBlobContainer<AbpProFileManagementContainer> blobContainer, FileManager fileManager)
{
_fileObjectManager = fileObjectManager;
_fileProvider = fileProvider;
_blobContainer = blobContainer;
_fileManager = fileManager;
}

/// <summary>
Expand All @@ -34,71 +31,34 @@ public async Task<PagedResultDto<PageFileObjectOutput>> PageAsync(PageFileObject
return result;
}

public async Task<List<UploadOutput>> UploadAsync(List<IFormFile> files)
[Authorize(FileManagementPermissions.FileManagement.Upload)]
public async Task UploadAsync(List<IFormFile> files)
{
var result = new List<UploadOutput>();
foreach (var formFile in files)
{
try
{
var item = new UploadOutput();
// 获取文件的二进制数据
using var memoryStream = new MemoryStream();
await formFile.CopyToAsync(memoryStream);
var fileBytes = memoryStream.ToArray();

// 这里可以对 fileBytes 进行后续处理,例如保存到数据库或文件系统等
// 示例:将文件保存到数据库
var updateResult = await _fileProvider.UploadAsync(new UpdateDto()
{
FileName = formFile.FileName,
Bytes = fileBytes,
ContentType = formFile.ContentType,
FileSize = formFile.Length
});

result.Add(new UploadOutput()
{
Id = updateResult.Id,
Name = updateResult.FileName,
Path = updateResult.FilePath,
Success = true
});
}
catch (Exception e)
{
Logger.LogError(e, "上传文件失败");
result.Add(new UploadOutput()
{
Id = Guid.Empty,
Name = formFile.FileName,
Path = string.Empty,
Success = false
});
}
// 获取文件的二进制数据
using var memoryStream = new MemoryStream();
await formFile.CopyToAsync(memoryStream);
var fileBytes = memoryStream.ToArray();
await _fileManager.CreateAsync(GuidGenerator.Create(), formFile.FileName, formFile.Length, formFile.ContentType, fileBytes, true);
}

return result;
}


/// <summary>
/// 删除文件
/// </summary>
[Authorize(FileManagementPermissions.FileManagement.Delete)]
public Task DeleteAsync(DeleteFileObjectInput input)
{
return _fileObjectManager.DeleteAsync(input.Id);
}

public async Task<GetFileObjectOutput> GetAsync(GetFileObjectInput input)
{
var file = await _fileObjectManager.GetAsync(input.Id);
return ObjectMapper.Map<FileObjectDto, GetFileObjectOutput>(file);
return _fileManager.DeleteAsync(input.Id);
}

public async Task<FileContentResult> DownloadAsync(DownloadFileObjectInput input)
[Authorize(FileManagementPermissions.FileManagement.Download)]
public async Task<RemoteStreamContent> DownloadAsync(DownloadFileObjectInput input)
{
var file = await _fileObjectManager.GetAsync(input.Id);
return new Microsoft.AspNetCore.Mvc.FileContentResult(file.Bytes, file.ContentType);
var fileObject = await _fileObjectManager.GetAsync(input.Id);
var file = await _blobContainer.GetAsync(input.Id.ToString());
return new RemoteStreamContent(file, fileObject.FileName, fileObject.ContentType);
}
}
Loading

0 comments on commit 62fcb7c

Please sign in to comment.