Browse Source

feat(data-seed): preset menu data with object storage and multiple languages

pull/310/head
cKey 4 years ago
parent
commit
9a20d116d1
  1. 288
      aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.HttpApi/LINGYUN/Abp/OssManagement/OssObjectController.cs
  2. 146
      aspnet-core/services/platform/LINGYUN.Platform.HttpApi.Host/DataSeeder/VbenAdminDataSeedContributor.cs

288
aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.HttpApi/LINGYUN/Abp/OssManagement/OssObjectController.cs

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

146
aspnet-core/services/platform/LINGYUN.Platform.HttpApi.Host/DataSeeder/VbenAdminDataSeedContributor.cs

@ -60,6 +60,10 @@ namespace LINGYUN.Platform.DataSeeder
await SeedPlatformMenuAsync(layout, layoutData);
// 网关菜单
await SeedApiGatewayMenuAsync(layout, layoutData);
// 多语言菜单
await SeedLocalizationMenuAsync(layout, layoutData);
// 对象存储菜单
await SeedOssManagementMenuAsync(layout, layoutData);
}
}
@ -849,6 +853,148 @@ namespace LINGYUN.Platform.DataSeeder
new string[] { "vben-admin" });
}
private async Task SeedLocalizationMenuAsync(Layout layout, Data data)
{
var localization = await SeedMenuAsync(
layout, //layout
data, //data
"Localization", //name
"/localization", //path
CodeNumberGenerator.CreateCode(26), //code
layout.Path, //component
"Localization", //displayName
"", //redirect
"Localization", //description
null, //parentId
layout.TenantId, //tenantId
new Dictionary<string, object>() //meta
{
{ "title", "Localization" },
{ "icon", "ant-design:translation-outlined" },
{ "hideTab", false },
{ "ignoreAuth", false },
},
new string[] { "vben-admin" });
var languages = await SeedMenuAsync(
layout, //layout
data, //data
"Languages", //name
"/localization/languages", //path
CodeNumberGenerator.AppendCode(localization.Code, CodeNumberGenerator.CreateCode(1)), //code
"/localization/languages/index", //component
"Languages", //displayName
"", //redirect
"Languages", //description
localization.Id, //parentId
layout.TenantId, //tenantId
new Dictionary<string, object>() //meta
{
{ "title", "Languages" },
{ "icon", "" },
{ "hideTab", false },
},
new string[] { "vben-admin" });
var resources = await SeedMenuAsync(
layout, //layout
data, //data
"Resources", //name
"/localization/resources", //path
CodeNumberGenerator.AppendCode(localization.Code, CodeNumberGenerator.CreateCode(2)), //code
"/localization/resources/index", //component
"Resources", //displayName
"", //redirect
"Resources", //description
localization.Id, //parentId
layout.TenantId, //tenantId
new Dictionary<string, object>() //meta
{
{ "title", "Resources" },
{ "icon", "" },
{ "hideTab", false },
},
new string[] { "vben-admin" });
var texts = await SeedMenuAsync(
layout, //layout
data, //data
"Texts", //name
"/localization/texts", //path
CodeNumberGenerator.AppendCode(localization.Code, CodeNumberGenerator.CreateCode(3)), //code
"/localization/texts/index", //component
"Texts", //displayName
"", //redirect
"Texts", //description
localization.Id, //parentId
layout.TenantId, //tenantId
new Dictionary<string, object>() //meta
{
{ "title", "Texts" },
{ "icon", "" },
{ "hideTab", false },
},
new string[] { "vben-admin" });
}
private async Task SeedOssManagementMenuAsync(Layout layout, Data data)
{
var oss = await SeedMenuAsync(
layout, //layout
data, //data
"OssManagement", //name
"/oss", //path
CodeNumberGenerator.CreateCode(27), //code
layout.Path, //component
"Oss Management", //displayName
"", //redirect
"Oss Management", //description
null, //parentId
layout.TenantId, //tenantId
new Dictionary<string, object>() //meta
{
{ "title", "Oss Management" },
{ "icon", "ant-design:file-twotone" },
{ "hideTab", false },
{ "ignoreAuth", false },
},
new string[] { "vben-admin" });
var containers = await SeedMenuAsync(
layout, //layout
data, //data
"Containers", //name
"/oss/containers", //path
CodeNumberGenerator.AppendCode(oss.Code, CodeNumberGenerator.CreateCode(1)), //code
"/oss-management/containers/index", //component
"Containers", //displayName
"", //redirect
"Containers", //description
oss.Id, //parentId
layout.TenantId, //tenantId
new Dictionary<string, object>() //meta
{
{ "title", "Containers" },
{ "icon", "" },
{ "hideTab", false },
},
new string[] { "vben-admin" });
var objects = await SeedMenuAsync(
layout, //layout
data, //data
"Objects", //name
"/oss/objects", //path
CodeNumberGenerator.AppendCode(oss.Code, CodeNumberGenerator.CreateCode(2)), //code
"/oss-management/objects/index", //component
"Objects", //displayName
"", //redirect
"Objects", //description
oss.Id, //parentId
layout.TenantId, //tenantId
new Dictionary<string, object>() //meta
{
{ "title", "Objects" },
{ "icon", "" },
{ "hideTab", false },
},
new string[] { "vben-admin" });
}
private async Task<Menu> SeedMenuAsync(
Layout layout,

Loading…
Cancel
Save