From 5f21316fa2bab1fd8aac273b5fd8fb3b2310f7ea Mon Sep 17 00:00:00 2001 From: Dhiren Sham Date: Wed, 5 Jun 2024 12:16:30 +0200 Subject: [PATCH] Use streams instead of files for attachments --- .../Converters/AttachmentsParsedConverter.cs | 2 +- src/CouchDB.Driver/CouchDatabase.cs | 7 +++---- src/CouchDB.Driver/Types/CouchAttachment.cs | 2 +- src/CouchDB.Driver/Types/CouchAttachmentsCollection.cs | 10 ++++------ 4 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/CouchDB.Driver/Converters/AttachmentsParsedConverter.cs b/src/CouchDB.Driver/Converters/AttachmentsParsedConverter.cs index 3433ffe..481e9ff 100644 --- a/src/CouchDB.Driver/Converters/AttachmentsParsedConverter.cs +++ b/src/CouchDB.Driver/Converters/AttachmentsParsedConverter.cs @@ -12,7 +12,7 @@ public override void WriteJson(JsonWriter writer, Dictionary kvp.Value.FileInfo is null) + .Where(kvp => kvp.Value.Stream is null) .ToDictionary(k => k.Key, v => v.Value) ); } diff --git a/src/CouchDB.Driver/CouchDatabase.cs b/src/CouchDB.Driver/CouchDatabase.cs index e6f9b61..bdafb84 100644 --- a/src/CouchDB.Driver/CouchDatabase.cs +++ b/src/CouchDB.Driver/CouchDatabase.cs @@ -455,13 +455,12 @@ private async Task UpdateAttachments(TSource document, CancellationToken cancell { foreach (CouchAttachment attachment in document.Attachments.GetAddedAttachments()) { - if (attachment.FileInfo == null) + if (attachment.Stream == null) { continue; } - using var stream = new StreamContent( - new FileStream(attachment.FileInfo.FullName, FileMode.Open)); + using var stream = new StreamContent(attachment.Stream); AttachmentResult response = await NewRequest() .AppendPathSegment(Uri.EscapeDataString(document.Id)) @@ -475,7 +474,7 @@ private async Task UpdateAttachments(TSource document, CancellationToken cancell if (response.Ok) { document.Rev = response.Rev; - attachment.FileInfo = null; + attachment.Stream.Close(); } } diff --git a/src/CouchDB.Driver/Types/CouchAttachment.cs b/src/CouchDB.Driver/Types/CouchAttachment.cs index e06d5a9..d62f16d 100644 --- a/src/CouchDB.Driver/Types/CouchAttachment.cs +++ b/src/CouchDB.Driver/Types/CouchAttachment.cs @@ -18,7 +18,7 @@ public sealed class CouchAttachment internal string DocumentRev { get; set; } [JsonIgnore] - internal FileInfo FileInfo { get; set; } + internal Stream Stream { get; set; } [JsonIgnore] internal bool Deleted { get; set; } diff --git a/src/CouchDB.Driver/Types/CouchAttachmentsCollection.cs b/src/CouchDB.Driver/Types/CouchAttachmentsCollection.cs index 7230c1b..585d1bc 100644 --- a/src/CouchDB.Driver/Types/CouchAttachmentsCollection.cs +++ b/src/CouchDB.Driver/Types/CouchAttachmentsCollection.cs @@ -28,13 +28,11 @@ internal CouchAttachmentsCollection(Dictionary attachme public void AddOrUpdate(string path, string contentType) { FileInfo info = GetFileInfo(path); - AddOrUpdate(info.Name, path, contentType); + AddOrUpdate(info.Name, info.OpenRead(), contentType); } - public void AddOrUpdate(string attachmentName, string path, string contentType) + public void AddOrUpdate(string attachmentName, Stream stream, string contentType) { - FileInfo info = GetFileInfo(path); - if (!_attachments.ContainsKey(attachmentName)) { _attachments.Add(attachmentName, new CouchAttachment()); @@ -42,7 +40,7 @@ public void AddOrUpdate(string attachmentName, string path, string contentType) CouchAttachment attachment = _attachments[attachmentName]; attachment.Name = attachmentName; - attachment.FileInfo = info; + attachment.Stream = stream; attachment.ContentType = contentType; } @@ -73,7 +71,7 @@ IEnumerator IEnumerable.GetEnumerator() internal CouchAttachment[] GetAddedAttachments() { return _attachments - .Where(kv => kv.Value.FileInfo != null) + .Where(kv => kv.Value.Stream != null) .Select(kv => kv.Value) .ToArray(); }