change StaticFilesServer to use Controller
This commit is contained in:
@@ -0,0 +1,37 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Security.Claims;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MareSynchronosStaticFilesServer
|
||||||
|
{
|
||||||
|
[Route("/cache")]
|
||||||
|
public class FilesController : Controller
|
||||||
|
{
|
||||||
|
private readonly ILogger<FilesController> logger;
|
||||||
|
private readonly IConfiguration configuration;
|
||||||
|
|
||||||
|
public FilesController(ILogger<FilesController> logger, IConfiguration configuration)
|
||||||
|
{
|
||||||
|
this.logger = logger;
|
||||||
|
this.configuration = configuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("{fileId}")]
|
||||||
|
public IActionResult GetFile(string fileId)
|
||||||
|
{
|
||||||
|
var authedUser = HttpContext.User.Claims.FirstOrDefault(f => string.Equals(f.Type, ClaimTypes.NameIdentifier, System.StringComparison.Ordinal))?.Value ?? "Unknown";
|
||||||
|
logger.LogInformation($"GetFile:{authedUser}:{fileId}");
|
||||||
|
|
||||||
|
FileInfo fi = new(Path.Combine(configuration.GetRequiredSection("MareSynchronos")["CacheDirectory"], fileId));
|
||||||
|
if (!fi.Exists) return NotFound();
|
||||||
|
|
||||||
|
var fileStream = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||||
|
|
||||||
|
return File(fileStream, "application/octet-stream");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,6 +19,10 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Grpc.AspNetCore" Version="2.50.0" />
|
<PackageReference Include="Grpc.AspNetCore" Version="2.50.0" />
|
||||||
<PackageReference Include="Grpc.Net.Client" Version="2.50.0" />
|
<PackageReference Include="Grpc.Net.Client" Version="2.50.0" />
|
||||||
|
<PackageReference Include="Meziantou.Analyzer" Version="1.0.757">
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
</PackageReference>
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.0" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="7.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="7.0.0" />
|
||||||
<PackageReference Include="prometheus-net.AspNetCore" Version="7.0.0" />
|
<PackageReference Include="prometheus-net.AspNetCore" Version="7.0.0" />
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ public class Startup
|
|||||||
|
|
||||||
var mareSettings = Configuration.GetRequiredSection("MareSynchronos");
|
var mareSettings = Configuration.GetRequiredSection("MareSynchronos");
|
||||||
|
|
||||||
//services.AddControllers();
|
services.AddControllers();
|
||||||
|
|
||||||
var defaultMethodConfig = new MethodConfig
|
var defaultMethodConfig = new MethodConfig
|
||||||
{
|
{
|
||||||
@@ -99,18 +99,10 @@ public class Startup
|
|||||||
app.UseAuthentication();
|
app.UseAuthentication();
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
|
|
||||||
app.UseStaticFiles(new StaticFileOptions()
|
|
||||||
{
|
|
||||||
FileProvider = new PhysicalFileProvider(Configuration.GetRequiredSection("MareSynchronos")["CacheDirectory"]),
|
|
||||||
RequestPath = "/cache",
|
|
||||||
ServeUnknownFileTypes = true,
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
app.UseEndpoints(e =>
|
app.UseEndpoints(e =>
|
||||||
{
|
{
|
||||||
e.MapGrpcService<FileService>();
|
e.MapGrpcService<FileService>();
|
||||||
//e.MapControllers();
|
e.MapControllers();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user