From 92ab7a1a2af749e785a9cd0a511663bbddf65ffa Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Mon, 14 Jul 2025 03:07:03 +0800 Subject: [PATCH] :sparkles: Still don't know what I am doing --- DysonNetwork.Drive/Storage/FileService.cs | 63 +++++++++++++++++++ DysonNetwork.Drive/Storage/FileServiceGrpc.cs | 12 +--- 2 files changed, 65 insertions(+), 10 deletions(-) diff --git a/DysonNetwork.Drive/Storage/FileService.cs b/DysonNetwork.Drive/Storage/FileService.cs index 7387de0..d18868d 100644 --- a/DysonNetwork.Drive/Storage/FileService.cs +++ b/DysonNetwork.Drive/Storage/FileService.cs @@ -3,6 +3,7 @@ using FFMpegCore; using System.Security.Cryptography; using DysonNetwork.Shared.Cache; using DysonNetwork.Shared.Proto; +using Google.Protobuf.WellKnownTypes; using Microsoft.EntityFrameworkCore; using Minio; using Minio.DataModel.Args; @@ -344,6 +345,68 @@ public class FileService( return file; } + public async Task UpdateFileAsync(CloudFile file, FieldMask updateMask) + { + var existingFile = await db.Files.FirstOrDefaultAsync(f => f.Id == file.Id); + if (existingFile == null) + { + throw new InvalidOperationException($"File with ID {file.Id} not found."); + } + + foreach (var path in updateMask.Paths) + { + switch (path) + { + case "name": + existingFile.Name = file.Name; + break; + case "description": + existingFile.Description = file.Description; + break; + case "file_meta": + existingFile.FileMeta = file.FileMeta; + break; + case "user_meta": + existingFile.UserMeta = file.UserMeta; + break; + case "mime_type": + existingFile.MimeType = file.MimeType; + break; + case "hash": + existingFile.Hash = file.Hash; + break; + case "size": + existingFile.Size = file.Size; + break; + case "uploaded_at": + existingFile.UploadedAt = file.UploadedAt; + break; + case "uploaded_to": + existingFile.UploadedTo = file.UploadedTo; + break; + case "has_compression": + existingFile.HasCompression = file.HasCompression; + break; + case "is_marked_recycle": + existingFile.IsMarkedRecycle = file.IsMarkedRecycle; + break; + case "storage_id": + existingFile.StorageId = file.StorageId; + break; + case "storage_url": + existingFile.StorageUrl = file.StorageUrl; + break; + default: + logger.LogWarning("Attempted to update unknown field: {Field}", path); + break; + } + } + + await db.SaveChangesAsync(); + await _PurgeCacheAsync(file.Id); + return existingFile; + } + public async Task DeleteFileAsync(CloudFile file) { await DeleteFileDataAsync(file); diff --git a/DysonNetwork.Drive/Storage/FileServiceGrpc.cs b/DysonNetwork.Drive/Storage/FileServiceGrpc.cs index d3118a5..ea1e735 100644 --- a/DysonNetwork.Drive/Storage/FileServiceGrpc.cs +++ b/DysonNetwork.Drive/Storage/FileServiceGrpc.cs @@ -15,19 +15,11 @@ namespace DysonNetwork.Drive.Storage public override async Task UpdateFile(UpdateFileRequest request, ServerCallContext context) { - // Assuming UpdateFileAsync exists in FileService and handles the update_mask - // This is a placeholder, as the current FileService.cs doesn't have a direct UpdateFile method - // You might need to implement this logic in FileService based on your needs. - // For now, we'll just return the requested file. var file = await fileService.GetFileAsync(request.File.Id); if (file == null) - { throw new RpcException(new Status(StatusCode.NotFound, "File not found")); - } - - // Apply updates from request.File to 'file' based on request.UpdateMask - // This part requires more detailed implementation based on how you want to handle partial updates. - return file.ToProtoValue(); + var updatedFile = await fileService.UpdateFileAsync(file, request.UpdateMask); + return updatedFile.ToProtoValue(); } public override async Task DeleteFile(DeleteFileRequest request, ServerCallContext context)