添加上传图片相关逻辑

This commit is contained in:
huangxianguo 2024-07-17 14:22:08 +08:00
parent 2b4e179cf9
commit 30d8d953a0
7 changed files with 200 additions and 9 deletions

View File

@ -0,0 +1,20 @@
using MasstransferInfrastructure.Database.Sqlite;
namespace MasstransferExporter.DataExporter;
/// <summary>
/// 坐标信息业务类
/// </summary>
public class CoordinateService
{
private static readonly SqliteHelper Db = SqliteHelper.GetInstance();
/// <summary>
/// 坐标信息导出
/// </summary>
public static void CoordinateExporter()
{
}
}

View File

@ -0,0 +1,161 @@
using System.Drawing;
using MasstransferCommon.Model.Constant;
using MasstransferCommunicate.Minio;
using MasstransferCommunicate.Mqtt.Client;
using MasstransferExporter.DataExporter.Model;
using MasstransferExporter.ImageExporter.Model;
using MasstransferInfrastructure.Database.Sqlite;
namespace MasstransferExporter.ImageExporter;
public class ImageService
{
private static readonly MinioHelper Minio = MinioHelper.GetInstance();
private static readonly SqliteHelper Helper = SqliteHelper.GetInstance();
private const string BasePath = "masstransfer";
private static SystemParams? GetSystemParams()
{
return Helper.Query<SystemParams>("select * from system_params limit 1")
.FirstOrDefault();
}
/// <summary>
/// 对图片生产缩略图
/// </summary>
private static void GenerateThumbnail(DirectoryInfo dir)
{
// 获取文件夹下所有文件的信息
var fs = dir.GetFiles();
if (fs != null && fs.Length > 0)
{
try
{
foreach (var file in fs)
{
if (file.Name.EndsWith(".bmp") && !file.Name.Contains("_thumbnail"))
{
// 判断是否已经存在缩略图,如果存在的话,则跳过生成过程
var thumbnailFilePath = file.FullName.Replace(".bmp", "_thumbnail.bmp");
if (!File.Exists(thumbnailFilePath))
{
GenerateThumbnail(file.FullName);
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
// 递归遍历当前目录下的所有文件夹
foreach (var subdir in dir.GetDirectories()) GenerateThumbnail(subdir);
}
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="filePath"></param>
static void GenerateThumbnail(string filePath)
{
using (var image = Image.FromFile(filePath))
{
var thumbnailWidth = 410;
var thumbnailHeight = 300;
var thumbnail = image.GetThumbnailImage(thumbnailWidth, thumbnailHeight, () => false, IntPtr.Zero);
var directory = Path.GetDirectoryName(filePath);
var fileNameWithoutExt = Path.GetFileNameWithoutExtension(filePath);
var extension = Path.GetExtension(filePath);
var newFileName = $"{fileNameWithoutExt}_thumbnail{extension}";
var newFilePath = Path.Combine(directory, newFileName);
thumbnail.Save(newFilePath);
}
}
/// <summary>
/// 上传图片
/// </summary>
public static async void ImageExporter()
{
var yesterday = DateTime.Today.AddDays(-1);
await ImageExporter(yesterday);
}
/// <summary>
/// 导出图片文件
/// </summary>
/// <param name="dateTime"></param>
/// <param name="isThumbnail"></param>
private static async Task ImageExporter(DateTime dateTime, bool isThumbnail = true)
{
try
{
var systemParams = GetSystemParams();
var imageStoragePath =
systemParams!.ImageStoragePath ?? Environment.GetEnvironmentVariable("USERPROFILE");
var date = dateTime.ToString("yyyyMMdd");
var dir = Path.Combine(imageStoragePath, BasePath, date);
if (!Directory.Exists(dir)) return;
var dirInfo = new DirectoryInfo(dir);
// 对图片进行压缩
GenerateThumbnail(dirInfo);
// 文件保存路径的规则是 {user BasePath}/masstransfer/images/{yyyyMMdd}/{batchCode}/wafer/{waferCode}/{timestamp}.bmp
var files = dirInfo.GetDirectories();
foreach (var batchDir in files)
{
var types = batchDir.GetDirectories();
foreach (var type in types)
{
var codeDirs = type.GetDirectories();
foreach (var codeDir in codeDirs)
{
var images = codeDir.GetFiles();
images = isThumbnail
? images.Where(x => x.Name.EndsWith("_thumbnail.bmp")).ToArray()
: images.Where(x => !x.Name.EndsWith("_thumbnail.bmp")).ToArray();
foreach (var image in images)
{
var fileName = $"123456/{codeDir.Name}/{batchDir.Name}/{type.Name}/{image.Name}";
var path = image.FullName;
await Minio.UploadFileAsync("image", fileName, path);
}
var data = new ImageExportData()
{
BatchNumber = batchDir.Name,
BizNumber = codeDir.Name,
ImageType = type.Name,
PicType = isThumbnail ? "thumbnail" : "raw"
};
// 上传完成后发布通知
await MessageQueueHelper.Publish(Topics.ImageUpload, data);
}
}
}
}
catch (Exception e)
{
Console.WriteLine($"图片导出失败,{e}");
}
}
}

View File

@ -0,0 +1,12 @@
namespace MasstransferExporter.ImageExporter.Model;
public class ImageExportData
{
public string PicType { get; set; }
public string BizNumber { get; set; }
public string BatchNumber { get; set; }
public string ImageType { get; set; }
}

View File

@ -32,7 +32,7 @@ public class LogFileExporter
foreach (var file in files)
{
var fileName = $"123456/{TimeUtil.FormatTime(yesterday, "yyyy-MM-dd")}/{level}/{file.Name}";
var fileName = $"123456/{TimeUtil.FormatTime(yesterday, "yyyyMMdd")}/{level}/{file.Name}";
await _minio.UploadFileAsync("log", fileName, file.FullName);
}

View File

@ -15,6 +15,7 @@
<PackageReference Include="Serilog" Version="4.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="System.Diagnostics.Process" Version="4.3.0" />
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
<PackageReference Include="System.Management" Version="8.0.0" />
</ItemGroup>
@ -35,8 +36,4 @@
<None Remove="obj\**" />
</ItemGroup>
<ItemGroup>
<Folder Include="ImageExporter\" />
</ItemGroup>
</Project>

View File

@ -1,6 +1,5 @@
using MasstransferCommunicate.Mqtt.Client;
using MasstransferExporter.DataExporter;
using MasstransferExporter.Stat;
using MasstransferExporter.ImageExporter;
using MasstransferInfrastructure.Mqtt.Model;
class Program
@ -16,7 +15,7 @@ class Program
await MessageQueueHelper.InitConnect(options);
HeartbeatExporter.HeartBeat();
ImageService.ImageExporter();
Console.WriteLine("按任意键退出");

View File

@ -60,11 +60,13 @@ public class MinioHelper
await _client.MakeBucketAsync(new MakeBucketArgs().WithBucket(bucketName));
}
await _client.PutObjectAsync(new PutObjectArgs()
var response = await _client.PutObjectAsync(new PutObjectArgs()
.WithBucket(bucketName)
.WithObject(fileName)
.WithFileName(filePath));
Console.WriteLine(response);
return $"{bucketName}/{fileName}";
}