From b943a516e1b0692a6a77f0ae1725cbf7bc919737 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E7=A0=81=E5=86=9C?= <599854767@qq.com>
Date: Wed, 23 Mar 2022 10:22:25 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=96=87=E4=BB=B6=E4=B8=8A?=
=?UTF-8?q?=E4=BC=A0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Controllers/CommonController.cs | 47 +++-------------
ZR.Service/System/IService/ISysFileService.cs | 12 ++--
ZR.Service/System/SysFileService.cs | 56 ++++++++++++++-----
ZR.Vue/src/views/tool/file/index.vue | 31 +++++-----
4 files changed, 76 insertions(+), 70 deletions(-)
diff --git a/ZR.Admin.WebApi/Controllers/CommonController.cs b/ZR.Admin.WebApi/Controllers/CommonController.cs
index 28daeb1..9abc60c 100644
--- a/ZR.Admin.WebApi/Controllers/CommonController.cs
+++ b/ZR.Admin.WebApi/Controllers/CommonController.cs
@@ -39,16 +39,6 @@ namespace ZR.Admin.WebApi.Controllers
OptionsSetting = options.Value;
}
- ///
- /// 心跳
- ///
- ///
- [HttpGet]
- public IActionResult Health()
- {
- return SUCCESS(true);
- }
-
///
/// hello
///
@@ -106,39 +96,16 @@ namespace ZR.Admin.WebApi.Controllers
[HttpPost()]
[Verify]
[ActionPermissionFilter(Permission = "common")]
- public IActionResult UploadFile([FromForm(Name = "file")] IFormFile formFile, string fileName = "", string fileDir = "uploads", int uploadType = 0)
+ public async Task UploadFile([FromForm(Name = "file")] IFormFile formFile, string fileName = "", string fileDir = "uploads", int uploadType = 0)
{
if (formFile == null) throw new CustomException(ResultCode.PARAM_ERROR, "上传文件不能为空");
- string fileExt = Path.GetExtension(formFile.FileName);
- string hashFileName = FileUtil.HashFileName();
- fileName = (fileName.IsEmpty() ? hashFileName : fileName) + fileExt;
- fileDir = fileDir.IsEmpty() ? "uploads" : fileDir;
- string filePath = FileUtil.GetdirPath(fileDir);
- string finalFilePath = Path.Combine(WebHostEnvironment.WebRootPath, filePath, fileName);
- double fileSize = formFile.Length / 1024.0;
- if (!Directory.Exists(Path.GetDirectoryName(finalFilePath)))
- {
- Directory.CreateDirectory(Path.GetDirectoryName(finalFilePath));
- }
-
- using (var stream = new FileStream(finalFilePath, FileMode.Create))
- {
- formFile.CopyTo(stream);
- }
-
- string accessPath = string.Concat(OptionsSetting.Upload.UploadUrl, "/", filePath.Replace("\\", "/"), "/", fileName);
- SysFile file = new(formFile.FileName, fileName, fileExt, fileSize + "kb", filePath, accessPath, HttpContext.GetName())
- {
- StoreType = (int)Infrastructure.Enums.StoreType.LOCAL,
- FileType = formFile.ContentType
- };
- long fileId = SysFileService.InsertFile(file);
+ SysFile file = await SysFileService.SaveFileLocal(WebHostEnvironment.WebRootPath, fileName, fileDir, HttpContext.GetName(), formFile);
return SUCCESS(new
{
- url = uploadType == 1 ? finalFilePath : accessPath,
+ url = uploadType == 1 ? file.FileUrl : file.AccessUrl,
fileName,
- fileId = fileId.ToString()
+ fileId = file.Id.ToString()
});
}
@@ -174,7 +141,11 @@ namespace ZR.Admin.WebApi.Controllers
{
result = SysFileService.SaveFile(fileDir, formFile, fileName, "");
});
- long id = SysFileService.InsertFile(new(formFile.FileName, fileName, fileExt, fileSize + "kb", "", result.Item2, HttpContext.GetName())
+ if (!result.Item1)
+ {
+ return ToResponse(ApiResult.Error("阿里云连接失败"));
+ }
+ long id = await SysFileService.InsertFile(new(formFile.FileName, fileName, fileExt, fileSize + "kb", "", result.Item2, HttpContext.GetName())
{
StoreType = (int)Infrastructure.Enums.StoreType.ALIYUN,
FileType = formFile.ContentType
diff --git a/ZR.Service/System/IService/ISysFileService.cs b/ZR.Service/System/IService/ISysFileService.cs
index 62b4f6e..aa40fee 100644
--- a/ZR.Service/System/IService/ISysFileService.cs
+++ b/ZR.Service/System/IService/ISysFileService.cs
@@ -1,5 +1,6 @@
using Infrastructure.Attribute;
using Microsoft.AspNetCore.Http;
+using System.Threading.Tasks;
using ZR.Model.Models;
using ZR.Model.System;
@@ -7,15 +8,18 @@ namespace ZR.Service.System.IService
{
public interface ISysFileService : IBaseService
{
- long InsertFile(SysFile file);
+ Task InsertFile(SysFile file);
///
/// 上传文件
///
- ///
+ ///
+ ///
///
- /// 结果、地址、文件名
- (bool, string, string) SaveFile(string picdir, IFormFile formFile);
+ ///
+ ///
+ /// 文件对象
+ Task SaveFileLocal(string rootPath, string fileName, string fileDir, string userName, IFormFile formFile);
(bool, string, string) SaveFile(string picdir, IFormFile formFile, string customFileName, string bucketName);
///
/// 按时间来创建文件夹
diff --git a/ZR.Service/System/SysFileService.cs b/ZR.Service/System/SysFileService.cs
index e3991b1..7f3c1ab 100644
--- a/ZR.Service/System/SysFileService.cs
+++ b/ZR.Service/System/SysFileService.cs
@@ -11,6 +11,8 @@ using System.Net;
using ZR.Model.System;
using ZR.Repository.System;
using Infrastructure.Extensions;
+using System.Threading.Tasks;
+using Microsoft.Extensions.Options;
namespace ZR.Service.System
{
@@ -22,26 +24,50 @@ namespace ZR.Service.System
{
private string domainUrl = AppSettings.GetConfig("ALIYUN_OSS:domainUrl");
private readonly SysFileRepository SysFileRepository;
-
- public SysFileService(SysFileRepository repository)
+ private OptionsSetting OptionsSetting;
+ public SysFileService(SysFileRepository repository, IOptions options)
{
SysFileRepository = repository;
+ OptionsSetting = options.Value;
+ }
+
+ ///
+ /// 存储本地
+ ///
+ ///
+ public async Task SaveFileLocal(string rootPath, string fileName, string fileDir, string userName, IFormFile formFile)
+ {
+ string fileExt = Path.GetExtension(formFile.FileName);
+ string hashFileName = FileUtil.HashFileName();
+ fileName = (fileName.IsEmpty() ? hashFileName : fileName) + fileExt;
+ fileDir = fileDir.IsEmpty() ? "uploads" : fileDir;
+ string filePath = FileUtil.GetdirPath(fileDir);
+ string finalFilePath = Path.Combine(rootPath, filePath, fileName);
+ double fileSize = Math.Round(formFile.Length / 1024.0, 2);
+
+ if (!Directory.Exists(Path.GetDirectoryName(finalFilePath)))
+ {
+ Directory.CreateDirectory(Path.GetDirectoryName(finalFilePath));
+ }
+
+ using (var stream = new FileStream(finalFilePath, FileMode.Create))
+ {
+ await formFile.CopyToAsync(stream);
+ }
+ string accessPath = string.Concat(OptionsSetting.Upload.UploadUrl, "/", filePath.Replace("\\", "/"), "/", fileName);
+ SysFile file = new(formFile.FileName, fileName, fileExt, fileSize + "kb", filePath, accessPath, userName)
+ {
+ StoreType = (int)Infrastructure.Enums.StoreType.LOCAL,
+ FileType = formFile.ContentType,
+ FileUrl = finalFilePath
+ };
+ file.Id = await InsertFile(file);
+ return file;
}
///
/// 上传文件到阿里云
///
- ///
- ///
- ///
- public (bool, string, string) SaveFile(string picdir, IFormFile formFile)
- {
- return SaveFile(picdir, formFile, "", "");
- }
-
- ///
- /// 存储文件
- ///
/// 文件夹
///
/// 自定义文件名
@@ -87,11 +113,11 @@ namespace ZR.Service.System
return BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(str)), 4, 8).Replace("-", "");
}
- public long InsertFile(SysFile file)
+ public Task InsertFile(SysFile file)
{
try
{
- return Insertable(file).ExecuteReturnSnowflakeId();//单条插入返回雪花ID;
+ return Insertable(file).ExecuteReturnSnowflakeIdAsync();//单条插入返回雪花ID;
}
catch (Exception ex)
{
diff --git a/ZR.Vue/src/views/tool/file/index.vue b/ZR.Vue/src/views/tool/file/index.vue
index c061328..0deb2db 100644
--- a/ZR.Vue/src/views/tool/file/index.vue
+++ b/ZR.Vue/src/views/tool/file/index.vue
@@ -73,7 +73,7 @@
-
+
{{item.dictLabel}}
@@ -113,16 +113,18 @@
{{formView.id}}
- {{formView.fileType}}
+ {{formView.realName}}
+
+
+
+ {{formView.fileType}}
+
{{formView.fileExt}}
-
- {{formView.realName}}
-
{{formView.fileName}}
@@ -230,6 +232,17 @@ export default {
// 列表数据查询
this.getList()
},
+ watch: {
+ 'form.storeType': {
+ handler: function(val) {
+ if (val == 1) {
+ this.uploadUrl = '/common/uploadFile'
+ } else if (val == 2) {
+ this.uploadUrl = '/common/UploadFileAliyun'
+ }
+ }
+ }
+ },
methods: {
// 查询数据
getList() {
@@ -326,14 +339,6 @@ export default {
storeTypeFormat(row, column) {
return this.selectDictLabel(this.storeTypeOptions, row.storeType)
},
- handleSelectStore(val) {
- this.queryParams.storeType = val
- if (val == 1) {
- this.uploadUrl = '/common/uploadFile'
- } else if (val == 2) {
- this.uploadUrl = '/common/UploadFileAliyun'
- }
- },
/** 复制代码成功 */
clipboardSuccess() {
this.msgSuccess('复制成功')