增加文件上传后台,上传,下载,预览,移动功能

master
孙亮 12 months ago
parent 44cae6eb46
commit dbbaaad852

@ -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
{
/// <summary>
/// 调用log日志文件接口
/// 调用log日志文件
/// </summary>
private static readonly ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
/// <summary>
/// minio数据库创建文件夹接口
/// </summary>
[HttpPost, Route("CreateFolder")]
public RequestEntity CreateFolder(List<CreateFolderInputModel> cert)
{
RequestEntity result = new RequestEntity();
try
{
using (IDbContext db = ObjectContainer.GetObject<IDbContext>("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;
}
/// <summary>
/// minio数据库删除文件夹接口
/// </summary>
[HttpPost, Route("RemoveFolder")]
public RequestEntity RemoveFolder(List<RemoveFolderInputModel> 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;
}
/// <summary>
/// minio数据库查询文件夹接口
/// </summary>
[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<string>();
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;
}
/// <summary>
/// minio数据库上传文件接口
/// </summary>
[HttpPost, Route("FileUpload")]
public RequestEntity FileUpload(List<FileUploadInputModel> 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权限表
/// <summary>
@ -712,28 +586,97 @@ namespace IBKLinker_Minio.Controller.MinioController
/// </summary>
/// <param name="minioStoragePath">minio文件文件夹路径表</param>
[HttpPost, Route("AddStoragePath")]
public RequestEntity AddStoragePath(MinioStoragePath minioStoragePath)
public RequestEntity AddStoragePath(List<MinioStoragePath> minioStoragePath)
{
RequestEntity request = new RequestEntity();
try
using (IDbContext db = ObjectContainer.GetObject<IDbContext>("db"))
{
///创建数据库连接上下文对象
using (IDbContext db = ObjectContainer.GetObject<IDbContext>("db"))
{
///拿到表自增长的值赋值给自增长主键pk
minioStoragePath.Pk = getPk("S_MINIO_STORAGE_PATH", "MINIO_STORAGE_PATH");
///给创建时间赋值
minioStoragePath.CreatorTime = DateTime.Now;
///把需要添加的数据同步到数据库
db.Insert<MinioStoragePath>(minioStoragePath);
request.code = "0";
try
{
MinioClient minio = new MinioClient("172.15.88.212:9000", "minioadmin", "minioadmin");//连接所需minio所在地址、登录名、密码
string buckname = "dayetegang";//桶名称
List<MinioStoragePath> date = new List<MinioStoragePath>();
///创建数据库连接上下文对象
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<MinioStoragePath>("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<MinioStoragePath>(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<MinioStoragePath>(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<MinioStoragePath>(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_PATHminio文件文件夹路径表添加数据失败错误原因:{0}", ex);
}
}
catch (Exception ex)
{
request.code = "1";
logger.ErrorFormat("主表:MINIO_STORAGE_PATHminio文件文件夹路径表添加数据失败错误原因:{0}", ex);
}
return request;
}
@ -785,23 +728,32 @@ namespace IBKLinker_Minio.Controller.MinioController
public RequestEntity RemoveStoragePath(IList<MinioStoragePath> master)
{
RequestEntity request = new RequestEntity();
try
using (IDbContext db = ObjectContainer.GetObject<IDbContext>("db"))
{
using (IDbContext db = ObjectContainer.GetObject<IDbContext>("db"))
try
{
MinioClient minio = new MinioClient("172.15.88.212:9000", "minioadmin", "minioadmin");
string buckname = "dayetegang";//桶名称
db.BeginTransaction();
db.Delete<MinioStoragePath>(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<MinioStoragePath> GetMenuTree(string PK = "")
{
List<MinioStoragePath> date = new List<MinioStoragePath>();
using (IDbContext db = ObjectContainer.GetObject<IDbContext>("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<StoragePath>();
List<MinioStoragePath> 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> minioStoragePath)
{
RequestEntity request = new RequestEntity();
MinioStoragePath data = new MinioStoragePath();
using (IDbContext db = ObjectContainer.GetObject<IDbContext>("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 公用方法
/// <summary>
/// 矫正pk值
@ -1002,6 +1020,35 @@ namespace IBKLinker_Minio.Controller.MinioController
return pk_TableCurrentMax;
}
}
/// <summary>
/// 编写内容:下载文件转换流数据方法
/// </summary>
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
}

@ -37,9 +37,29 @@ namespace IBKLinker_Minio.Entity.MinioController
public class CreateFolderInputModel
{
/// <summary>
/// 路径
/// 子文件夹的父ID
/// </summary>
public string Path { get; set; }
public int Upk { get; set; }
/// <summary>
/// 文件名称
/// </summary>
public string FileName { get; set; }
/// <summary>
/// 文件类型
/// </summary>
public int FileType { get; set; }
/// <summary>
/// 文件路径
/// </summary>
public string FilePath { get; set; }
/// <summary>
/// 创建者
/// </summary>
public string CreatorUser { get; set; }
}
@ -60,4 +80,21 @@ namespace IBKLinker_Minio.Entity.MinioController
/// </summary>
public string Type { get; set; }
}
/// <summary>
/// 文件下载前端发送model
/// </summary>
public class DownloadFileModel
{
/// <summary>
/// 路径
/// </summary>
public string FileName { get; set; }
/// <summary>
/// 删除类型 0文件夹删除 1文件删除
/// </summary>
public string Pk { get; set; }
}
}

@ -45,7 +45,7 @@ namespace IBKLinker_Minio.Entity.MinioController
///文件路径
/// </summary>
[Persistent("FILE_PATH"), DisplayName("文件路径")]
public string FilePate { get; set; }
public string FilePath { get; set; }
/// <summary>
@ -96,6 +96,22 @@ namespace IBKLinker_Minio.Entity.MinioController
[Persistent("ALTERNATE_FIELDS"), DisplayName("备用字段")]
public string AlternateFields { get; set; }
/// <summary>
///文件格式
/// </summary>
[Persistent(false), DisplayName("")]
public string FileCategory { get; set; }
/// <summary>
///流数据bate64
/// </summary>
[Persistent(false), DisplayName("")]
public string Bate64 { get; set; }
/// <summary>
///操作类型 0下载 1预览 2打印
/// </summary>
[Persistent(false), DisplayName("")]
public string OperationType { get; set; }
}
}

Loading…
Cancel
Save