Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public override void WriteJson(JsonWriter writer, Dictionary<string, CouchAttach
JsonSerializer serializer)
{
serializer.Serialize(writer, value
.Where(kvp => kvp.Value.FileInfo is null)
.Where(kvp => kvp.Value.Stream is null)
.ToDictionary(k => k.Key, v => v.Value)
);
}
Expand Down
7 changes: 3 additions & 4 deletions src/CouchDB.Driver/CouchDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/CouchDB.Driver/Types/CouchAttachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
10 changes: 4 additions & 6 deletions src/CouchDB.Driver/Types/CouchAttachmentsCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,19 @@ internal CouchAttachmentsCollection(Dictionary<string, CouchAttachment> 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());
}

CouchAttachment attachment = _attachments[attachmentName];
attachment.Name = attachmentName;
attachment.FileInfo = info;
attachment.Stream = stream;
attachment.ContentType = contentType;
}

Expand Down Expand Up @@ -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();
}
Expand Down