From e86dcb6c3c9cdc444248495c2ec1ac0dc5a3966a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E4=BC=9F?= <421281095@qq.com> Date: Fri, 8 Aug 2025 09:00:47 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E7=BC=A9=E7=95=A5=E5=9B=BE=E6=8F=90?= =?UTF-8?q?=E5=8F=96=E5=B9=B6=E4=B8=8A=E4=BC=A0=202.=20=E6=8F=90=E5=8F=96?= =?UTF-8?q?=E7=84=A6=E8=B7=9D=E3=80=81=E5=88=86=E8=BE=A8=E7=8E=87=E3=80=81?= =?UTF-8?q?=E5=A4=A7=E5=B0=8F=E7=AD=89=E4=BF=A1=E6=81=AF=203.=20=E7=9C=81?= =?UTF-8?q?=E5=AF=B9=E6=8E=A5=E7=9B=B8=E5=85=B3=E9=83=A8=E5=88=86=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Response/CommandPictureResponse.cs | 4 +- OpenAuth.App/ServiceApp/ManageApp.cs | 2 +- .../ServiceApp/Subscribe/ConfigSubscribe.cs | 119 +++++++++++++++++- OpenAuth.Repository/Domain/LasaMediaFile.cs | 8 +- OpenAuth.WebApi/boot/TestAbc.cs | 50 +++++++- 5 files changed, 168 insertions(+), 15 deletions(-) diff --git a/OpenAuth.App/ServiceApp/FlyTask/Response/CommandPictureResponse.cs b/OpenAuth.App/ServiceApp/FlyTask/Response/CommandPictureResponse.cs index cf47822..5e99f50 100644 --- a/OpenAuth.App/ServiceApp/FlyTask/Response/CommandPictureResponse.cs +++ b/OpenAuth.App/ServiceApp/FlyTask/Response/CommandPictureResponse.cs @@ -16,10 +16,10 @@ public class ExtData public int pitch { get; set; } public int roll { get; set; } public DateTime? time { get; set; } - public double height { get; set; } + public double? height { get; set; } public long imgWidth { get; set; } public long imgHeight { get; set; } public long imgOriginWidth { get; set; } public long imgOriginHeight { get; set; } - public double psjj { get; set; } + public double? psjj { get; set; } } \ No newline at end of file diff --git a/OpenAuth.App/ServiceApp/ManageApp.cs b/OpenAuth.App/ServiceApp/ManageApp.cs index 2c82bee..9445a87 100644 --- a/OpenAuth.App/ServiceApp/ManageApp.cs +++ b/OpenAuth.App/ServiceApp/ManageApp.cs @@ -1737,7 +1737,7 @@ namespace OpenAuth.App.ServiceApp imgWidth = dimensions.width, imgOriginHeight = dimensions.height, imgOriginWidth = dimensions.width, - psjj = 24, // 等效焦距 todo 等效焦距如何取 + psjj = lasaMediaFile.FocalLength, time = lasaMediaFile.CreateTime, height = lasaMediaFile.Height, lon = lasaMediaFile.Lng, diff --git a/OpenAuth.App/ServiceApp/Subscribe/ConfigSubscribe.cs b/OpenAuth.App/ServiceApp/Subscribe/ConfigSubscribe.cs index 5e2ec85..3aaed41 100644 --- a/OpenAuth.App/ServiceApp/Subscribe/ConfigSubscribe.cs +++ b/OpenAuth.App/ServiceApp/Subscribe/ConfigSubscribe.cs @@ -5,6 +5,9 @@ using Infrastructure.CloudSdk; using Infrastructure.CloudSdk.minio; using Infrastructure.CloudSdk.wayline; using Infrastructure.Extensions; +using Irony.Parsing.Construction; +using MetadataExtractor; +using MetadataExtractor.Formats.Exif; using Microsoft.Extensions.Logging; using MQTTnet.Client; using Newtonsoft.Json; @@ -250,7 +253,9 @@ public class ConfigSubscribe : IJob FlightId = flightId, TaskId = taskId, ParentKey = "0", - Name = string.IsNullOrEmpty(executeTask.TaskName) ? timeStr : $"{executeTask.TaskName} {timeStr}", + Name = string.IsNullOrEmpty(executeTask.TaskName) + ? timeStr + : $"{executeTask.TaskName} {timeStr}", WorkspaceId = executeTask.WorkspaceId, CreateTime = date, }; @@ -264,8 +269,111 @@ 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(); + long? picSize = 0; + int width = 0, height = 0, focalLength = 0; + int offset = 0, length = 0; + var fileUrl ="http://" + _minioService.endPoint + "/" + _minioService._bucketName + "/" + objectKey; + using (var httpClient = new HttpClient()) + { + // 目前读取64KB + // 添加Range请求头 + httpClient.DefaultRequestHeaders.Range = + new System.Net.Http.Headers.RangeHeaderValue(0, 65535); + try + { + var response = httpClient + .GetAsync(fileUrl, HttpCompletionOption.ResponseHeadersRead).Result; + if (response.StatusCode == System.Net.HttpStatusCode.PartialContent) + { + //Focal Length 35: 24 mm + //Exif Image Width: 4032 pixels : Exif SubIFD + //Exif Image Height: 3024 pixels : Exif SubIFD + // 3.1 解析Content-Range头(格式:bytes start-end/total) + var contentRange = response.Content.Headers.ContentRange; + if (contentRange != null) + { + picSize = contentRange.Length.Value; + Console.WriteLine( + $"获取范围:{contentRange.From}-{contentRange.To}/{contentRange.Length}"); + } + + // 成功获取部分内容 + var y = response.Content.ReadAsByteArrayAsync().Result; + MemoryStream ms = new MemoryStream(y); + var directories = ImageMetadataReader.ReadMetadata(ms); + foreach (var directory in directories) + { + if (directory is ExifDirectoryBase) + { + if (directory.Name.Equals("Exif SubIFD")) + { + foreach (var tag in directory.Tags) + { + if (tag.Name.Equals("Exif Image Width")) + { + width = int.Parse(tag.Description.Replace("pixels", "") + .Trim()); + } + + if (tag.Name.Equals("Exif Image Height")) + { + height = int.Parse(tag.Description.Replace("pixels", "") + .Trim()); + } + + if (tag.Name.Equals("Focal Length 35")) + { + focalLength = int.Parse(tag.Description.Replace("mm", "") + .Trim()); + } + } + } + + //Console.WriteLine(directory.Name); + if (directory.Name.Equals("Exif Thumbnail")) + { + foreach (var tag in directory.Tags) + { + if (tag.Name.Equals("Thumbnail Offset")) + { + offset = int.Parse(tag.Description.Replace("bytes", "") + .Trim()); + } + + if (tag.Name.Equals("Thumbnail Length")) + { + length = int.Parse(tag.Description.Replace("bytes", "") + .Trim()); + } + } + } + } + } + + ms.Seek(offset + 6, SeekOrigin.Begin); + byte[] buffer = new byte[length]; + int bytesRead = ms.Read(buffer, 0, length); + // 上传缩略图到MinIO + await _minioService.PutObjectAsync("", data.file.name.ToString(), suoluokey, + new MemoryStream(buffer)); + } + else if (response.StatusCode == System.Net.HttpStatusCode.OK) + { + // 服务器不支持Range请求,返回完整内容 + throw new InvalidOperationException("服务器不支持Range请求"); + } + else + { + throw new HttpRequestException($"请求失败: {response.StatusCode}"); + } + } + catch (Exception ex) + { + throw new Exception($"下载Range数据失败: {ex.Message}", ex); + } + } + /*string objectKey1 = data.file.object_key.ToString(); //缩略图处理 var imageStream = _minioService.GetObjectAsStream("", objectKey1); var width = 0; @@ -293,7 +401,7 @@ public class ConfigSubscribe : IJob await _minioService.PutObjectAsync("", data.file.name.ToString(), suoluokey, memoryStream); // 根据实际格式修改Content-Type } - } + }*/ var fileUpload = new LasaMediaFile() { @@ -321,9 +429,10 @@ public class ConfigSubscribe : IJob Width = width, Height = height, minipic = suoluokey, - //Size = size, + Size = picSize, ShowOnMap = 1, - display = 1 + display = 1, + FocalLength = focalLength }; // todo 添加事务 await _sqlSugarClient.Insertable(fileUpload).ExecuteCommandAsync(); diff --git a/OpenAuth.Repository/Domain/LasaMediaFile.cs b/OpenAuth.Repository/Domain/LasaMediaFile.cs index 73b9383..4b3be01 100644 --- a/OpenAuth.Repository/Domain/LasaMediaFile.cs +++ b/OpenAuth.Repository/Domain/LasaMediaFile.cs @@ -108,9 +108,9 @@ public class LasaMediaFile public string FileTags { get; set; } - public long Size { get; set; } - public int Width { get; set; } - public int Height { get; set; } + public long? Size { get; set; } + public int? Width { get; set; } + public int? Height { get; set; } public string Tid { get; set; } public string Bid { get; set; } @@ -120,6 +120,8 @@ public class LasaMediaFile public string PicLink { get; set; } public string minipic { get; set; } + + public double? FocalLength { get; set; } [SugarColumn(IsIgnore = true)] public List Children { get; set; } diff --git a/OpenAuth.WebApi/boot/TestAbc.cs b/OpenAuth.WebApi/boot/TestAbc.cs index ea55589..c54d994 100644 --- a/OpenAuth.WebApi/boot/TestAbc.cs +++ b/OpenAuth.WebApi/boot/TestAbc.cs @@ -9,6 +9,7 @@ using MetadataExtractor.Formats.Exif; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using NUnit.Framework; +using OpenAuth.Repository.Domain; namespace OpenAuth.WebApi.boot; @@ -423,6 +424,10 @@ public class TestAbc [Test] public void DownloadRange() { + var minio = new MinioService(); + long? picSize = 0; + int width = 0, height = 0, focalLength = 0; + int offset = 0, length = 0; using (var httpClient = new HttpClient()) { var total = 0L; @@ -432,7 +437,8 @@ public class TestAbc { var response = httpClient .GetAsync( - "http://175.27.168.120:6013/test/640aea27-2dd3-4081-933c-fcef432fcec0/71924889-540d-4362-aeb1-9ee787a655a7/DJI_202508050906_002_71924889-540d-4362-aeb1-9ee787a655a7/DJI_20250805090809_0001_V.jpeg", + "http://" + minio.endPoint + "/" + minio._bucketName + "/" + + "/640aea27-2dd3-4081-933c-fcef432fcec0/71924889-540d-4362-aeb1-9ee787a655a7/DJI_202508050906_002_71924889-540d-4362-aeb1-9ee787a655a7/DJI_20250805090809_0001_V.jpeg", HttpCompletionOption.ResponseHeadersRead).Result; if (response.StatusCode == System.Net.HttpStatusCode.PartialContent) @@ -444,6 +450,7 @@ public class TestAbc var contentRange = response.Content.Headers.ContentRange; if (contentRange != null) { + picSize = contentRange.Length; Console.WriteLine($"获取范围:{contentRange.From}-{contentRange.To}/{contentRange.Length}"); } @@ -451,14 +458,37 @@ public class TestAbc var y = response.Content.ReadAsByteArrayAsync().Result; MemoryStream ms = new MemoryStream(y); var directories = ImageMetadataReader.ReadMetadata(ms); - int offset = 0, length = 0; foreach (var directory in directories) { if (directory is ExifDirectoryBase) { foreach (var directoryTag in directory.Tags) { - Console.WriteLine($"{directoryTag.Name}: {directoryTag.Description} : { directoryTag.DirectoryName}"); + Console.WriteLine( + $"{directoryTag.Name}: {directoryTag.Description} : {directoryTag.DirectoryName}"); + } + if (directory.Name.Equals("Exif SubIFD")) + { + foreach (var tag in directory.Tags) + { + if (tag.Name.Equals("Exif Image Width")) + { + width = int.Parse(tag.Description.Replace("pixels", "") + .Trim()); + } + + if (tag.Name.Equals("Exif Image Height")) + { + height = int.Parse(tag.Description.Replace("pixels", "") + .Trim()); + } + + if (tag.Name.Equals("Focal Length 35")) + { + focalLength = int.Parse(tag.Description.Replace("mm", "") + .Trim()); + } + } } //Console.WriteLine(directory.Name); @@ -480,13 +510,25 @@ public class TestAbc } } + + var fileUpload = new LasaMediaFile() + { + Id = Guid.NewGuid().ToString(), + Width = width, + Height = height, + Size = picSize, + ShowOnMap = 1, + display = 1, + FocalLength = focalLength + }; + Console.WriteLine("是否有错呢:" + JsonConvert.SerializeObject(fileUpload)); ms.Seek(offset + 6, SeekOrigin.Begin); byte[] buffer = new byte[length]; int bytesRead = ms.Read(buffer, 0, length); var x = IsValidImageFormat(buffer); Console.WriteLine("是否为有效图片:" + x); Console.WriteLine("读取的字节长度: " + bytesRead); - File.WriteAllBytes("f:/abcccc.jpeg", buffer); + File.WriteAllBytes("f:/abbbbbb.jpeg", buffer); } else if (response.StatusCode == System.Net.HttpStatusCode.OK) {