feat(FileController): add control to get document

This commit is contained in:
tekh 2025-08-04 17:50:58 +02:00
parent d4ea68fc0e
commit b71e451121
3 changed files with 70 additions and 1 deletions

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,57 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Web;
namespace WorkFlow.API.Controllers;
[Route("api/[controller]")]
[ApiController]
[Authorize]
public class FileController : ControllerBase
{
[HttpGet("{path}")]
public IActionResult GetFile([FromRoute] string path, [FromQuery] bool icon = false)
{
string dPath = HttpUtility.UrlDecode(path);
byte[]? fileBytes = null;
string? contentType = null;
if (dPath == "docs/doc1.pdf" && !icon)
{
fileBytes = Convert.FromBase64String(Base64File.Doc1Base64);
contentType = "application/pdf";
}
else if (dPath == "icons/icon1.png" && icon)
{
fileBytes = Convert.FromBase64String(Base64File.Icon1Base64);
contentType = "image/png";
}
else
{
string fullPath = Path.Combine(AppContext.BaseDirectory, "files", dPath);
if (!System.IO.File.Exists(fullPath))
return NotFound();
fileBytes = System.IO.File.ReadAllBytes(fullPath);
contentType = GetContentType(fullPath);
}
return File(fileBytes, contentType ?? "application/octet-stream");
}
private string GetContentType(string path)
{
var ext = Path.GetExtension(path).ToLowerInvariant();
return ext switch
{
".pdf" => "application/pdf",
".png" => "image/png",
".jpg" => "image/jpeg",
".jpeg" => "image/jpeg",
".txt" => "text/plain",
".docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
_ => "application/octet-stream",
};
}
}

View File

@ -8,7 +8,7 @@
},
"TfFileIconUri": {
"Scheme": "https",
"Host": " dd-gan.digitaldata.works",
"Host": "dd-gan.digitaldata.works",
"Port": 8443,
"Path": "api/file",
"Query": "icon=true"