Skip to content

Commit

Permalink
feat: 暂时使用一个很玄乎的办法搞定了 206
Browse files Browse the repository at this point in the history
  • Loading branch information
SALTWOOD committed Apr 27, 2024
1 parent d4eedba commit 77ad68f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CSharp-OpenBMCLAPI/Modules/Cluster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public Cluster(ClusterRequiredData requiredData) : base()
client = HttpRequest.client;
client.DefaultRequestHeaders.Authorization = new("Bearer", requiredData.Token?.Token.token);

this.storage = new CachedStorage(new FileStorage(ClusterRequiredData.Config.clusterFileDirectory));
this.storage = new FileStorage(ClusterRequiredData.Config.clusterFileDirectory);

this.counter = new();
InitializeSocket();
Expand Down
13 changes: 10 additions & 3 deletions CSharp-OpenBMCLAPI/Modules/HttpServiceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,26 @@ public static async Task<FileAccessInfo> DownloadHash(HttpContext context, Clust
if (context.Request.Header.ContainsKey("range"))
{
// 206 处理部分
context.Response.StatusCode = 206;
(long from, long to) = ToRangeByte(context.Request.Header["range"].Split("=").Last().Split("-"));
long length = (to - from + 1);
context.Request.Header.ToString().Dump();
context.Response.Header["Content-Length"] = length.ToString();
await cluster.storage.HandleRequest(Utils.HashToFileName(hash), context);

//TODO: 尝试优化这坨屎
Stream file = cluster.storage.ReadFileStream(Utils.HashToFileName(hash));
file.Seek(from, SeekOrigin.Begin);
for (long i = from; i <= to; i++)
{
context.Response.Stream.WriteByte((byte)file.ReadByte());
}
context.Response.Stream.Position = 0;
context.Response.Stream.Seek(from, SeekOrigin.Begin);

context.Response.Header["Content-Range"] = $"{from}-{to}/{context.Response.Stream.Length}";
context.Response.Header["x-bmclapi-hash"] = hash;
context.Response.Header["Accept-Ranges"] = "bytes";
context.Response.Header["Content-Type"] = "application/octet-stream";
context.Response.Header["Connection"] = "closed";
context.Response.StatusCode = 206;
fai = new FileAccessInfo
{
hits = 1,
Expand Down

0 comments on commit 77ad68f

Please sign in to comment.