forked from Eauldane/SnowcloakServer
Initial
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
|
||||
namespace MareSynchronosStaticFilesServer.Utils;
|
||||
|
||||
// Counts the number of bytes read/written to an underlying stream
|
||||
public class CountedStream : Stream
|
||||
{
|
||||
private readonly Stream _stream;
|
||||
public long BytesRead { get; private set; }
|
||||
public long BytesWritten { get; private set; }
|
||||
public bool DisposeUnderlying = true;
|
||||
|
||||
public Stream UnderlyingStream { get => _stream; }
|
||||
|
||||
public CountedStream(Stream underlyingStream)
|
||||
{
|
||||
_stream = underlyingStream;
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (!DisposeUnderlying)
|
||||
return;
|
||||
_stream.Dispose();
|
||||
}
|
||||
|
||||
public override bool CanRead => _stream.CanRead;
|
||||
public override bool CanSeek => _stream.CanSeek;
|
||||
public override bool CanWrite => _stream.CanWrite;
|
||||
public override long Length => _stream.Length;
|
||||
|
||||
public override long Position { get => _stream.Position; set => _stream.Position = value; }
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
_stream.Flush();
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
int n = _stream.Read(buffer, offset, count);
|
||||
BytesRead += n;
|
||||
return n;
|
||||
}
|
||||
|
||||
public async override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
|
||||
{
|
||||
int n = await _stream.ReadAsync(buffer, offset, count, cancellationToken);
|
||||
BytesRead += n;
|
||||
return n;
|
||||
}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
return _stream.Seek(offset, origin);
|
||||
}
|
||||
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
_stream.SetLength(value);
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
_stream.Write(buffer, offset, count);
|
||||
BytesWritten += count;
|
||||
}
|
||||
|
||||
public async override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
|
||||
{
|
||||
await _stream.WriteAsync(buffer, offset, count, cancellationToken);
|
||||
BytesWritten += count;
|
||||
}
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
namespace MareSynchronosStaticFilesServer.Utils;
|
||||
|
||||
public static partial class FilePathUtil
|
||||
{
|
||||
public static FileInfo GetFileInfoForHash(string basePath, string hash)
|
||||
{
|
||||
if (hash.Length != 40 || !hash.All(char.IsAsciiLetterOrDigit)) throw new InvalidOperationException();
|
||||
|
||||
FileInfo fi = new(Path.Join(basePath, hash[0].ToString(), hash));
|
||||
if (!fi.Exists)
|
||||
{
|
||||
fi = new FileInfo(Path.Join(basePath, hash));
|
||||
if (!fi.Exists)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return fi;
|
||||
}
|
||||
|
||||
public static string GetFilePath(string basePath, string hash)
|
||||
{
|
||||
if (hash.Length != 40 || !hash.All(char.IsAsciiLetterOrDigit)) throw new InvalidOperationException();
|
||||
|
||||
var dirPath = Path.Join(basePath, hash[0].ToString());
|
||||
var path = Path.Join(dirPath, hash);
|
||||
if (!Directory.Exists(dirPath)) Directory.CreateDirectory(dirPath);
|
||||
return path;
|
||||
}
|
||||
}
|
@@ -0,0 +1,82 @@
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace MareSynchronosStaticFilesServer.Utils;
|
||||
|
||||
// Calculates the hash of content read or written to a stream
|
||||
public class HashingStream : Stream
|
||||
{
|
||||
private readonly Stream _stream;
|
||||
private readonly HashAlgorithm _hashAlgo;
|
||||
private bool _finished = false;
|
||||
public bool DisposeUnderlying = true;
|
||||
|
||||
public Stream UnderlyingStream { get => _stream; }
|
||||
|
||||
public HashingStream(Stream underlyingStream, HashAlgorithm hashAlgo)
|
||||
{
|
||||
_stream = underlyingStream;
|
||||
_hashAlgo = hashAlgo;
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (!DisposeUnderlying)
|
||||
return;
|
||||
if (!_finished)
|
||||
_stream.Dispose();
|
||||
_hashAlgo.Dispose();
|
||||
}
|
||||
|
||||
public override bool CanRead => _stream.CanRead;
|
||||
public override bool CanSeek => false;
|
||||
public override bool CanWrite => _stream.CanWrite;
|
||||
public override long Length => _stream.Length;
|
||||
|
||||
public override long Position { get => _stream.Position; set => throw new NotSupportedException(); }
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
_stream.Flush();
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
if (_finished)
|
||||
throw new ObjectDisposedException("HashingStream");
|
||||
int n = _stream.Read(buffer, offset, count);
|
||||
if (n > 0)
|
||||
_hashAlgo.TransformBlock(buffer, offset, n, buffer, offset);
|
||||
return n;
|
||||
}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
if (_finished)
|
||||
throw new ObjectDisposedException("HashingStream");
|
||||
_stream.SetLength(value);
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
if (_finished)
|
||||
throw new ObjectDisposedException("HashingStream");
|
||||
_stream.Write(buffer, offset, count);
|
||||
string x = new(System.Text.Encoding.ASCII.GetChars(buffer.AsSpan().Slice(offset, count).ToArray()));
|
||||
_hashAlgo.TransformBlock(buffer, offset, count, buffer, offset);
|
||||
}
|
||||
|
||||
public byte[] Finish()
|
||||
{
|
||||
if (_finished)
|
||||
return _hashAlgo.Hash;
|
||||
_hashAlgo.TransformFinalBlock(Array.Empty<byte>(), 0, 0);
|
||||
if (DisposeUnderlying)
|
||||
_stream.Dispose();
|
||||
return _hashAlgo.Hash;
|
||||
}
|
||||
}
|
@@ -0,0 +1,76 @@
|
||||
using MareSynchronosShared.Metrics;
|
||||
using MareSynchronosShared.Services;
|
||||
using MareSynchronosShared.Utils.Configuration;
|
||||
using MareSynchronosStaticFilesServer.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
|
||||
namespace MareSynchronosStaticFilesServer.Utils;
|
||||
|
||||
public class RequestBlockFileListResult : IActionResult
|
||||
{
|
||||
private readonly Guid _requestId;
|
||||
private readonly RequestQueueService _requestQueueService;
|
||||
private readonly MareMetrics _mareMetrics;
|
||||
private readonly IEnumerable<FileInfo> _fileList;
|
||||
private readonly IConfigurationService<StaticFilesServerConfiguration> _configurationService;
|
||||
|
||||
public RequestBlockFileListResult(Guid requestId, RequestQueueService requestQueueService, MareMetrics mareMetrics, IEnumerable<FileInfo> fileList,
|
||||
IConfigurationService<StaticFilesServerConfiguration> configurationService)
|
||||
{
|
||||
_requestId = requestId;
|
||||
_requestQueueService = requestQueueService;
|
||||
_mareMetrics = mareMetrics;
|
||||
_mareMetrics.IncGauge(MetricsAPI.GaugeCurrentDownloads);
|
||||
_fileList = fileList;
|
||||
_configurationService = configurationService;
|
||||
}
|
||||
|
||||
public async Task ExecuteResultAsync(ActionContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(context);
|
||||
|
||||
var useSSI = _configurationService.GetValueOrDefault(nameof(StaticFilesServerConfiguration.UseSSI), false);
|
||||
|
||||
context.HttpContext.Response.StatusCode = 200;
|
||||
|
||||
if (useSSI)
|
||||
context.HttpContext.Response.ContentType = _configurationService.GetValue<string>(nameof(StaticFilesServerConfiguration.SSIContentType));
|
||||
else
|
||||
context.HttpContext.Response.ContentType = "application/octet-stream";
|
||||
|
||||
string ssiFilePrefix = null;
|
||||
|
||||
if (useSSI)
|
||||
ssiFilePrefix = _configurationService.GetValue<string>(nameof(StaticFilesServerConfiguration.XAccelRedirectPrefix));
|
||||
|
||||
foreach (var file in _fileList)
|
||||
{
|
||||
if (useSSI)
|
||||
{
|
||||
var internalName = Path.Combine(ssiFilePrefix, file.Name);
|
||||
await context.HttpContext.Response.WriteAsync(
|
||||
"#" + file.Name + ":" + file.Length.ToString(CultureInfo.InvariantCulture) + "#"
|
||||
+ "<!--#include file=\"" + internalName + "\" -->", Encoding.ASCII);
|
||||
}
|
||||
else
|
||||
{
|
||||
await context.HttpContext.Response.WriteAsync("#" + file.Name + ":" + file.Length.ToString(CultureInfo.InvariantCulture) + "#", Encoding.ASCII);
|
||||
await context.HttpContext.Response.SendFileAsync(file.FullName);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_requestQueueService.FinishRequest(_requestId);
|
||||
_mareMetrics.DecGauge(MetricsAPI.GaugeCurrentDownloads);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,25 @@
|
||||
using MareSynchronosShared.Metrics;
|
||||
using MareSynchronosShared.Services;
|
||||
using MareSynchronosShared.Utils.Configuration;
|
||||
using MareSynchronosStaticFilesServer.Services;
|
||||
|
||||
namespace MareSynchronosStaticFilesServer.Utils;
|
||||
|
||||
public class RequestBlockFileListResultFactory
|
||||
{
|
||||
private readonly MareMetrics _metrics;
|
||||
private readonly RequestQueueService _requestQueueService;
|
||||
private readonly IConfigurationService<StaticFilesServerConfiguration> _configurationService;
|
||||
|
||||
public RequestBlockFileListResultFactory(MareMetrics metrics, RequestQueueService requestQueueService, IConfigurationService<StaticFilesServerConfiguration> configurationService)
|
||||
{
|
||||
_metrics = metrics;
|
||||
_requestQueueService = requestQueueService;
|
||||
_configurationService = configurationService;
|
||||
}
|
||||
|
||||
public RequestBlockFileListResult Create(Guid requestId, IEnumerable<FileInfo> fileList)
|
||||
{
|
||||
return new RequestBlockFileListResult(requestId, _requestQueueService, _metrics, fileList, _configurationService);
|
||||
}
|
||||
}
|
@@ -0,0 +1,74 @@
|
||||
namespace MareSynchronosStaticFilesServer.Utils;
|
||||
|
||||
// Writes data read from one stream out to a second stream
|
||||
public class TeeStream : Stream
|
||||
{
|
||||
private readonly Stream _inStream;
|
||||
private readonly Stream _outStream;
|
||||
public bool DisposeUnderlying = true;
|
||||
|
||||
public Stream InStream { get => _inStream; }
|
||||
public Stream OutStream { get => _outStream; }
|
||||
|
||||
public TeeStream(Stream inStream, Stream outStream)
|
||||
{
|
||||
_inStream = inStream;
|
||||
_outStream = outStream;
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (!DisposeUnderlying)
|
||||
return;
|
||||
_inStream.Dispose();
|
||||
_outStream.Dispose();
|
||||
}
|
||||
|
||||
public override bool CanRead => _inStream.CanRead;
|
||||
public override bool CanSeek => _inStream.CanSeek;
|
||||
public override bool CanWrite => false;
|
||||
public override long Length => _inStream.Length;
|
||||
|
||||
public override long Position
|
||||
{
|
||||
get => _inStream.Position;
|
||||
set => _inStream.Position = value;
|
||||
}
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
_inStream.Flush();
|
||||
_outStream.Flush();
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
int n = _inStream.Read(buffer, offset, count);
|
||||
if (n > 0)
|
||||
_outStream.Write(buffer, offset, n);
|
||||
return n;
|
||||
}
|
||||
|
||||
public async override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
|
||||
{
|
||||
int n = await _inStream.ReadAsync(buffer, offset, count, cancellationToken);
|
||||
if (n > 0)
|
||||
await _outStream.WriteAsync(buffer, offset, n);
|
||||
return n;
|
||||
}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
return _inStream.Seek(offset, origin);
|
||||
}
|
||||
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
_inStream.SetLength(value);
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
@@ -0,0 +1,21 @@
|
||||
namespace MareSynchronosStaticFilesServer.Utils;
|
||||
|
||||
public class UserQueueEntry
|
||||
{
|
||||
public UserQueueEntry(UserRequest userRequest, DateTime expirationDate)
|
||||
{
|
||||
UserRequest = userRequest;
|
||||
ExpirationDate = expirationDate;
|
||||
}
|
||||
|
||||
public void MarkActive()
|
||||
{
|
||||
IsActive = true;
|
||||
ActivationDate = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
public UserRequest UserRequest { get; }
|
||||
public DateTime ExpirationDate { get; }
|
||||
public bool IsActive { get; private set; } = false;
|
||||
public DateTime ActivationDate { get; private set; }
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
namespace MareSynchronosStaticFilesServer.Utils;
|
||||
|
||||
public record UserRequest(Guid RequestId, string User, List<string> FileIds)
|
||||
{
|
||||
public bool IsCancelled { get; set; } = false;
|
||||
}
|
Reference in New Issue
Block a user