using DysonNetwork.Shared.Proto;
using Google.Protobuf.WellKnownTypes;
namespace DysonNetwork.Shared.Data;
///
/// The class that used in jsonb columns which referenced the cloud file.
/// The aim of this class is to store some properties that won't change to a file to reduce the database load.
///
public class CloudFileReferenceObject : ModelBase, ICloudFile
{
public string Id { get; set; } = null!;
public string Name { get; set; } = string.Empty;
public Dictionary FileMeta { get; set; } = null!;
public Dictionary UserMeta { get; set; } = null!;
public string? MimeType { get; set; }
public string? Hash { get; set; }
public long Size { get; set; }
public bool HasCompression { get; set; } = false;
public static CloudFileReferenceObject FromProtoValue(Proto.CloudFile proto)
{
return new CloudFileReferenceObject
{
Id = proto.Id,
Name = proto.Name,
FileMeta = proto.FileMeta
.ToDictionary(kvp => kvp.Key, kvp => GrpcTypeHelper.ConvertField(kvp.Value)),
UserMeta = proto.UserMeta
.ToDictionary(kvp => kvp.Key, kvp => GrpcTypeHelper.ConvertField(kvp.Value)),
MimeType = proto.MimeType,
Hash = proto.Hash,
Size = proto.Size,
HasCompression = proto.HasCompression
};
}
///
/// Converts the current object to its protobuf representation
///
public Proto.CloudFile ToProtoValue()
{
var proto = new Proto.CloudFile
{
Id = Id,
Name = Name,
MimeType = MimeType ?? string.Empty,
Hash = Hash ?? string.Empty,
Size = Size,
HasCompression = HasCompression,
ContentType = MimeType ?? string.Empty,
Url = string.Empty // This should be set by the caller if needed
};
// Convert file metadata
if (FileMeta != null)
{
foreach (var (key, value) in FileMeta)
{
proto.FileMeta[key] = Value.ForString(value?.ToString() ?? string.Empty);
}
}
// Convert user metadata
if (UserMeta != null)
{
foreach (var (key, value) in UserMeta)
{
proto.UserMeta[key] = Value.ForString(value?.ToString() ?? string.Empty);
}
}
return proto;
}
}