Proper site manager send file method

This commit is contained in:
2025-12-01 22:55:20 +08:00
parent b99b61e0f9
commit cbd68c9ae6
2 changed files with 28 additions and 12 deletions

View File

@@ -18,6 +18,11 @@
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="MimeKit" Version="4.14.0" />
<PackageReference Include="MimeTypes" Version="2.5.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NodaTime.Serialization.Protobuf" Version="2.0.2" /> <PackageReference Include="NodaTime.Serialization.Protobuf" Version="2.0.2" />
<PackageReference Include="NodaTime" Version="3.2.2" /> <PackageReference Include="NodaTime" Version="3.2.2" />
<PackageReference Include="NodaTime.Serialization.SystemTextJson" Version="1.3.0" /> <PackageReference Include="NodaTime.Serialization.SystemTextJson" Version="1.3.0" />

View File

@@ -206,19 +206,30 @@ public class SiteManagerController(
[HttpGet("content/{**relativePath}")] [HttpGet("content/{**relativePath}")]
[Authorize] [Authorize]
public async Task<ActionResult<string>> GetFileContent(Guid siteId, string relativePath) public async Task<IActionResult> GetFileContent(Guid siteId, string relativePath)
{ {
var check = await CheckAccess(siteId); var check = await CheckAccess(siteId);
if (check != null) return check; if (check != null) return check;
string fullPath;
try try
{ {
var content = await fileManager.ReadFileContent(siteId, relativePath); fullPath = fileManager.GetValidatedFullPath(siteId, relativePath);
return Ok(content);
} }
catch (FileNotFoundException) catch (ArgumentException)
{ {
return BadRequest("Invalid path");
}
if (!System.IO.File.Exists(fullPath))
return NotFound(); return NotFound();
// Determine MIME type based on file extension
var mimeType = GetMimeType(relativePath);
try
{
return PhysicalFile(fullPath, mimeType);
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -246,18 +257,18 @@ public class SiteManagerController(
if (!System.IO.File.Exists(fullPath)) if (!System.IO.File.Exists(fullPath))
return NotFound(); return NotFound();
// Determine MIME type // Determine MIME type based on file extension
var mimeType = "application/octet-stream"; // default var mimeType = GetMimeType(relativePath);
var ext = Path.GetExtension(relativePath).ToLowerInvariant();
if (ext == ".txt") mimeType = "text/plain";
else if (ext == ".html" || ext == ".htm") mimeType = "text/html";
else if (ext == ".css") mimeType = "text/css";
else if (ext == ".js") mimeType = "application/javascript";
else if (ext == ".json") mimeType = "application/json";
return PhysicalFile(fullPath, mimeType, Path.GetFileName(relativePath)); return PhysicalFile(fullPath, mimeType, Path.GetFileName(relativePath));
} }
private static string GetMimeType(string fileName)
{
var ext = Path.GetExtension(fileName);
return MimeTypes.TryGetMimeType(ext, out var t) ? t : "application/octet-stream";
}
[HttpPut("edit/{**relativePath}")] [HttpPut("edit/{**relativePath}")]
[Authorize] [Authorize]
public async Task<IActionResult> UpdateFile(Guid siteId, string relativePath, [FromBody] UpdateFileRequest request) public async Task<IActionResult> UpdateFile(Guid siteId, string relativePath, [FromBody] UpdateFileRequest request)