initial commit

This commit is contained in:
2026-03-20 23:52:10 +01:00
parent 05bea695bd
commit ce04cd8d77
38 changed files with 3006 additions and 52 deletions

View File

@@ -0,0 +1,54 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
namespace CityInfo.API.Controllers
{
[Route("api/files")]
[Authorize]
[ApiController]
public class FilesController : ControllerBase
{
private readonly FileExtensionContentTypeProvider _fileExtensionContentTypeProvide;
public FilesController(
FileExtensionContentTypeProvider fileExtensionContentTypeProvider)
{
_fileExtensionContentTypeProvide = fileExtensionContentTypeProvider
?? throw new System.ArgumentNullException(
nameof(FileExtensionContentTypeProvider));
}
[HttpGet("{fileId}")]
public ActionResult GetFile(string fileId)
{
var pathToFile = "email_template.pdf";
if (!System.IO.File.Exists(pathToFile))
{
return NotFound();
}
if (!_fileExtensionContentTypeProvide
.TryGetContentType(pathToFile, out var contentType))
{
contentType = "application/octet-stream";
}
var bytes = System.IO.File.ReadAllBytes(pathToFile);
return File(bytes, contentType , Path.GetFileName(pathToFile));
}
[HttpPost]
public async Task<ActionResult> CreateFile(IFormFile file)
{
if(file.Length == 0 || file.Length > 20971520 || file.ContentType != "application/pdf")
{
return BadRequest("No file or an invalid one has been inputted.");
}
var path = Path.Combine(
Directory.GetCurrentDirectory(),
$"uploaded_file_{Guid.NewGuid()}.pdf");
using (var stream = new FileStream(path, FileMode.Create))
{
await file.CopyToAsync(stream);
}
return Ok("Your file has been uploaded successfully.");
}
}
}