From dbbaaad852480ff41dc40c55f364c0c9d86755b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E4=BA=AE?= <593170460@qq.com> Date: Mon, 9 Oct 2023 14:36:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=96=87=E4=BB=B6=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E5=90=8E=E5=8F=B0=EF=BC=8C=E4=B8=8A=E4=BC=A0=EF=BC=8C?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=EF=BC=8C=E9=A2=84=E8=A7=88=EF=BC=8C=E7=A7=BB?= =?UTF-8?q?=E5=8A=A8=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MinioController/MinioApiController.cs | 381 ++++++++++-------- .../MinioControllerInputModel.cs | 41 +- .../MinioController/MinioStoragePath.cs | 18 +- 3 files changed, 270 insertions(+), 170 deletions(-) diff --git a/IBKLinker-Minio/Controller/MinioController/MinioApiController.cs b/IBKLinker-Minio/Controller/MinioController/MinioApiController.cs index 98e6afc..b2ca7a2 100644 --- a/IBKLinker-Minio/Controller/MinioController/MinioApiController.cs +++ b/IBKLinker-Minio/Controller/MinioController/MinioApiController.cs @@ -18,6 +18,15 @@ using System.Net.Mime; using IBKLinker_Minio.Entity.MinioController; using SOA.Objects; using SOA.Persistent; +using System.Web.Http.Results; +using System.Runtime.InteropServices.ComTypes; +using System.Xml; +using Swashbuckle.Swagger; +using System.Runtime.ConstrainedExecution; +using System.Net.Http; +using System.Web.UI.WebControls; +using System.Runtime.Remoting.Contexts; +using System.Runtime.InteropServices; namespace IBKLinker_Minio.Controller.MinioController { @@ -30,145 +39,10 @@ namespace IBKLinker_Minio.Controller.MinioController public class MinioApiController : ApiController { /// - /// 调用log日志文件接口 + /// 调用log日志文件 /// private static readonly ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); - /// - /// minio数据库创建文件夹接口 - /// - [HttpPost, Route("CreateFolder")] - public RequestEntity CreateFolder(List cert) - { - RequestEntity result = new RequestEntity(); - try - { - using (IDbContext db = ObjectContainer.GetObject("db")) - { - #region minio创建文件夹 - MinioClient minio = new MinioClient("172.15.88.212:9000", "minioadmin", "minioadmin");//连接所需minio所在地址、登录名、密码 - //取前端发送路径(或单一文件夹名称) - var folderName = cert[0].Path + "/"; - //桶名称 - string buckname = "dayetegang"; - minio.PutObjectAsync(buckname, folderName, new MemoryStream(), 0, "application/octet-stream"); - #endregion - - result.code = "0"; - result.msg = "文件夹创建成功"; - } - } - catch (Exception ex) - { - result.msg = ex.ToString(); - result.code = "1"; - logger.InfoFormat(ex.ToString()); - } - return result; - } - /// - /// minio数据库删除文件夹接口 - /// - [HttpPost, Route("RemoveFolder")] - public RequestEntity RemoveFolder(List cert) - { - RequestEntity result = new RequestEntity(); - try - { - #region minio删除文件夹 - MinioClient minio = new MinioClient("172.15.88.212:9000", "minioadmin", "minioadmin"); - string buckname = "dayetegang";//桶名称 - string state = "";//提示状态 - var folderName = ""; //文件或文件夹 - if (cert[0].Type.ToString() == "0")//判断是删除文件加还是文件 0为文件夹 1为文件 - { - folderName = cert[0].Path.ToString() + "/";//文件夹后缀需要加上/ - state = "文件夹"; - } - else - { - folderName = cert[0].Path.ToString();//文件不需要后缀/ - state = "文件"; - } - minio.RemoveObjectAsync(buckname, folderName); - #endregion - result.code = "0"; - result.msg = state + "删除成功!"; - } - catch (Exception ex) - { - result.msg = ex.ToString(); - result.code = "1"; - logger.InfoFormat(ex.ToString()); - } - return result; - } - /// - /// minio数据库查询文件夹接口 - /// - [HttpPost, Route("QueryFolder")] - public RequestEntity QueryFolder(string fileneme) - { - RequestEntity result = new RequestEntity(); - try - { - #region minio查询文件夹 - var minio = new MinioClient("127.0.0.1:9000", "minioadmin", "minioadmin"); - //var folderName = fileneme + "/"; - string bucketName = "dayetegang"; - // 获取存储桶中的所有对象 - var objects = minio.ListObjectsAsync(bucketName, null, true); - // 存储文件夹名称的集合 - var folders = new HashSet(); - var stream = new MemoryStream(); - // 设置链接的有效期(以秒为单位) - int expiresInt = 3600; // 1小时 - // 设置响应内容类型为 "application/pdf" - var presignedUrl = minio.PresignedGetObjectAsync(bucketName, fileneme, expiresInt: expiresInt); - #endregion - result.code = "0"; - result.msg = "文件夹删除成功"; - } - catch (Exception ex) - { - result.msg = ex.ToString(); - result.code = "1"; - logger.InfoFormat(ex.ToString()); - } - return result; - } - /// - /// minio数据库上传文件接口 - /// - [HttpPost, Route("FileUpload")] - public RequestEntity FileUpload(List cert) - { - RequestEntity result = new RequestEntity(); - try - { - var minio = new MinioClient("172.15.88.212:9000", "minioadmin", "minioadmin"); - string bucketName = "dayetegang"; - int a = 0; - for (int i = 0; i < cert[0].FileData.Length; i++) - { - var contentType = "application/" + cert[0].FileType[i].ToString(); - string strBase64 = cert[0].FileData[i].Split(',')[1];//取文件base64流数据 - byte[] bt = Convert.FromBase64String(strBase64);//获取html base64 - minio.PutObjectAsync(bucketName, cert[0].StoragePath.ToString(), new MemoryStream(bt), bt.Length, contentType); - a++; - } - result.code = "0"; - result.msg = "共计成功上传" + a + "个文件!"; - - } - catch (Exception ex) - { - result.code = "1"; - result.msg = ex.ToString(); - } - return result; - } - #region minio权限表 /// @@ -712,28 +586,97 @@ namespace IBKLinker_Minio.Controller.MinioController /// /// minio文件文件夹路径表 [HttpPost, Route("AddStoragePath")] - public RequestEntity AddStoragePath(MinioStoragePath minioStoragePath) + public RequestEntity AddStoragePath(List minioStoragePath) { RequestEntity request = new RequestEntity(); - try + using (IDbContext db = ObjectContainer.GetObject("db")) { - ///创建数据库连接上下文对象 - using (IDbContext db = ObjectContainer.GetObject("db")) - { - ///拿到表自增长的值赋值给自增长主键pk - minioStoragePath.Pk = getPk("S_MINIO_STORAGE_PATH", "MINIO_STORAGE_PATH"); - ///给创建时间赋值 - minioStoragePath.CreatorTime = DateTime.Now; - ///把需要添加的数据同步到数据库 - db.Insert(minioStoragePath); - request.code = "0"; + try + { + MinioClient minio = new MinioClient("172.15.88.212:9000", "minioadmin", "minioadmin");//连接所需minio所在地址、登录名、密码 + string buckname = "dayetegang";//桶名称 + List date = new List(); + ///创建数据库连接上下文对象 + db.BeginTransaction();//启动事务 + if (minioStoragePath != null) + { + foreach (var item in minioStoragePath) + { + MinioStoragePath data = new MinioStoragePath(); + if (item.FileType == 0) //如果是文件夹 + { + if (item.Upk > 0)//判断如果创建的是根目录文件夹前端不需要对父级id传值 + { + date = db.Query("select * from MINIO_STORAGE_PATH where pk='" + minioStoragePath[0].Upk + "' ORDER BY PK").ToList(); + if (date.Count > 0) + { + data.Upk = minioStoragePath[0].Upk;//子文件夹的父级id + data.FileName = minioStoragePath[0].FileName;//文件名称 + data.FileType = minioStoragePath[0].FileType;//文件类型 + data.FilePath = minioStoragePath[0].FilePath;//文件路径 + data.CreatorUser = minioStoragePath[0].CreatorUser;//创建者 + data.CreatorTime = DateTime.Now;//创建时间 + data.FileLevel = date[0].FileLevel + 1;//层级 + data.Pk = Convert.ToInt32(db.QueryScalar("select S_MINIO_STORAGE_PATH.nextval from dual"));///拿到表自增长的值赋值给自增长主键pk + db.Insert(data); + } + } + else + { + data.Pk = Convert.ToInt32(db.QueryScalar("select S_MINIO_STORAGE_PATH.nextval from dual"));///拿到表自增长的值赋值给自增长主键pk + data.Upk = 0;//子文件夹的父级id(如果是根目录默认父级id为0) + data.FileName = minioStoragePath[0].FileName;//文件名称 + data.FileType = minioStoragePath[0].FileType;//文件类型 + data.FilePath = minioStoragePath[0].FilePath;//文件路径 + data.CreatorUser = minioStoragePath[0].CreatorUser;//创建者 + data.CreatorTime = DateTime.Now;//创建时间 + data.FileLevel = 1;//层级 + db.Insert(data); + } + #region minio创建文件夹 + //var folderName = minioStoragePath[0].FilePath + "/";//取前端发送路径(或单一文件夹名称) + //minio.PutObjectAsync(buckname, folderName, new MemoryStream(), 0, "application/octet-stream"); + #endregion + request.msg = "文件夹创建成功!"; + } + if (item.FileType == 1)//如果是文件 + { + var contentType = "application/" + item.FileCategory; + string strBase64 = item.Bate64;//取文件base64流数据 + byte[] bt = Convert.FromBase64String(strBase64);//获取html base64 + var uploadTask = minio.PutObjectAsync(buckname, "/" + item.FileName, new MemoryStream(bt), bt.Length, contentType);//上传文件到minio数据库 + uploadTask.Wait(); + if (uploadTask.Status == TaskStatus.RanToCompletion && !uploadTask.IsFaulted)//判断是否上传成功 + { + data.Pk = Convert.ToInt32(db.QueryScalar("select S_MINIO_STORAGE_PATH.nextval from dual"));///拿到表自增长的值赋值给自增长主键pk + data.Upk = item.Upk;//子文件夹的父级id(如果是根目录默认父级id为0) + data.FileName = item.FileName;//文件名称 + data.FileType = item.FileType;//文件类型 + data.FilePath = item.FilePath;//文件路径 + data.CreatorUser = item.CreatorUser;//创建者 + data.CreatorTime = DateTime.Now;//创建时间 + data.FileLevel = item.FileLevel + 1;//层级 + db.Insert(data); + request.msg = "文件上传成功!"; + request.code = "0"; + } + else + { + request.msg = "文件上传失败,请检查文件是否正确!"; + request.code = "0"; + } + } + } + db.Commit(); + } + } + catch (Exception ex) + { + db.Rollback(); + request.code = "1"; + logger.ErrorFormat("主表:MINIO_STORAGE_PATH,minio文件文件夹路径表添加数据失败,错误原因:{0}", ex); } - } - catch (Exception ex) - { - request.code = "1"; - logger.ErrorFormat("主表:MINIO_STORAGE_PATH,minio文件文件夹路径表添加数据失败,错误原因:{0}", ex); } return request; } @@ -785,23 +728,32 @@ namespace IBKLinker_Minio.Controller.MinioController public RequestEntity RemoveStoragePath(IList master) { RequestEntity request = new RequestEntity(); - try + using (IDbContext db = ObjectContainer.GetObject("db")) { - using (IDbContext db = ObjectContainer.GetObject("db")) + try { + MinioClient minio = new MinioClient("172.15.88.212:9000", "minioadmin", "minioadmin"); + string buckname = "dayetegang";//桶名称 db.BeginTransaction(); - db.Delete(master); - - db.Commit(); + foreach (var item in master) + { + if (item.FileType == 1) //如果类型是文件 + { + minio.RemoveObjectAsync(buckname, item.FileName);//minio操作删除文件 + string sql = string.Format(" delete from MINIO_FILEASSOCIATION where FILE_PK like'%{0}%' or ASSOCIATION_FILE_PK like'%{0}%'", item.Pk);//删除关联表中的关联关系 + db.Execute(sql); + } + } request.code = "0"; - } - } - catch (Exception ex) - { - request.code = "1"; + db.Commit(); - logger.ErrorFormat("主表:MINIO_ROLE_PERMISSION,创建数据库连接对象失败,错误原因:{0}", ex); + } + catch (Exception ex) + { + request.code = "1"; + logger.ErrorFormat("主表:MINIO_ROLE_PERMISSION,创建数据库连接对象失败,错误原因:{0}", ex); + } } return request; } @@ -849,7 +801,6 @@ namespace IBKLinker_Minio.Controller.MinioController [HttpPost, Route("GetMenuTree")] public List GetMenuTree(string PK = "") { - List date = new List(); using (IDbContext db = ObjectContainer.GetObject("db")) { @@ -893,7 +844,7 @@ namespace IBKLinker_Minio.Controller.MinioController RequstStoragePath model = new RequstStoragePath(); model.Pk = item.Pk; model.FileName = item.FileName; - model.FilePate = item.FilePate; + model.FilePate = item.FilePath; model.FileType = item.FileType; model.requstStoragePaths = new List(); List ds = GetMenuTree(item.Pk.ToString()); @@ -902,7 +853,7 @@ namespace IBKLinker_Minio.Controller.MinioController StoragePath models = new StoragePath(); models.Pk = items.Pk; models.FileName = items.FileName; - models.FilePate = items.FilePate; + models.FilePate = items.FilePath; models.FileType = items.FileType; model.requstStoragePaths.Add(models); } @@ -969,6 +920,73 @@ namespace IBKLinker_Minio.Controller.MinioController return request; } #endregion + + #region minio文件下载及预览 + [HttpPost, Route("FileDownload")] + public RequestEntity FileDownload(List minioStoragePath) + { + RequestEntity request = new RequestEntity(); + MinioStoragePath data = new MinioStoragePath(); + using (IDbContext db = ObjectContainer.GetObject("db")) + { + try + { + if (minioStoragePath != null) + { + foreach (var item in minioStoragePath) + { + var minio = new MinioClient("172.15.88.212:9000", "minioadmin", "minioadmin"); + string bucketName = "dayetegang"; + string downloadpath = "D:/linshi"; + if (System.IO.Directory.Exists(@downloadpath) == false)//如果不存在就创建文件夹 + { + System.IO.Directory.CreateDirectory(@downloadpath); + } + minio.GetObjectAsync(bucketName, item.FileName, downloadpath +"/"+ item.FileName);//先将minio数据库中的文件存储到本地 + bool fileExist = File.Exists(downloadpath + "/" + item.FileName);//判断是否已经存储到本地文件夹 + if (fileExist)//如果本地存在 + { + data.Bate64 = Base64(downloadpath + "/" + item.FileName); + data.FileName = item.FileName; + if (data.Bate64 != null) + { + File.Delete(downloadpath + "/" + item.FileName);//删除暂存文件 + } + if (item.OperationType =="0") //操作类型为下载 + { + string sql = "update MINIO_STORAGE_PATH set DOWNLOAD_NUM='" + item.DownloadNum + 1 + "' where pk='" + item.Pk + "'"; + db.Execute(sql); + } + request.code = "0"; + } + else //如果不存在 + { + request.msg = "请联系管理员检查!"; + request.code = "1"; + } + } + if (data != null) + { + request.data = data; + } + } + else + { + request.msg = "请联系管理员检查!"; + request.code = "1"; + } + + } + catch (Exception ex) + { + request.msg = "请联系管理员检查!"; + request.code = "1"; + logger.InfoFormat(ex.ToString()); + } + } + return request; + } + #endregion #region 公用方法 /// /// 矫正pk值 @@ -1002,6 +1020,35 @@ namespace IBKLinker_Minio.Controller.MinioController return pk_TableCurrentMax; } } + /// + /// 编写内容:下载文件转换流数据方法 + /// + public string Base64(string FilePath) + { + string data = ""; + try + { + using (MemoryStream msReader = new MemoryStream()) + { + using (FileStream fs = new FileStream(FilePath, FileMode.Open)) + { + byte[] buffer = new byte[1024]; + int readLen = 0; + while ((readLen = fs.Read(buffer, 0, buffer.Length)) > 0) + { + msReader.Write(buffer, 0, readLen); + } + } + data = Convert.ToBase64String(msReader.ToArray());//转换base64 + } + return data;//返回值 + } + catch (Exception ex) + { + logger.InfoFormat(ex.ToString()); + throw; + } + } #endregion } diff --git a/IBKLinker-Minio/Entity/MinioController/MinioControllerInputModel.cs b/IBKLinker-Minio/Entity/MinioController/MinioControllerInputModel.cs index 286b9bb..da5a73a 100644 --- a/IBKLinker-Minio/Entity/MinioController/MinioControllerInputModel.cs +++ b/IBKLinker-Minio/Entity/MinioController/MinioControllerInputModel.cs @@ -37,9 +37,29 @@ namespace IBKLinker_Minio.Entity.MinioController public class CreateFolderInputModel { /// - /// 路径 + /// 子文件夹的父ID /// - public string Path { get; set; } + public int Upk { get; set; } + + /// + /// 文件名称 + /// + public string FileName { get; set; } + + /// + /// 文件类型 + /// + public int FileType { get; set; } + + /// + /// 文件路径 + /// + public string FilePath { get; set; } + + /// + /// 创建者 + /// + public string CreatorUser { get; set; } } @@ -60,4 +80,21 @@ namespace IBKLinker_Minio.Entity.MinioController /// public string Type { get; set; } } + + /// + /// 文件下载前端发送model + /// + public class DownloadFileModel + { + /// + /// 路径 + /// + public string FileName { get; set; } + + + /// + /// 删除类型 0文件夹删除 1文件删除 + /// + public string Pk { get; set; } + } } diff --git a/IBKLinker-Minio/Entity/MinioController/MinioStoragePath.cs b/IBKLinker-Minio/Entity/MinioController/MinioStoragePath.cs index dfeed67..321ff25 100644 --- a/IBKLinker-Minio/Entity/MinioController/MinioStoragePath.cs +++ b/IBKLinker-Minio/Entity/MinioController/MinioStoragePath.cs @@ -45,7 +45,7 @@ namespace IBKLinker_Minio.Entity.MinioController ///文件路径 /// [Persistent("FILE_PATH"), DisplayName("文件路径")] - public string FilePate { get; set; } + public string FilePath { get; set; } /// @@ -96,6 +96,22 @@ namespace IBKLinker_Minio.Entity.MinioController [Persistent("ALTERNATE_FIELDS"), DisplayName("备用字段")] public string AlternateFields { get; set; } + /// + ///文件格式 + /// + [Persistent(false), DisplayName("")] + public string FileCategory { get; set; } + /// + ///流数据bate64 + /// + [Persistent(false), DisplayName("")] + public string Bate64 { get; set; } + + /// + ///操作类型 0下载 1预览 2打印 + /// + [Persistent(false), DisplayName("")] + public string OperationType { get; set; } } }