You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
6.5 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using IBKLinker_Minio.Entity;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using Minio;
using System.Net.PeerToPeer;
using System.IO;
using System.Reactive.Linq;
using Minio.DataModel;
using System.Security.AccessControl;
using System.IO.Pipes;
using System.Net.Mime;
using IBKLinker_Minio.Entity.MinioController;
using SOA.Objects;
using SOA.Persistent;
namespace IBKLinker_Minio.Controller.MinioController
{
/// <summary>
/// 作者:孙亮
/// 编写时间2023-08-15
/// 编写内容mino上传接口
/// </summary>
[RoutePrefix("minio/minioapi")]
public class MinioApiController : ApiController
{
/// <summary>
/// 调用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;
}
}
}