custom file stream implementation
This commit is contained in:
@@ -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()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user