2 changed files with 290 additions and 144 deletions
@ -1,144 +1,144 @@ |
|||||
using LINGYUN.Abp.OssManagement.Permissions; |
using LINGYUN.Abp.OssManagement.Permissions; |
||||
using Microsoft.AspNetCore.Authorization; |
using Microsoft.AspNetCore.Authorization; |
||||
using Microsoft.AspNetCore.Mvc; |
using Microsoft.AspNetCore.Mvc; |
||||
using System; |
using System; |
||||
using System.Collections.Generic; |
using System.Collections.Generic; |
||||
using System.ComponentModel.DataAnnotations; |
using System.ComponentModel.DataAnnotations; |
||||
using System.IO; |
using System.IO; |
||||
using System.Linq; |
using System.Linq; |
||||
using System.Threading.Tasks; |
using System.Threading.Tasks; |
||||
using Volo.Abp; |
using Volo.Abp; |
||||
using Volo.Abp.AspNetCore.Mvc; |
using Volo.Abp.AspNetCore.Mvc; |
||||
using Volo.Abp.Auditing; |
using Volo.Abp.Auditing; |
||||
using Volo.Abp.IO; |
using Volo.Abp.IO; |
||||
using Volo.Abp.Validation; |
using Volo.Abp.Validation; |
||||
|
|
||||
namespace LINGYUN.Abp.OssManagement |
namespace LINGYUN.Abp.OssManagement |
||||
{ |
{ |
||||
[RemoteService(Name = OssManagementRemoteServiceConsts.RemoteServiceName)] |
[RemoteService(Name = OssManagementRemoteServiceConsts.RemoteServiceName)] |
||||
[Area("oss-management")] |
[Area("oss-management")] |
||||
[Route("api/oss-management/objects")] |
[Route("api/oss-management/objects")] |
||||
public class OssObjectController : AbpController, IOssObjectAppService |
public class OssObjectController : AbpController, IOssObjectAppService |
||||
{ |
{ |
||||
protected IFileValidater FileValidater { get; } |
protected IFileValidater FileValidater { get; } |
||||
protected IOssObjectAppService OssObjectAppService { get; } |
protected IOssObjectAppService OssObjectAppService { get; } |
||||
|
|
||||
public OssObjectController( |
public OssObjectController( |
||||
IFileValidater fileValidater, |
IFileValidater fileValidater, |
||||
IOssObjectAppService ossObjectAppService) |
IOssObjectAppService ossObjectAppService) |
||||
{ |
{ |
||||
FileValidater = fileValidater; |
FileValidater = fileValidater; |
||||
OssObjectAppService = ossObjectAppService; |
OssObjectAppService = ossObjectAppService; |
||||
} |
} |
||||
|
|
||||
[HttpPost] |
[HttpPost] |
||||
public virtual async Task<OssObjectDto> CreateAsync(CreateOssObjectInput input) |
public virtual async Task<OssObjectDto> CreateAsync(CreateOssObjectInput input) |
||||
{ |
{ |
||||
return await OssObjectAppService.CreateAsync(input); |
return await OssObjectAppService.CreateAsync(input); |
||||
} |
} |
||||
|
|
||||
[HttpPost] |
[HttpPost] |
||||
[Route("upload")] |
[Route("upload")] |
||||
[DisableAuditing] |
[DisableAuditing] |
||||
[Authorize(AbpOssManagementPermissions.OssObject.Create)] |
[Authorize(AbpOssManagementPermissions.OssObject.Create)] |
||||
public virtual async Task UploadAsync([FromForm] UploadOssObjectInput input) |
public virtual async Task UploadAsync([FromForm] UploadOssObjectInput input) |
||||
{ |
{ |
||||
await FileValidater.ValidationAsync(input); |
await FileValidater.ValidationAsync(input); |
||||
// 以上传的文件名创建一个临时目录
|
// 以上传的文件名创建一个临时目录
|
||||
var tempFilePath = Path.Combine( |
var tempFilePath = Path.Combine( |
||||
Path.GetTempPath(), |
Path.GetTempPath(), |
||||
"lingyun-abp-application", |
"lingyun-abp-application", |
||||
"upload-tmp", |
"upload-tmp", |
||||
string.Concat(input.Path ?? "", input.FileName).ToMd5()); |
string.Concat(input.Path ?? "", input.FileName).ToMd5()); |
||||
DirectoryHelper.CreateIfNotExists(tempFilePath); |
DirectoryHelper.CreateIfNotExists(tempFilePath); |
||||
// 以上传的分片索引创建临时文件
|
// 以上传的分片索引创建临时文件
|
||||
var tempSavedFile = Path.Combine(tempFilePath, $"{input.ChunkNumber}.uploadtmp"); |
var tempSavedFile = Path.Combine(tempFilePath, $"{input.ChunkNumber}.uploadtmp"); |
||||
try |
try |
||||
{ |
{ |
||||
if (HttpContext.RequestAborted.IsCancellationRequested) |
if (HttpContext.RequestAborted.IsCancellationRequested) |
||||
{ |
{ |
||||
// 如果取消请求,删除临时目录
|
// 如果取消请求,删除临时目录
|
||||
Directory.Delete(tempFilePath, true); |
Directory.Delete(tempFilePath, true); |
||||
return; |
return; |
||||
} |
} |
||||
|
|
||||
if (input.File != null) |
if (input.File != null) |
||||
{ |
{ |
||||
// 保存临时文件
|
// 保存临时文件
|
||||
using (var fs = new FileStream(tempSavedFile, FileMode.Create, FileAccess.Write)) |
using (var fs = new FileStream(tempSavedFile, FileMode.Create, FileAccess.Write)) |
||||
{ |
{ |
||||
// 写入当前分片文件
|
// 写入当前分片文件
|
||||
await input.File.CopyToAsync(fs); |
await input.File.CopyToAsync(fs); |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
if (input.ChunkNumber == input.TotalChunks) |
if (input.ChunkNumber == input.TotalChunks) |
||||
{ |
{ |
||||
var createOssObjectInput = new CreateOssObjectInput |
var createOssObjectInput = new CreateOssObjectInput |
||||
{ |
{ |
||||
Bucket = input.Bucket, |
Bucket = input.Bucket, |
||||
Path = input.Path, |
Path = input.Path, |
||||
Object = input.FileName |
Object = input.FileName |
||||
}; |
}; |
||||
// 合并文件
|
// 合并文件
|
||||
var mergeSavedFile = Path.Combine(tempFilePath, $"{input.FileName}"); |
var mergeSavedFile = Path.Combine(tempFilePath, $"{input.FileName}"); |
||||
// 获取并排序所有分片文件
|
// 获取并排序所有分片文件
|
||||
var mergeFiles = Directory.GetFiles(tempFilePath).OrderBy(f => f.Length).ThenBy(f => f); |
var mergeFiles = Directory.GetFiles(tempFilePath).OrderBy(f => f.Length).ThenBy(f => f); |
||||
// 创建临时合并文件
|
// 创建临时合并文件
|
||||
using (var memoryStream = new MemoryStream()) |
using (var memoryStream = new MemoryStream()) |
||||
{ |
{ |
||||
foreach (var mergeFile in mergeFiles) |
foreach (var mergeFile in mergeFiles) |
||||
{ |
{ |
||||
// 读取当前文件字节
|
// 读取当前文件字节
|
||||
var mergeFileBytes = await FileHelper.ReadAllBytesAsync(mergeFile); |
var mergeFileBytes = await FileHelper.ReadAllBytesAsync(mergeFile); |
||||
// 写入到合并文件流
|
// 写入到合并文件流
|
||||
await memoryStream.WriteAsync(mergeFileBytes, HttpContext.RequestAborted); |
await memoryStream.WriteAsync(mergeFileBytes, HttpContext.RequestAborted); |
||||
Array.Clear(mergeFileBytes, 0, mergeFileBytes.Length); |
Array.Clear(mergeFileBytes, 0, mergeFileBytes.Length); |
||||
// 删除已参与合并的临时文件分片
|
// 删除已参与合并的临时文件分片
|
||||
FileHelper.DeleteIfExists(mergeFile); |
FileHelper.DeleteIfExists(mergeFile); |
||||
} |
} |
||||
createOssObjectInput.SetContent(memoryStream); |
createOssObjectInput.SetContent(memoryStream); |
||||
|
|
||||
await OssObjectAppService.CreateAsync(createOssObjectInput); |
await OssObjectAppService.CreateAsync(createOssObjectInput); |
||||
// 文件保存之后删除临时文件目录
|
// 文件保存之后删除临时文件目录
|
||||
Directory.Delete(tempFilePath, true); |
Directory.Delete(tempFilePath, true); |
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
catch |
catch |
||||
{ |
{ |
||||
// 发生异常删除临时文件目录
|
// 发生异常删除临时文件目录
|
||||
Directory.Delete(tempFilePath, true); |
Directory.Delete(tempFilePath, true); |
||||
throw; |
throw; |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
[HttpDelete] |
[HttpDelete] |
||||
[Route("bulk-delete")] |
[Route("bulk-delete")] |
||||
public virtual async Task BulkDeleteAsync([FromBody] BulkDeleteOssObjectInput input) |
public virtual async Task BulkDeleteAsync([FromBody] BulkDeleteOssObjectInput input) |
||||
{ |
{ |
||||
await OssObjectAppService.BulkDeleteAsync(input); |
await OssObjectAppService.BulkDeleteAsync(input); |
||||
} |
} |
||||
|
|
||||
[HttpDelete] |
[HttpDelete] |
||||
public virtual async Task DeleteAsync(GetOssObjectInput input) |
public virtual async Task DeleteAsync(GetOssObjectInput input) |
||||
{ |
{ |
||||
await OssObjectAppService.DeleteAsync(input); |
await OssObjectAppService.DeleteAsync(input); |
||||
} |
} |
||||
|
|
||||
[HttpGet] |
[HttpGet] |
||||
public virtual async Task<OssObjectDto> GetAsync(GetOssObjectInput input) |
public virtual async Task<OssObjectDto> GetAsync(GetOssObjectInput input) |
||||
{ |
{ |
||||
return await OssObjectAppService.GetAsync(input); |
return await OssObjectAppService.GetAsync(input); |
||||
} |
} |
||||
|
|
||||
private static void ThrowValidationException(string message, string memberName) |
private static void ThrowValidationException(string message, string memberName) |
||||
{ |
{ |
||||
throw new AbpValidationException(message, |
throw new AbpValidationException(message, |
||||
new List<ValidationResult> |
new List<ValidationResult> |
||||
{ |
{ |
||||
new ValidationResult(message, new[] {memberName}) |
new ValidationResult(message, new[] {memberName}) |
||||
}); |
}); |
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
|
|||||
Loading…
Reference in new issue