From 20ddd9ae8c458adc2aefd3cb595adbde415c2b32 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 26 Oct 2022 11:20:10 -0400 Subject: [PATCH] fix cipher attachment saving on EF repos (#2365) --- .../Repositories/BaseEntityFrameworkRepository.cs | 8 ++++---- .../Repositories/CipherRepository.cs | 8 +++++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Infrastructure.EntityFramework/Repositories/BaseEntityFrameworkRepository.cs b/src/Infrastructure.EntityFramework/Repositories/BaseEntityFrameworkRepository.cs index 9dc7818d73..6d179f0159 100644 --- a/src/Infrastructure.EntityFramework/Repositories/BaseEntityFrameworkRepository.cs +++ b/src/Infrastructure.EntityFramework/Repositories/BaseEntityFrameworkRepository.cs @@ -124,8 +124,8 @@ public abstract class BaseEntityFrameworkRepository !string.IsNullOrWhiteSpace(e.Attachments)) .Select(e => e.Attachments) .ToListAsync(); - var storage = attachments.Sum(e => JsonDocument.Parse(e)?.RootElement.EnumerateArray() - .Sum(p => p.GetProperty("Size").GetInt64()) ?? 0); + var storage = attachments.Sum(e => JsonDocument.Parse(e)?.RootElement.EnumerateObject() + .Sum(p => p.Value.GetProperty("Size").GetInt64()) ?? 0); var organization = new Organization { Id = organizationId, @@ -152,8 +152,8 @@ public abstract class BaseEntityFrameworkRepository !string.IsNullOrWhiteSpace(e.Attachments)) .Select(e => e.Attachments) .ToListAsync(); - var storage = attachments.Sum(e => JsonDocument.Parse(e)?.RootElement.EnumerateArray() - .Sum(p => p.GetProperty("Size").GetInt64()) ?? 0); + var storage = attachments.Sum(e => JsonDocument.Parse(e)?.RootElement.EnumerateObject() + .Sum(p => p.Value.GetProperty("Size").GetInt64()) ?? 0); var user = new Models.User { Id = userId, diff --git a/src/Infrastructure.EntityFramework/Repositories/CipherRepository.cs b/src/Infrastructure.EntityFramework/Repositories/CipherRepository.cs index 1719a161b4..01a9ce2144 100644 --- a/src/Infrastructure.EntityFramework/Repositories/CipherRepository.cs +++ b/src/Infrastructure.EntityFramework/Repositories/CipherRepository.cs @@ -549,9 +549,11 @@ public class CipherRepository : Repository, { var dbContext = GetDatabaseContext(scope); var cipher = await dbContext.Ciphers.FindAsync(attachment.Id); - var attachmentsJson = string.IsNullOrWhiteSpace(cipher.Attachments) ? new JObject() : JObject.Parse(cipher.Attachments); - attachmentsJson.Add(attachment.AttachmentId, attachment.AttachmentData); - cipher.Attachments = JsonConvert.SerializeObject(attachmentsJson); + var attachments = string.IsNullOrWhiteSpace(cipher.Attachments) ? + new Dictionary() : + JsonConvert.DeserializeObject>(cipher.Attachments); + attachments.Add(attachment.AttachmentId, JsonConvert.DeserializeObject(attachment.AttachmentData)); + cipher.Attachments = JsonConvert.SerializeObject(attachments); await dbContext.SaveChangesAsync(); if (attachment.OrganizationId.HasValue)