Browse Source

fix(private-file): 用户目录可以自创建.

pull/413/head
cKey 4 years ago
parent
commit
a2174b963e
  1. 38
      aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.Application/LINGYUN/Abp/OssManagement/PrivateFileAppService.cs
  2. 2
      aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.Domain/LINGYUN/Abp/OssManagement/GetOssObjectRequest.cs
  3. 1
      aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.Domain/LINGYUN/Abp/OssManagement/GetOssObjectsRequest.cs
  4. 8
      aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.Domain/LINGYUN/Abp/OssManagement/IOssContainerExtensions.cs
  5. 8
      aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.FileSystem/LINGYUN/Abp/OssManagement/FileSystem/FileSystemOssContainer.cs

38
aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.Application/LINGYUN/Abp/OssManagement/PrivateFileAppService.cs

@ -1,4 +1,6 @@
using Microsoft.AspNetCore.Authorization; using LINGYUN.Abp.Features.LimitValidation;
using LINGYUN.Abp.OssManagement.Features;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Caching.Distributed;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -7,6 +9,7 @@ using System.Web;
using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Dtos;
using Volo.Abp.Caching; using Volo.Abp.Caching;
using Volo.Abp.Content; using Volo.Abp.Content;
using Volo.Abp.Features;
using Volo.Abp.IO; using Volo.Abp.IO;
using Volo.Abp.Users; using Volo.Abp.Users;
@ -37,15 +40,44 @@ namespace LINGYUN.Abp.OssManagement
} }
[Authorize] [Authorize]
[RequiresFeature(AbpOssManagementFeatureNames.OssObject.DownloadFile)]
[RequiresLimitFeature(
AbpOssManagementFeatureNames.OssObject.DownloadLimit,
AbpOssManagementFeatureNames.OssObject.DownloadInterval,
LimitPolicy.Month)]
public override async Task<IRemoteStreamContent> GetAsync(GetPublicFileInput input) public override async Task<IRemoteStreamContent> GetAsync(GetPublicFileInput input)
{ {
return await base.GetAsync(input); var ossObjectRequest = new GetOssObjectRequest(
GetCurrentBucket(),
// 需要处理特殊字符
HttpUtility.UrlDecode(input.Name),
GetCurrentPath(HttpUtility.UrlDecode(input.Path)),
HttpUtility.UrlDecode(input.Process))
{
MD5 = true,
// TODO: 用户访问自身目录可以实现无限制创建目录层级?
CreatePathIsNotExists = true,
};
var ossContainer = OssContainerFactory.Create();
var ossObject = await ossContainer.GetObjectAsync(ossObjectRequest);
return new RemoteStreamContent(ossObject.Content);
} }
[Authorize] [Authorize]
public override async Task<ListResultDto<OssObjectDto>> GetListAsync(GetFilesInput input) public override async Task<ListResultDto<OssObjectDto>> GetListAsync(GetFilesInput input)
{ {
return await base.GetListAsync(input); var ossContainer = OssContainerFactory.Create();
var response = await ossContainer.GetObjectsAsync(
GetCurrentBucket(),
GetCurrentPath(HttpUtility.UrlDecode(input.Path)),
skipCount: 0,
maxResultCount: input.MaxResultCount,
createPathIsNotExists: true); // TODO: 用户访问自身目录可以实现无限制创建目录层级?
return new ListResultDto<OssObjectDto>(
ObjectMapper.Map<List<OssObject>, List<OssObjectDto>>(response.Objects));
} }
[Authorize] [Authorize]

2
aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.Domain/LINGYUN/Abp/OssManagement/GetOssObjectRequest.cs

@ -14,6 +14,8 @@ namespace LINGYUN.Abp.OssManagement
/// </summary> /// </summary>
public string Process { get; } public string Process { get; }
public bool CreatePathIsNotExists { get; set; } = false;
public GetOssObjectRequest( public GetOssObjectRequest(
[NotNull] string bucket, [NotNull] string bucket,
[NotNull] string @object, [NotNull] string @object,

1
aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.Domain/LINGYUN/Abp/OssManagement/GetOssObjectsRequest.cs

@ -13,6 +13,7 @@ namespace LINGYUN.Abp.OssManagement
public int Current { get; } public int Current { get; }
public int? MaxKeys { get; } public int? MaxKeys { get; }
public bool MD5 { get; set; } public bool MD5 { get; set; }
public bool CreatePathIsNotExists { get; set; } = false;
public GetOssObjectsRequest( public GetOssObjectsRequest(
[NotNull] string bucketName, [NotNull] string bucketName,
string prefix = null, string prefix = null,

8
aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.Domain/LINGYUN/Abp/OssManagement/IOssContainerExtensions.cs

@ -59,12 +59,14 @@ namespace LINGYUN.Abp.OssManagement
string bucket, string bucket,
string @object, string @object,
string path = "", string path = "",
bool md5 = false) bool md5 = false,
bool createPathIsNotExists = false)
{ {
return await ossContainer.GetObjectAsync( return await ossContainer.GetObjectAsync(
new GetOssObjectRequest(bucket, @object, path) new GetOssObjectRequest(bucket, @object, path)
{ {
MD5 = md5, MD5 = md5,
CreatePathIsNotExists = createPathIsNotExists
}); });
} }
@ -77,12 +79,14 @@ namespace LINGYUN.Abp.OssManagement
string encodingType = null, string encodingType = null,
bool md5 = false, bool md5 = false,
int skipCount = 0, int skipCount = 0,
int maxResultCount = 10) int maxResultCount = 10,
bool createPathIsNotExists = false)
{ {
return await ossContainer.GetObjectsAsync( return await ossContainer.GetObjectsAsync(
new GetOssObjectsRequest(name, prefix, marker, delimiter, encodingType, skipCount, maxResultCount) new GetOssObjectsRequest(name, prefix, marker, delimiter, encodingType, skipCount, maxResultCount)
{ {
MD5 = md5, MD5 = md5,
CreatePathIsNotExists = createPathIsNotExists,
}); });
} }
} }

8
aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.FileSystem/LINGYUN/Abp/OssManagement/FileSystem/FileSystemOssContainer.cs

@ -264,11 +264,14 @@ namespace LINGYUN.Abp.OssManagement.FileSystem
var filePath = CalculateFilePath(request.Bucket, objectName); var filePath = CalculateFilePath(request.Bucket, objectName);
if (!File.Exists(filePath)) if (!File.Exists(filePath))
{ {
if (!Directory.Exists(filePath)) if (!Directory.Exists(filePath) && !request.CreatePathIsNotExists)
{ {
throw new BusinessException(code: OssManagementErrorCodes.ObjectNotFound); throw new BusinessException(code: OssManagementErrorCodes.ObjectNotFound);
// throw new ContainerNotFoundException($"Can't not found object {objectName} in container {request.Bucket} with file system"); // throw new ContainerNotFoundException($"Can't not found object {objectName} in container {request.Bucket} with file system");
} }
DirectoryHelper.CreateIfNotExists(filePath);
var directoryInfo = new DirectoryInfo(filePath); var directoryInfo = new DirectoryInfo(filePath);
var ossObject = new OssObject( var ossObject = new OssObject(
directoryInfo.Name.EnsureEndsWith('/'), directoryInfo.Name.EnsureEndsWith('/'),
@ -384,11 +387,12 @@ namespace LINGYUN.Abp.OssManagement.FileSystem
{ {
// 先定位检索的目录 // 先定位检索的目录
var filePath = CalculateFilePath(request.BucketName, request.Prefix); var filePath = CalculateFilePath(request.BucketName, request.Prefix);
if (!Directory.Exists(filePath)) if (!Directory.Exists(filePath) && !request.CreatePathIsNotExists)
{ {
throw new BusinessException(code: OssManagementErrorCodes.ContainerNotFound); throw new BusinessException(code: OssManagementErrorCodes.ContainerNotFound);
// throw new ContainerNotFoundException($"Can't not found container {request.BucketName} in file system"); // throw new ContainerNotFoundException($"Can't not found container {request.BucketName} in file system");
} }
DirectoryHelper.CreateIfNotExists(filePath);
// 目录也属于Oss对象,需要抽象的文件系统集合来存储 // 目录也属于Oss对象,需要抽象的文件系统集合来存储
var fileSystemNames = Directory.GetFileSystemEntries(filePath); var fileSystemNames = Directory.GetFileSystemEntries(filePath);
int maxFilesCount = fileSystemNames.Length; int maxFilesCount = fileSystemNames.Length;

Loading…
Cancel
Save