Skip to content

Commit

Permalink
feat: change WebSocket data transfer from byte[] to stringBase64
Browse files Browse the repository at this point in the history
  • Loading branch information
seilc1 committed Aug 24, 2024
1 parent 81db2c4 commit 4d27d6b
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 20 deletions.
19 changes: 3 additions & 16 deletions src/TrackMate.Backend.RestApi/Hubs/TrackNodeHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Concurrent;
using Trackmate.Backend;
using Trackmate.Backend.Models;
using Trackmate.Backend.TrackNodes;

namespace TrackMate.Backend.RestApi.Hubs;

Expand Down Expand Up @@ -32,23 +33,9 @@ public async Task<TrackNodeModel> CreateTrackNode(CreateTrackNodeModel model)
/// <param name="mimeType">Mime/Type of the uploaded image.</param>
/// <param name="chunk">Byte chunk of the image uploaded.</param>
/// <param name="isLastChunk">Flag if the upload is completed.</param>
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);
}

/// <summary>
Expand Down
3 changes: 0 additions & 3 deletions src/Trackmate.Backend/Models/UploadPictureModel.cs

This file was deleted.

4 changes: 3 additions & 1 deletion src/Trackmate.Backend/TrackNodeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public async Task<TrackNodeModel> CreateTrackNodeAsync(CreateTrackNodeModel mode
public async Task<TrackNodeModel> 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);
}
}
3 changes: 3 additions & 0 deletions src/Trackmate.Backend/TrackNodes/UploadPictureModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Trackmate.Backend.TrackNodes;

public record UploadPictureModel(Guid TrackNodeId, string ImageDataBase64, string MimeType);

0 comments on commit 4d27d6b

Please sign in to comment.