custom file stream implementation

This commit is contained in:
rootdarkarchon
2023-08-26 00:36:30 +02:00
parent a3b33c0e6b
commit 56b27e5ee8
5 changed files with 153 additions and 17 deletions

View File

@@ -2,8 +2,6 @@
using MareSynchronosStaticFilesServer.Services;
using MareSynchronosStaticFilesServer.Utils;
using Microsoft.AspNetCore.Mvc;
using System.Globalization;
using System.Text;
namespace MareSynchronosStaticFilesServer.Controllers;
@@ -34,27 +32,22 @@ public class CacheController : ControllerBase
_requestQueue.ActivateRequest(requestId);
Response.ContentType = "application/octet-stream";
var memoryStream = new MemoryStream();
var streamWriter = new BinaryWriter(memoryStream);
long requestSize = 0;
List<BlockFileDataSubstream> substreams = new();
foreach (var file in request.FileIds)
{
var fs = await _cachedFileProvider.GetAndDownloadFileStream(file);
if (fs == null) continue;
streamWriter.Write(Encoding.ASCII.GetBytes("#" + file + ":" + fs.Length.ToString(CultureInfo.InvariantCulture) + "#"));
byte[] buffer = new byte[fs.Length];
_ = await fs.ReadAsync(buffer, HttpContext.RequestAborted);
streamWriter.Write(buffer);
substreams.Add(new(fs));
requestSize += fs.Length;
}
streamWriter.Flush();
memoryStream.Position = 0;
_fileStatisticsService.LogRequest(requestSize);
return _requestFileStreamResultFactory.Create(requestId, memoryStream);
return _requestFileStreamResultFactory.Create(requestId, new BlockFileDataStream(substreams.ToArray()));
}
}