🐛 Fixes in CloudFile filemeta transfer via gRPC

This commit is contained in:
2025-07-19 12:03:18 +08:00
parent e0e1eb76cd
commit 4398984551
7 changed files with 73 additions and 52 deletions

View File

@@ -1,5 +1,5 @@
using DysonNetwork.Shared.Proto;
using Google.Protobuf.WellKnownTypes;
using Google.Protobuf;
namespace DysonNetwork.Shared.Data;
@@ -41,10 +41,14 @@ public class CloudFileReferenceObject : ModelBase, ICloudFile
{
Id = proto.Id,
Name = proto.Name,
FileMeta = proto.FileMeta
.ToDictionary(kvp => kvp.Key, kvp => GrpcTypeHelper.ConvertValueToObject(kvp.Value)),
UserMeta = proto.UserMeta
.ToDictionary(kvp => kvp.Key, kvp => GrpcTypeHelper.ConvertValueToObject(kvp.Value)),
FileMeta = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, object?>>(
proto.FileMeta.ToStringUtf8(),
GrpcTypeHelper.SystemTextSerializerOptions
) ?? [],
UserMeta = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, object?>>(
proto.UserMeta.ToStringUtf8(),
GrpcTypeHelper.SystemTextSerializerOptions
) ?? [],
MimeType = proto.MimeType,
Hash = proto.Hash,
Size = proto.Size,
@@ -55,9 +59,9 @@ public class CloudFileReferenceObject : ModelBase, ICloudFile
/// <summary>
/// Converts the current object to its protobuf representation
/// </summary>
public Proto.CloudFile ToProtoValue()
public CloudFile ToProtoValue()
{
var proto = new Proto.CloudFile
var proto = new CloudFile
{
Id = Id,
Name = Name,
@@ -70,22 +74,14 @@ public class CloudFileReferenceObject : ModelBase, ICloudFile
};
// Convert file metadata
if (FileMeta != null)
{
foreach (var (key, value) in FileMeta)
{
proto.FileMeta[key] = Value.ForString(value?.ToString() ?? string.Empty);
}
}
proto.FileMeta = ByteString.CopyFromUtf8(
System.Text.Json.JsonSerializer.Serialize(FileMeta, GrpcTypeHelper.SystemTextSerializerOptions)
);
// Convert user metadata
if (UserMeta != null)
{
foreach (var (key, value) in UserMeta)
{
proto.UserMeta[key] = Value.ForString(value?.ToString() ?? string.Empty);
}
}
proto.UserMeta = ByteString.CopyFromUtf8(
System.Text.Json.JsonSerializer.Serialize(UserMeta, GrpcTypeHelper.SystemTextSerializerOptions)
);
return proto;
}

View File

@@ -1,4 +1,5 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using Google.Protobuf.Collections;
using Google.Protobuf.WellKnownTypes;
using Newtonsoft.Json;
@@ -8,7 +9,14 @@ namespace DysonNetwork.Shared.Proto;
public abstract class GrpcTypeHelper
{
private static readonly JsonSerializerSettings SerializerSettings = new()
public static readonly JsonSerializerOptions SystemTextSerializerOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Converters = { new JsonStringEnumConverter() }
};
public static readonly JsonSerializerSettings SerializerSettings = new()
{
ContractResolver = new DefaultContractResolver { NamingStrategy = new SnakeCaseNamingStrategy() },
PreserveReferencesHandling = PreserveReferencesHandling.Objects,

View File

@@ -19,11 +19,11 @@ message CloudFile {
// Original name of the file
string name = 2;
// The metadata uses JSON bytes to store to keep the data structure over gRPC
// File metadata (e.g., dimensions, duration, etc.)
map<string, google.protobuf.Value> file_meta = 3;
bytes file_meta = 3;
// User-defined metadata
map<string, google.protobuf.Value> user_meta = 4;
bytes user_meta = 4;
// MIME type of the file
string mime_type = 5;