-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathInMemoryAttachmentsBodyStorage.cs
More file actions
75 lines (63 loc) · 2.24 KB
/
InMemoryAttachmentsBodyStorage.cs
File metadata and controls
75 lines (63 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
namespace ServiceControl.Audit.Persistence.InMemory
{
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ServiceControl.Audit.Auditing.BodyStorage;
class InMemoryAttachmentsBodyStorage : IBodyStorage
{
List<MessageBody> messageBodies;
public InMemoryAttachmentsBodyStorage()
{
messageBodies = [];
}
public Task Store(string bodyId, string contentType, int bodySize, Stream bodyStream, CancellationToken cancellationToken)
{
var messageBody = messageBodies.FirstOrDefault(w => w.BodyId == bodyId);
var needToAdd = false;
if (messageBody == null)
{
messageBody = new MessageBody() { BodyId = bodyId };
needToAdd = true;
}
messageBody.BodySize = bodySize;
using (var reader = new BinaryReader(bodyStream))
{
messageBody.Content = reader.ReadBytes(bodySize);
}
messageBody.ContentType = contentType;
if (needToAdd)
{
messageBodies.Add(messageBody);
}
return Task.CompletedTask;
}
public async Task<StreamResult> TryFetch(string bodyId, CancellationToken cancellationToken)
{
var messageBody = messageBodies.FirstOrDefault(w => w.BodyId == bodyId);
return await Task.FromResult(messageBody == null
? new StreamResult
{
HasResult = false,
Stream = null
}
: new StreamResult
{
HasResult = true,
Stream = new MemoryStream(messageBody.Content),
ContentType = messageBody.ContentType,
BodySize = messageBody.BodySize,
Etag = string.Empty
});
}
class MessageBody
{
public string BodyId { get; set; }
public string ContentType { get; set; }
public int BodySize { get; set; }
public byte[] Content { get; set; }
}
}
}