Skip to content

Commit 59bbe20

Browse files
authored
Merge pull request #1 from dhirensham/feature/attachments-as-streams-instead-of-files
Use streams instead of files for attachments
2 parents 212a0db + 5f21316 commit 59bbe20

File tree

4 files changed

+9
-12
lines changed

4 files changed

+9
-12
lines changed

src/CouchDB.Driver/Converters/AttachmentsParsedConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public override void WriteJson(JsonWriter writer, Dictionary<string, CouchAttach
1212
JsonSerializer serializer)
1313
{
1414
serializer.Serialize(writer, value
15-
.Where(kvp => kvp.Value.FileInfo is null)
15+
.Where(kvp => kvp.Value.Stream is null)
1616
.ToDictionary(k => k.Key, v => v.Value)
1717
);
1818
}

src/CouchDB.Driver/CouchDatabase.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -455,13 +455,12 @@ private async Task UpdateAttachments(TSource document, CancellationToken cancell
455455
{
456456
foreach (CouchAttachment attachment in document.Attachments.GetAddedAttachments())
457457
{
458-
if (attachment.FileInfo == null)
458+
if (attachment.Stream == null)
459459
{
460460
continue;
461461
}
462462

463-
using var stream = new StreamContent(
464-
new FileStream(attachment.FileInfo.FullName, FileMode.Open));
463+
using var stream = new StreamContent(attachment.Stream);
465464

466465
AttachmentResult response = await NewRequest()
467466
.AppendPathSegment(Uri.EscapeDataString(document.Id))
@@ -475,7 +474,7 @@ private async Task UpdateAttachments(TSource document, CancellationToken cancell
475474
if (response.Ok)
476475
{
477476
document.Rev = response.Rev;
478-
attachment.FileInfo = null;
477+
attachment.Stream.Close();
479478
}
480479
}
481480

src/CouchDB.Driver/Types/CouchAttachment.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public sealed class CouchAttachment
1818
internal string DocumentRev { get; set; }
1919

2020
[JsonIgnore]
21-
internal FileInfo FileInfo { get; set; }
21+
internal Stream Stream { get; set; }
2222

2323
[JsonIgnore]
2424
internal bool Deleted { get; set; }

src/CouchDB.Driver/Types/CouchAttachmentsCollection.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,19 @@ internal CouchAttachmentsCollection(Dictionary<string, CouchAttachment> attachme
2828
public void AddOrUpdate(string path, string contentType)
2929
{
3030
FileInfo info = GetFileInfo(path);
31-
AddOrUpdate(info.Name, path, contentType);
31+
AddOrUpdate(info.Name, info.OpenRead(), contentType);
3232
}
3333

34-
public void AddOrUpdate(string attachmentName, string path, string contentType)
34+
public void AddOrUpdate(string attachmentName, Stream stream, string contentType)
3535
{
36-
FileInfo info = GetFileInfo(path);
37-
3836
if (!_attachments.ContainsKey(attachmentName))
3937
{
4038
_attachments.Add(attachmentName, new CouchAttachment());
4139
}
4240

4341
CouchAttachment attachment = _attachments[attachmentName];
4442
attachment.Name = attachmentName;
45-
attachment.FileInfo = info;
43+
attachment.Stream = stream;
4644
attachment.ContentType = contentType;
4745
}
4846

@@ -73,7 +71,7 @@ IEnumerator IEnumerable.GetEnumerator()
7371
internal CouchAttachment[] GetAddedAttachments()
7472
{
7573
return _attachments
76-
.Where(kv => kv.Value.FileInfo != null)
74+
.Where(kv => kv.Value.Stream != null)
7775
.Select(kv => kv.Value)
7876
.ToArray();
7977
}

0 commit comments

Comments
 (0)