From 4d27d6b27d29de79fdd3d490646118fa95a6efb8 Mon Sep 17 00:00:00 2001 From: christoph seiler Date: Sat, 24 Aug 2024 15:09:06 +0200 Subject: [PATCH] feat: change WebSocket data transfer from byte[] to stringBase64 --- .../Hubs/TrackNodeHub.cs | 19 +++---------------- .../Models/UploadPictureModel.cs | 3 --- src/Trackmate.Backend/TrackNodeService.cs | 4 +++- .../TrackNodes/UploadPictureModel.cs | 3 +++ 4 files changed, 9 insertions(+), 20 deletions(-) delete mode 100644 src/Trackmate.Backend/Models/UploadPictureModel.cs create mode 100644 src/Trackmate.Backend/TrackNodes/UploadPictureModel.cs diff --git a/src/TrackMate.Backend.RestApi/Hubs/TrackNodeHub.cs b/src/TrackMate.Backend.RestApi/Hubs/TrackNodeHub.cs index 57b4f34..6d255de 100644 --- a/src/TrackMate.Backend.RestApi/Hubs/TrackNodeHub.cs +++ b/src/TrackMate.Backend.RestApi/Hubs/TrackNodeHub.cs @@ -2,6 +2,7 @@ using System.Collections.Concurrent; using Trackmate.Backend; using Trackmate.Backend.Models; +using Trackmate.Backend.TrackNodes; namespace TrackMate.Backend.RestApi.Hubs; @@ -32,23 +33,9 @@ public async Task CreateTrackNode(CreateTrackNodeModel model) /// Mime/Type of the uploaded image. /// Byte chunk of the image uploaded. /// Flag if the upload is completed. - public async Task UploadPictureChunkForTrackNode(Guid trackNodeId, string mimeType, byte[] chunk, bool isLastChunk) + public async Task UploadPictureChunkForTrackNode(UploadPictureModel uploadPictureModel) { - logger.LogInformation("Uploaded chunk({byteSize}) for new track node {trackNodeId}.", chunk.Length, trackNodeId); - await _trackNodeUploadDictionary[trackNodeId].WriteAsync(chunk); - - if (isLastChunk) - { - logger.LogInformation("Uploaded last chunk for track node {trackNodeId}, MimeType: {mimeType}.", trackNodeId, mimeType); - await Clients.Caller.SendAsync("Uploaded ", trackNodeId); - - Stream stream = _trackNodeUploadDictionary[trackNodeId]; - _trackNodeUploadDictionary.Remove(trackNodeId); - stream.Seek(0, SeekOrigin.Begin); - - await trackNodeService.UploadTrackNodePictureAsync(new UploadPictureModel(trackNodeId, mimeType, stream), default); - logger.LogInformation("Uploaded picture for track node {trackNodeId}.", trackNodeId); - } + await trackNodeService.UploadTrackNodePictureAsync(uploadPictureModel, default); } /// diff --git a/src/Trackmate.Backend/Models/UploadPictureModel.cs b/src/Trackmate.Backend/Models/UploadPictureModel.cs deleted file mode 100644 index faf2565..0000000 --- a/src/Trackmate.Backend/Models/UploadPictureModel.cs +++ /dev/null @@ -1,3 +0,0 @@ -namespace Trackmate.Backend.Models; - -public record UploadPictureModel(Guid TrackNodeId, string MimeType, Stream imageData); diff --git a/src/Trackmate.Backend/TrackNodeService.cs b/src/Trackmate.Backend/TrackNodeService.cs index 87adca6..960a2cf 100644 --- a/src/Trackmate.Backend/TrackNodeService.cs +++ b/src/Trackmate.Backend/TrackNodeService.cs @@ -19,7 +19,9 @@ public async Task CreateTrackNodeAsync(CreateTrackNodeModel mode public async Task UploadTrackNodePictureAsync(UploadPictureModel uploadPictureModel, CancellationToken cancellationToken) { logger.LogInformation("Enriching track node (id:{trackNodeId} with picture embedding.", uploadPictureModel.TrackNodeId); - PictureEmbeddingModel embedding = await embeddingClient.GeneratePictureEmbeddingAsync(uploadPictureModel.MimeType, uploadPictureModel.imageData); + + using Stream dataStream = new MemoryStream(Convert.FromBase64String(uploadPictureModel.ImageDataBase64)); + PictureEmbeddingModel embedding = await embeddingClient.GeneratePictureEmbeddingAsync(uploadPictureModel.MimeType, dataStream); return await trackNodeDataSource.AppendEmbeddingAsync(uploadPictureModel.TrackNodeId, embedding, cancellationToken); } } \ No newline at end of file diff --git a/src/Trackmate.Backend/TrackNodes/UploadPictureModel.cs b/src/Trackmate.Backend/TrackNodes/UploadPictureModel.cs new file mode 100644 index 0000000..9682faf --- /dev/null +++ b/src/Trackmate.Backend/TrackNodes/UploadPictureModel.cs @@ -0,0 +1,3 @@ +namespace Trackmate.Backend.TrackNodes; + +public record UploadPictureModel(Guid TrackNodeId, string ImageDataBase64, string MimeType);