111 lines
4.5 KiB
C#
111 lines
4.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace LiMan.APi.Controllers
|
|
{
|
|
/// <summary>
|
|
/// Controller caricamento file
|
|
/// </summary>
|
|
[ApiController]
|
|
[Route("api/filesave")]
|
|
public class FilesaveController : ControllerBase
|
|
{
|
|
private readonly IWebHostEnvironment env;
|
|
private readonly ILogger<FilesaveController> logger;
|
|
|
|
public FilesaveController(IWebHostEnvironment env,
|
|
ILogger<FilesaveController> logger)
|
|
{
|
|
this.env = env;
|
|
this.logger = logger;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Caricamento file effettivo via POST
|
|
/// </summary>
|
|
/// <param name="TicketId">TicketId x riferimento</param>
|
|
/// <param name="files"></param>
|
|
/// <returns></returns>
|
|
[HttpPost()]
|
|
public async Task<ActionResult<IList<UploadResult>>> PostFile([FromForm] string TicketId, [FromForm] IEnumerable<IFormFile> files)
|
|
{
|
|
// max 3 files
|
|
var maxAllowedFiles = 3;
|
|
var numTick = TicketId;
|
|
// max 50 mb
|
|
long maxFileSize = 1024 * 1024 * 50;
|
|
var filesProcessed = 0;
|
|
var resourcePath = new Uri($"{Request.Scheme}://{Request.Host}/");
|
|
List<UploadResult> uploadResults = new();
|
|
|
|
foreach (var file in files)
|
|
{
|
|
var uploadResult = new UploadResult();
|
|
string trustedFileNameForFileStorage;
|
|
var untrustedFileName = file.FileName;
|
|
uploadResult.FileName = untrustedFileName;
|
|
var trustedFileNameForDisplay = WebUtility.HtmlEncode(untrustedFileName);
|
|
|
|
if (filesProcessed < maxAllowedFiles)
|
|
{
|
|
if (file.Length == 0)
|
|
{
|
|
logger.LogInformation("{FileName} length is 0 (Err: 1)", trustedFileNameForDisplay);
|
|
uploadResult.ErrorCode = 1;
|
|
}
|
|
else if (file.Length > maxFileSize)
|
|
{
|
|
logger.LogInformation("{FileName} of {Length} bytes is larger than the limit of {Limit} bytes (Err: 2)", trustedFileNameForDisplay, file.Length, maxFileSize);
|
|
uploadResult.ErrorCode = 2;
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
DateTime oggi = DateTime.Today;
|
|
trustedFileNameForFileStorage = Path.GetRandomFileName();
|
|
string fileDir = Path.Combine(env.ContentRootPath, env.EnvironmentName, "unsafe_uploads", TicketId);
|
|
//string fileDir = Path.Combine(env.ContentRootPath, env.EnvironmentName, "unsafe_uploads", $"{oggi:yyyy}", $"{oggi:MM}", $"{oggi:dd}");
|
|
if (!Directory.Exists(fileDir))
|
|
{
|
|
Directory.CreateDirectory(fileDir);
|
|
}
|
|
var path = Path.Combine(fileDir, trustedFileNameForFileStorage);
|
|
|
|
await using FileStream fs = new(path, FileMode.Create);
|
|
await file.CopyToAsync(fs);
|
|
|
|
logger.LogInformation("{FileName} saved at {Path}", trustedFileNameForDisplay, path);
|
|
uploadResult.Uploaded = true;
|
|
uploadResult.StoredFileName = trustedFileNameForFileStorage;
|
|
}
|
|
catch (IOException ex)
|
|
{
|
|
logger.LogError("{FileName} error on upload (Err: 3): {Message}", trustedFileNameForDisplay, ex.Message);
|
|
uploadResult.ErrorCode = 3;
|
|
}
|
|
}
|
|
|
|
filesProcessed++;
|
|
}
|
|
else
|
|
{
|
|
logger.LogInformation("{FileName} not uploaded because the request exceeded the allowed {Count} of files (Err: 4)", trustedFileNameForDisplay, maxAllowedFiles);
|
|
uploadResult.ErrorCode = 4;
|
|
}
|
|
|
|
uploadResults.Add(uploadResult);
|
|
}
|
|
|
|
return new CreatedResult(resourcePath, uploadResults);
|
|
}
|
|
}
|
|
}
|