Skip to content

Commit

Permalink
feat: 206 支持完毕,Merge 到 master
Browse files Browse the repository at this point in the history
  • Loading branch information
SALTWOOD committed Apr 29, 2024
1 parent 716480e commit 403863d
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions CSharp-OpenBMCLAPI/Modules/HttpServiceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,21 @@ public static async Task<FileAccessInfo> DownloadHash(HttpContext context, Clust
(from, to) = ToRangeByte(context.Request.Header["range"].Split("=").Last().Split("-"));
if (to < from && to != -1) (from, to) = (to, from);

//TODO: 尝试优化这坨屎
Stream file = cluster.storage.ReadFileStream(Utils.HashToFileName(hash));
if (to == -1) to = file.Length;
using (Stream file = cluster.storage.ReadFileStream(Utils.HashToFileName(hash)))
{
if (to == -1) to = file.Length;

long length = (to - from + 1);
context.Response.Header["Content-Length"] = length.ToString();
long length = (to - from + 1);
context.Response.Header["Content-Length"] = length.ToString();

file.Seek(from, SeekOrigin.Begin);
for (long i = from; i <= to; i++)
{
context.Response.Stream.WriteByte((byte)file.ReadByte());
file.Seek(from, SeekOrigin.Begin);
byte[] buffer = new byte[4096];
for (; file.Position <= to;)
{
int count = file.Read(buffer, 0, buffer.Length);
if (count != buffer.Length) context.Response.Stream.Write(buffer[..count]);
else context.Response.Stream.Write(buffer);
}
}
context.Response.ResetStreamPosition();

Expand Down

0 comments on commit 403863d

Please sign in to comment.