2024-07-03 23:48:57 -05:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
|
using LANCommander.SDK.Helpers;
|
2024-08-04 18:44:33 -05:00
|
|
|
|
using LANCommander.Server.Services;
|
2023-09-13 23:29:59 -05:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2024-07-03 23:48:57 -05:00
|
|
|
|
using System.IO.Compression;
|
2023-09-13 23:29:59 -05:00
|
|
|
|
|
2024-08-04 18:44:33 -05:00
|
|
|
|
namespace LANCommander.Server.Controllers
|
2023-09-13 23:29:59 -05:00
|
|
|
|
{
|
2024-08-28 20:33:59 -05:00
|
|
|
|
public class ServerController : BaseController
|
2023-09-13 23:29:59 -05:00
|
|
|
|
{
|
|
|
|
|
|
private readonly ServerService ServerService;
|
2024-07-03 23:48:57 -05:00
|
|
|
|
private readonly IMapper Mapper;
|
2023-09-13 23:29:59 -05:00
|
|
|
|
|
2024-08-28 20:33:59 -05:00
|
|
|
|
public ServerController(
|
|
|
|
|
|
ILogger<ServerController> logger,
|
2025-11-16 12:31:25 -06:00
|
|
|
|
SettingsProvider<Settings.Settings> settingsProvider,
|
2024-08-28 20:33:59 -05:00
|
|
|
|
IMapper mapper,
|
2025-11-16 12:31:25 -06:00
|
|
|
|
ServerService serverService) : base(logger, settingsProvider)
|
2023-09-13 23:29:59 -05:00
|
|
|
|
{
|
2024-07-03 23:48:57 -05:00
|
|
|
|
ServerService = serverService;
|
|
|
|
|
|
Mapper = mapper;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-13 23:29:59 -05:00
|
|
|
|
[HttpGet("/Server/{id:guid}/{*path}")]
|
2024-11-12 23:22:01 -06:00
|
|
|
|
public async Task<IActionResult> WebAsync(Guid id, string path)
|
2023-09-13 23:29:59 -05:00
|
|
|
|
{
|
2024-12-20 11:34:59 -06:00
|
|
|
|
var server = await ServerService
|
|
|
|
|
|
.Include(s => s.HttpPaths)
|
|
|
|
|
|
.GetAsync(id);
|
2023-09-13 23:29:59 -05:00
|
|
|
|
|
|
|
|
|
|
if (server == null)
|
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
2023-11-04 19:43:38 -05:00
|
|
|
|
if (server.HttpPaths == null || server.HttpPaths.Count == 0)
|
|
|
|
|
|
return NotFound();
|
2023-09-13 23:29:59 -05:00
|
|
|
|
|
2023-11-04 19:43:38 -05:00
|
|
|
|
// Sanitize
|
|
|
|
|
|
if (path == null)
|
|
|
|
|
|
path = "/";
|
2023-09-13 23:29:59 -05:00
|
|
|
|
|
2023-11-04 19:43:38 -05:00
|
|
|
|
path = path.Trim('/');
|
|
|
|
|
|
path = path + "/";
|
|
|
|
|
|
|
|
|
|
|
|
var httpPath = server.HttpPaths.FirstOrDefault(hp => path.StartsWith(hp.Path.TrimStart('/')));
|
|
|
|
|
|
|
|
|
|
|
|
// Check to see if there's a root path defined if nothing else matches
|
|
|
|
|
|
if (httpPath == null)
|
|
|
|
|
|
httpPath = server.HttpPaths.FirstOrDefault(hp => hp.Path == "/");
|
|
|
|
|
|
|
|
|
|
|
|
if (httpPath == null)
|
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
|
|
|
|
|
|
var relativePath = path.Substring(httpPath.Path.TrimStart('/').Length).Replace('/', Path.DirectorySeparatorChar).TrimStart('\\');
|
|
|
|
|
|
|
|
|
|
|
|
var localPath = Path.Combine(httpPath.LocalPath, relativePath).TrimEnd('\\');
|
|
|
|
|
|
var attrs = System.IO.File.GetAttributes(localPath);
|
|
|
|
|
|
|
|
|
|
|
|
if ((attrs & FileAttributes.Directory) == FileAttributes.Directory)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!System.IO.Directory.Exists(localPath))
|
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
|
|
|
|
|
return Json(Directory.GetFileSystemEntries(localPath).Select(fse => fse.Substring(localPath.Length)));
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!System.IO.File.Exists(localPath))
|
|
|
|
|
|
return NotFound();
|
2023-09-13 23:29:59 -05:00
|
|
|
|
|
2023-11-04 19:43:38 -05:00
|
|
|
|
return File(new FileStream(localPath, FileMode.Open, FileAccess.Read, FileShare.Read), "application/octet-stream", Path.GetFileName(localPath));
|
|
|
|
|
|
}
|
2023-09-13 23:29:59 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|