-
Notifications
You must be signed in to change notification settings - Fork 43
/
FileContent.cs
104 lines (89 loc) · 3.89 KB
/
FileContent.cs
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using MiniWebServer.Abstractions.Http;
using System.Buffers;
namespace MiniWebServer.MiniApp.Content
{
public class FileContent : MiniContent
{
private readonly FileInfo file;
private readonly HttpHeaders headers;
private readonly FileContentRange? fileContentRange;
public FileContent(string fileName, FileContentRange? fileContentRange = null) : this(new FileInfo(fileName), fileContentRange)
{
}
public FileContent(FileInfo file, FileContentRange? fileContentRange = null)
{
this.file = file ?? throw new ArgumentNullException(nameof(file));
if (!file.Exists)
{
throw new FileNotFoundException(file.FullName);
}
this.fileContentRange = fileContentRange;
if (fileContentRange != null)
{
long length = fileContentRange.LastBytePosInclusive.HasValue ? (fileContentRange.LastBytePosInclusive.Value - fileContentRange.FirstBytePosInclusive + 1) : file.Length - fileContentRange.FirstBytePosInclusive;
if (length > file.Length - fileContentRange.FirstBytePosInclusive)
{
length = file.Length - fileContentRange.FirstBytePosInclusive;
}
headers = new() {
{ "Content-Length", length.ToString() }
};
}
else
{
headers = new() {
{ "Content-Length", file.Length.ToString() }
};
}
}
public override HttpHeaders Headers => headers;
public override async Task<long> WriteToAsync(Stream stream, CancellationToken cancellationToken)
{
long length = file.Length;
var buffer = ArrayPool<byte>.Shared.Rent(8192); // rent a 8K-buffer
try
{
using var fs = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.Read);
if (fileContentRange != null)
{
if (fileContentRange.FirstBytePosInclusive > 0)
{
fs.Seek(fileContentRange.FirstBytePosInclusive, SeekOrigin.Begin);
length = file.Length - fileContentRange.FirstBytePosInclusive;
}
if (fileContentRange.LastBytePosInclusive.HasValue)
{
length = fileContentRange.LastBytePosInclusive.Value - fileContentRange.FirstBytePosInclusive + 1; // the the byte positions are inclusive, for example: 0-0 means 1 byte (at [0])
if (length > file.Length - fileContentRange.FirstBytePosInclusive) // if the selected representation is shorter than the specified FirstBytePosInclusive - length, the entire representation is used.
{
length = file.Length - fileContentRange.FirstBytePosInclusive;
}
}
}
var bytesRead = await fs.ReadAsync(buffer.AsMemory(0, (int)Math.Min(length, buffer.Length)), cancellationToken);
while (length > 0)
{
if (bytesRead != buffer.Length)
stream.Write(buffer.AsSpan(0, bytesRead));
else
stream.Write(buffer.AsSpan());
length -= bytesRead;
if (length > 0)
{
bytesRead = await fs.ReadAsync(buffer, cancellationToken);
}
}
fs.Close();
return file.Length;
}
catch (Exception)
{
return 0;
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
}
}
}
}