55 lines
2.0 KiB
C#
55 lines
2.0 KiB
C#
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.");
|
|
}
|
|
}
|
|
}
|