Compare commits

...

2 Commits

Author SHA1 Message Date
lgd 81d42f9f22 生成缩略图 2 months ago
lgd 91c3632040 生产缩略图 2 months ago

@ -6,6 +6,7 @@ using Minio;
using Minio.DataModel;
using Minio.DataModel.Args;
using Minio.Exceptions;
using System.IO;
namespace Infrastructure.CloudSdk.minio;
@ -241,4 +242,45 @@ public class MinioService
var url = await _minioClient.PresignedGetObjectAsync(args);
return url;
}
public async Task PutObjectAsync(string buketName,string tupianName,string objectName,MemoryStream memory) {
if (string.IsNullOrEmpty(buketName))
{
buketName = _bucketName;
}
PutObjectArgs args = new PutObjectArgs();
args.WithBucket(buketName).WithObject(objectName).WithStreamData(memory).WithObjectSize(memory.Length).WithContentType("image/jpeg");
await _minioClient.PutObjectAsync(args);
}
public async Task<Stream> GetObjectAsStream(string bucketName, string objectName)
{
if (string.IsNullOrEmpty(bucketName))
{
bucketName = _bucketName;
}
var memoryStream = new MemoryStream();
try
{
await _minioClient.GetObjectAsync(new GetObjectArgs()
.WithBucket(bucketName)
.WithObject(objectName)
.WithCallbackStream(stream =>
{
stream.CopyTo(memoryStream);
}));
memoryStream.Position = 0; // 重置流位置
return memoryStream;
}
catch (Exception e)
{
Console.WriteLine($"Error getting object: {e.Message}");
return null;
}
}
}

@ -13,8 +13,12 @@ using OpenAuth.App.ServiceApp;
using OpenAuth.Repository.Domain;
using OpenAuth.WebApi;
using Quartz;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using SqlSugar;
namespace OpenAuth.App.BaseApp.Subscribe;
public class ConfigSubscribe : IJob
@ -261,6 +265,34 @@ public class ConfigSubscribe : IJob
.Where(a => a.ObjectKey.Equals(objectKey)).SingleAsync();
if (mediaFile == null)
{
string objectKey1 = data.file.object_key.ToString();
string suoluokey = "minipic/" + data.file.name.ToString();
//缩略图处理
var imageStream = _minioService.GetObjectAsStream("", objectKey1);
var width = 0;
var height = 0;
using (var image = Image.Load(imageStream.Result))
{
width = image.Width;
height = image.Height;
var thumbnailSize = 100; // 缩略图大小,可以根据需要调整
var thumbnail = image.Clone(ctx => ctx.Resize(new ResizeOptions
{
Size = new Size(thumbnailSize, thumbnailSize),
Mode = ResizeMode.Crop // 根据需要选择裁剪或填充模式
}));
// 将缩略图保存到内存流中(为了上传)
using (var memoryStream = new MemoryStream())
{
thumbnail.SaveAsJpeg(memoryStream); // 可以根据需要选择不同的格式如Png, Bmp等。
memoryStream.Position = 0; // 重置流的位置到开始处
// 上传缩略图到MinIO
await _minioService.PutObjectAsync("", data.file.name.ToString(), suoluokey, memoryStream); // 根据实际格式修改Content-Type
}
}
var fileUpload = new LasaMediaFile()
{
Id = Guid.NewGuid().ToString(),
@ -283,7 +315,10 @@ public class ConfigSubscribe : IJob
ParentKey = folderKey[2],
Tid = result.tid,
Bid = result.bid,
FlightType = flightType
FlightType = flightType,
Width = width,
Height = height,
minipic = suoluokey
};
// todo 添加事务
await _sqlSugarClient.Insertable(fileUpload).ExecuteCommandAsync();

@ -116,4 +116,6 @@ public class LasaMediaFile
[SugarColumn(IsIgnore = true)]
public string PicLink { get; set; }
public string minipic { get; set; }
}

@ -13,6 +13,12 @@ using StackExchange.Redis;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.PixelFormats;
using Minio;
using Infrastructure.CloudSdk.minio;
namespace OpenAuth.WebApi.Controllers.ServiceControllers
{
@ -27,13 +33,17 @@ namespace OpenAuth.WebApi.Controllers.ServiceControllers
private readonly MqttClientManager _mqttClientManager;
private readonly MqttMessageCenter _mqttCenter;
private readonly RedisCacheContext _cache;
private readonly HttpClient _httpClient;
private readonly MinioService _minioService;
public AirportMaintenanceController(AirportMaintenanceApp app, MqttClientManager mqttClientManager, MqttMessageCenter mqttCenter, RedisCacheContext cache)
public AirportMaintenanceController(AirportMaintenanceApp app, MqttClientManager mqttClientManager, MqttMessageCenter mqttCenter, RedisCacheContext cache, HttpClient httpClient,MinioService minioService)
{
_app = app;
_mqttClientManager = mqttClientManager;
_mqttCenter = mqttCenter;
_cache = cache;
_httpClient = httpClient;
_minioService = minioService;
}
/// <summary>
/// 机场注册 注册码生成
@ -745,6 +755,45 @@ namespace OpenAuth.WebApi.Controllers.ServiceControllers
}
return result;
}
[HttpGet]
[AllowAnonymous]
public async Task<Response<string>> getMiniPic()
{
var result = new Response<string>();
try
{
// string presignedUrl = "http://175.27.168.120:6014/test/44fc8b4b-9448-4e79-a71d-536d094b8598/ad854032-d4b7-42dc-8ac0-d1faef0ebc1e/DJI_202507231000_002_ad854032-d4b7-42dc-8ac0-d1faef0ebc1e/DJI_20250723100110_0001_V.jpeg";
var imageStream = _minioService.GetObjectAsStream("test", "44fc8b4b-9448-4e79-a71d-536d094b8598/ad854032-d4b7-42dc-8ac0-d1faef0ebc1e/DJI_202507231000_002_ad854032-d4b7-42dc-8ac0-d1faef0ebc1e/DJI_20250723100110_0001_V.jpeg");
// var imageStream = await _httpClient.GetStreamAsync(presignedUrl);
using (var image = Image.Load(imageStream.Result))
{
var width = image.Width;
var height = image.Height;
var thumbnailSize = 100; // 缩略图大小,可以根据需要调整
var thumbnail = image.Clone(ctx => ctx.Resize(new ResizeOptions
{
Size = new Size(thumbnailSize, thumbnailSize),
Mode = ResizeMode.Crop // 根据需要选择裁剪或填充模式
}));
// 将缩略图保存到内存流中(为了上传)
using (var memoryStream = new MemoryStream())
{
thumbnail.SaveAsJpeg(memoryStream); // 可以根据需要选择不同的格式如Png, Bmp等。
memoryStream.Position = 0; // 重置流的位置到开始处
// 上传缩略图到MinIO
await _minioService.PutObjectAsync("test" , "suolue_DJI_20250723100110_0001_V.jpeg", "abc/suolue_DJI_20250723100110_0001_V.jpeg", memoryStream); // 根据实际格式修改Content-Type
}
}
}
catch (Exception ex)
{
result.Code = 500;
result.Message = ex.Message;
}
return result;
}
}
}

Loading…
Cancel
Save