feat(FileController): add control to get document
This commit is contained in:
parent
d4ea68fc0e
commit
b71e451121
12
src/WorkFlow.API/Base64File.cs
Normal file
12
src/WorkFlow.API/Base64File.cs
Normal file
File diff suppressed because one or more lines are too long
57
src/WorkFlow.API/Controllers/FileController.cs
Normal file
57
src/WorkFlow.API/Controllers/FileController.cs
Normal 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",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -8,7 +8,7 @@
|
|||||||
},
|
},
|
||||||
"TfFileIconUri": {
|
"TfFileIconUri": {
|
||||||
"Scheme": "https",
|
"Scheme": "https",
|
||||||
"Host": " dd-gan.digitaldata.works",
|
"Host": "dd-gan.digitaldata.works",
|
||||||
"Port": 8443,
|
"Port": 8443,
|
||||||
"Path": "api/file",
|
"Path": "api/file",
|
||||||
"Query": "icon=true"
|
"Query": "icon=true"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user