Browse Source

新增文件删除接口

zhontai 2 years ago
parent
commit
c99bb6585e

+ 1 - 1
src/hosts/ZhonTai.Host/Configs/ossconfig.json

@@ -13,7 +13,7 @@
       "IsEnableCache": true,
       "BucketName": "admin",
       "Url": "", //文件外链
-      "Enable": true,
+      "Enable": false,
       "Md5":  false
     },
     //阿里云

+ 15 - 0
src/platform/ZhonTai.Admin/Services/File/Dto/FileDeleteInput.cs

@@ -0,0 +1,15 @@
+
+using System.ComponentModel.DataAnnotations;
+using ZhonTai.Admin.Core.Validators;
+
+namespace ZhonTai.Admin.Services.File.Dto;
+
+public class FileDeleteInput
+{
+    /// <summary>
+    /// 文件Id
+    /// </summary>
+    [Required]
+    [ValidateRequired("请选择文件")]
+    public long Id { get; set; }
+}

+ 10 - 0
src/platform/ZhonTai.Admin/Services/File/Dto/FileGetPageOutput.cs

@@ -4,6 +4,11 @@ namespace ZhonTai.Admin.Services.File.Dto;
 
 public class FileGetPageOutput
 {
+    /// <summary>
+    /// 文件Id
+    /// </summary>
+    public long Id { get; set; }
+
     /// <summary>
     /// OSS供应商
     /// </summary>
@@ -14,6 +19,11 @@ public class FileGetPageOutput
     /// </summary>
     public string BucketName { get; set; }
 
+    /// <summary>
+    /// 文件目录
+    /// </summary>
+    public string FileDirectory { get; set; }
+
     /// <summary>
     /// 文件Guid
     /// </summary>

+ 33 - 4
src/platform/ZhonTai.Admin/Services/File/FileService.cs

@@ -64,6 +64,35 @@ public class FileService : BaseService, IFileService, IDynamicApi
         return data;
     }
 
+    /// <summary>
+    /// 删除文件
+    /// </summary>
+    /// <param name="input"></param>
+    /// <returns></returns>
+    [HttpPost]
+    public async Task DeleteAsync(FileDeleteInput input)
+    {
+        var file = await _fileRepository.GetAsync(input.Id);
+        if (file == null)
+        {
+            return;
+        }
+
+        var shareFile = await _fileRepository.Where(a=>a.Id != input.Id && a.FileGuid == file.FileGuid).AnyAsync();
+        if (!shareFile)
+        {
+            var oSSService = _oSSServiceFactory.Create(file.Provider.ToString());
+            var oSSOptions = _oSSConfig.OSSConfigs.Where(a => a.Enable && a.Provider == file.Provider).FirstOrDefault();
+            if (oSSOptions.Enable)
+            {
+                var filePath = Path.Combine(file.FileDirectory, file.FileGuid + file.Extension).ToPath();
+                await oSSService.RemoveObjectAsync(file.BucketName, filePath);
+            }
+        }
+
+        await _fileRepository.DeleteAsync(file.Id);
+    }
+
     /// <summary>
     /// 上传文件
     /// </summary>
@@ -84,16 +113,16 @@ public class FileService : BaseService, IFileService, IDynamicApi
             {
                 var sameFileEntity = new FileEntity
                 {
-                    Provider = oSSOptions.Provider,
-                    BucketName = oSSOptions.BucketName,
-                    FileGuid = FreeUtil.NewMongodbId(),
+                    Provider = md5FileEntity.Provider,
+                    BucketName = md5FileEntity.BucketName,
+                    FileGuid = md5FileEntity.FileGuid,
                     FileName = Path.GetFileNameWithoutExtension(file.FileName),
                     Extension = Path.GetExtension(file.FileName).ToLower(),
                     FileDirectory = md5FileEntity.FileDirectory,
                     Size = md5FileEntity.Size,
                     SizeFormat = md5FileEntity.SizeFormat,
+                    LinkUrl = md5FileEntity.LinkUrl,
                     Md5 = md5,
-                    LinkUrl = md5FileEntity.LinkUrl
                 };
                 sameFileEntity = await _fileRepository.InsertAsync(sameFileEntity);
                 return sameFileEntity;

+ 2 - 0
src/platform/ZhonTai.Admin/Services/File/IFileService.cs

@@ -16,6 +16,8 @@ public interface IFileService
 {
     Task<PageOutput<FileGetPageOutput>> GetPageAsync(PageInput<FileGetPageDto> input);
 
+    Task DeleteAsync(FileDeleteInput input);
+
     Task<FileEntity> UploadFileAsync(IFormFile file, string fileDirectory = "");
 
     Task<List<FileEntity>> UploadFilesAsync([Required] IFormFileCollection files, string fileDirectory = "");

+ 5 - 0
src/platform/ZhonTai.Admin/Services/Permission/PermissionService.cs

@@ -259,6 +259,11 @@ public class PermissionService : BaseService, IPermissionService, IDynamicApi
     {
         var entity = Mapper.Map<PermissionEntity>(input);
         entity.Type = PermissionType.Dot;
+        if (entity.Sort == 0)
+        {
+            var sort = await _permissionRepository.Select.Where(a => a.ParentId == input.ParentId).MaxAsync(a => a.Sort);
+            entity.Sort = sort + 1;
+        }
         await _permissionRepository.InsertAsync(entity);
 
         if (input.ApiIds != null && input.ApiIds.Any())

+ 22 - 0
src/platform/ZhonTai.Admin/ZhonTai.Admin.xml

@@ -4269,6 +4269,16 @@
             文档接口
             </summary>
         </member>
+        <member name="P:ZhonTai.Admin.Services.File.Dto.FileDeleteInput.Id">
+            <summary>
+            文件Id
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Services.File.Dto.FileGetPageOutput.Id">
+            <summary>
+            文件Id
+            </summary>
+        </member>
         <member name="P:ZhonTai.Admin.Services.File.Dto.FileGetPageOutput.ProviderName">
             <summary>
             OSS供应商
@@ -4279,6 +4289,11 @@
             存储桶名称
             </summary>
         </member>
+        <member name="P:ZhonTai.Admin.Services.File.Dto.FileGetPageOutput.FileDirectory">
+            <summary>
+            文件目录
+            </summary>
+        </member>
         <member name="P:ZhonTai.Admin.Services.File.Dto.FileGetPageOutput.FileGuid">
             <summary>
             文件Guid
@@ -4336,6 +4351,13 @@
             <param name="input"></param>
             <returns></returns>
         </member>
+        <member name="M:ZhonTai.Admin.Services.File.FileService.DeleteAsync(ZhonTai.Admin.Services.File.Dto.FileDeleteInput)">
+            <summary>
+            删除文件
+            </summary>
+            <param name="input"></param>
+            <returns></returns>
+        </member>
         <member name="M:ZhonTai.Admin.Services.File.FileService.UploadFileAsync(Microsoft.AspNetCore.Http.IFormFile,System.String)">
             <summary>
             上传文件