Merge branch 'release/FileUploadLogExt'
This commit is contained in:
@@ -1,9 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using Core;
|
||||
using Core;
|
||||
using LiMan.APi.Data;
|
||||
using LiMan.DB.DBModels;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
@@ -11,6 +6,12 @@ using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NLog;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LiMan.APi.Controllers
|
||||
{
|
||||
@@ -21,14 +22,20 @@ namespace LiMan.APi.Controllers
|
||||
[Route("api/filesave")]
|
||||
public class FilesaveController : ControllerBase
|
||||
{
|
||||
private readonly IWebHostEnvironment env;
|
||||
private readonly ILogger<FilesaveController> logger;
|
||||
#region Private Fields
|
||||
|
||||
private static IConfiguration _configuration;
|
||||
|
||||
/// <summary>
|
||||
/// Dataservice x accesso DB
|
||||
/// Classe per logging
|
||||
/// </summary>
|
||||
protected ApiDataService dataService { get; set; }
|
||||
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
||||
|
||||
private readonly IWebHostEnvironment env;
|
||||
|
||||
#endregion Private Fields
|
||||
|
||||
#region Public Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Init generico
|
||||
@@ -37,15 +44,132 @@ namespace LiMan.APi.Controllers
|
||||
/// <param name="DataService"></param>
|
||||
/// <param name="env"></param>
|
||||
/// <param name="logger"></param>
|
||||
public FilesaveController(IConfiguration configuration, ApiDataService DataService, IWebHostEnvironment env, ILogger<FilesaveController> logger)
|
||||
public FilesaveController(IConfiguration configuration, ApiDataService DataService, IWebHostEnvironment env)
|
||||
{
|
||||
dataService = DataService;
|
||||
_configuration = configuration;
|
||||
this.env = env;
|
||||
this.logger = logger;
|
||||
logger.LogInformation("Avviata classe FilesaveController");
|
||||
Log.Info("Avviata classe FilesaveController");
|
||||
}
|
||||
|
||||
#endregion Public Constructors
|
||||
|
||||
#region Protected Properties
|
||||
|
||||
/// <summary>
|
||||
/// Dataservice x accesso DB
|
||||
/// </summary>
|
||||
protected ApiDataService dataService { get; set; }
|
||||
|
||||
#endregion Protected Properties
|
||||
|
||||
#region Public Methods
|
||||
|
||||
/// <summary>
|
||||
/// Richiesta di registrazione ticket supporto
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="CurrRequest"></param>
|
||||
/// <returns></returns>
|
||||
// POST api/files/list/1
|
||||
[HttpPost("list/{id}")]
|
||||
public async Task<List<FileAttachModel>> list(int id, [FromBody] SupportRequest CurrRequest)
|
||||
{
|
||||
List<FileAttachModel> result = new List<FileAttachModel>();
|
||||
// controllo valori
|
||||
if (CurrRequest.IsValid)
|
||||
{
|
||||
// cerco i files dato ticket
|
||||
result = await dataService.FileGetFilt(id);
|
||||
await dataService.recordCall(CurrRequest.CodInst, CurrRequest.CodApp, $"POST:api/files/list:{id}");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Caricamento file effettivo via POST
|
||||
/// </summary>
|
||||
/// <param name="ticketId">TicketId x riferimento</param>
|
||||
/// <param name="files">Elenco files da caricare</param>
|
||||
/// <returns></returns>
|
||||
[HttpPost()]
|
||||
public async Task<ActionResult<IList<UploadResult>>> PostFiles([FromForm] int ticketId, [FromForm] IEnumerable<IFormFile> files)
|
||||
{
|
||||
// max 10 files
|
||||
var maxAllowedFiles = 10;
|
||||
// max 50 mb
|
||||
long maxFileSize = 1024 * 1024 * 50;
|
||||
var filesProcessed = 0;
|
||||
string ticketDir = $"T{ticketId:000000000}";
|
||||
var resourcePath = new Uri($"{Request.Scheme}://{Request.Host}/api/filesave/list/{ticketId}");
|
||||
List<UploadResult> uploadResults = new();
|
||||
string fileDir = env.ContentRootPath;
|
||||
string relDir = env.EnvironmentName;
|
||||
|
||||
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)
|
||||
{
|
||||
Log.Info($"{trustedFileNameForDisplay} length is 0 (Err: 1)");
|
||||
uploadResult.ErrorCode = 1;
|
||||
}
|
||||
else if (file.Length > maxFileSize)
|
||||
{
|
||||
Log.Info($"{trustedFileNameForDisplay} of {file.Length} bytes is larger than the limit of {maxFileSize} bytes (Err: 2)");
|
||||
uploadResult.ErrorCode = 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
DateTime oggi = DateTime.Today;
|
||||
trustedFileNameForFileStorage = Path.GetRandomFileName();
|
||||
relDir = _configuration["FileShare"];
|
||||
fileDir = Path.Combine(relDir, ticketDir);
|
||||
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);
|
||||
|
||||
Log.Info($"{trustedFileNameForDisplay} saved at {path}");
|
||||
uploadResult.Uploaded = true;
|
||||
uploadResult.StoredFileName = trustedFileNameForFileStorage;
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
Log.Error($"{trustedFileNameForDisplay} error on upload (Err: 3): {ex.Message}");
|
||||
uploadResult.ErrorCode = 3;
|
||||
}
|
||||
}
|
||||
|
||||
filesProcessed++;
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.Info($"{trustedFileNameForDisplay} not uploaded because the request exceeded the allowed {maxAllowedFiles} of files (Err: 4)");
|
||||
uploadResult.ErrorCode = 4;
|
||||
}
|
||||
|
||||
uploadResults.Add(uploadResult);
|
||||
}
|
||||
// salvo su DB
|
||||
var fatto = dataService.FileAdd(ticketId, ticketDir, uploadResults);
|
||||
Log.Info($"Ticket: {ticketId} | dir: {ticketDir} | {uploadResults.Count} files");
|
||||
|
||||
return new CreatedResult(resourcePath, uploadResults);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Caricamento file effettivo via POST
|
||||
@@ -72,12 +196,12 @@ namespace LiMan.APi.Controllers
|
||||
|
||||
if (file.Length == 0)
|
||||
{
|
||||
logger.LogInformation("{FileName} length is 0 (Err: 1)", trustedFileNameForDisplay);
|
||||
Log.Info($"{trustedFileNameForDisplay} length is 0 (Err: 1)");
|
||||
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);
|
||||
Log.Info($"{trustedFileNameForDisplay} of {file.Length} bytes is larger than the limit of {maxFileSize} bytes (Err: 2)");
|
||||
uploadResult.ErrorCode = 2;
|
||||
}
|
||||
else
|
||||
@@ -97,13 +221,13 @@ namespace LiMan.APi.Controllers
|
||||
await using FileStream fs = new(path, FileMode.Create);
|
||||
await file.CopyToAsync(fs);
|
||||
|
||||
logger.LogInformation("{FileName} saved at {Path}", trustedFileNameForDisplay, path);
|
||||
Log.Info($"{trustedFileNameForDisplay} saved at {path}");
|
||||
uploadResult.Uploaded = true;
|
||||
uploadResult.StoredFileName = trustedFileNameForFileStorage;
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
logger.LogError("{FileName} error on upload (Err: 3): {Message}", trustedFileNameForDisplay, ex.Message);
|
||||
Log.Error($"{trustedFileNameForDisplay} error on upload (Err: 3): {ex.Message}");
|
||||
uploadResult.ErrorCode = 3;
|
||||
}
|
||||
}
|
||||
@@ -111,112 +235,11 @@ namespace LiMan.APi.Controllers
|
||||
uploadResults.Add(uploadResult);
|
||||
// salvo su DB
|
||||
var fatto = dataService.FileAdd(ticketId, ticketDir, uploadResults);
|
||||
Log.Info($"Ticket: {ticketId} | dir: {ticketDir} | {uploadResults.Count} files");
|
||||
|
||||
return new CreatedResult(resourcePath, uploadResult);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Caricamento file effettivo via POST
|
||||
/// </summary>
|
||||
/// <param name="ticketId">TicketId x riferimento</param>
|
||||
/// <param name="files">Elenco files da caricare</param>
|
||||
/// <returns></returns>
|
||||
[HttpPost()]
|
||||
public async Task<ActionResult<IList<UploadResult>>> PostFiles([FromForm] int ticketId, [FromForm] IEnumerable<IFormFile> files)
|
||||
{
|
||||
// max 5 files
|
||||
var maxAllowedFiles = 10;
|
||||
// max 50 mb
|
||||
long maxFileSize = 1024 * 1024 * 50;
|
||||
var filesProcessed = 0;
|
||||
string ticketDir = $"T{ticketId:000000000}";
|
||||
var resourcePath = new Uri($"{Request.Scheme}://{Request.Host}/api/filesave/list/{ticketId}");
|
||||
List<UploadResult> uploadResults = new();
|
||||
string fileDir = env.ContentRootPath;
|
||||
string relDir = env.EnvironmentName;
|
||||
|
||||
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();
|
||||
relDir = _configuration["FileShare"];
|
||||
fileDir = Path.Combine(relDir, ticketDir);
|
||||
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);
|
||||
}
|
||||
// salvo su DB
|
||||
var fatto = dataService.FileAdd(ticketId, ticketDir, uploadResults);
|
||||
|
||||
return new CreatedResult(resourcePath, uploadResults);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Richiesta di registrazione ticket supporto
|
||||
/// </summary>
|
||||
/// <param name="CurrRequest">Obj Richiesta</param>
|
||||
// POST api/files/list/1
|
||||
[HttpPost("list/{id}")]
|
||||
public async Task<List<FileAttachModel>> list(int id, [FromBody] SupportRequest CurrRequest)
|
||||
{
|
||||
List<FileAttachModel> result = new List<FileAttachModel>();
|
||||
// controllo valori
|
||||
if (CurrRequest.IsValid)
|
||||
{
|
||||
// cerco i files dato ticket
|
||||
result = await dataService.FileGetFilt(id);
|
||||
await dataService.recordCall(CurrRequest.CodInst, CurrRequest.CodApp, $"POST:api/files/list:{id}");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
#endregion Public Methods
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ using LiMan.APi.Data;
|
||||
using LiMan.DB.DTO;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NLog;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -86,6 +87,7 @@ namespace LiMan.APi.Controllers
|
||||
// restituisco richieste aperte
|
||||
var rawResult= await dataService.TicketByCliente(CurrRequest.CodInst, CurrRequest.CodApp, CurrRequest.MasterKey, 1);
|
||||
result = rawResult.FirstOrDefault();
|
||||
Log.Info($"Ticket generated: {result.IdxTicket} | CI: {CurrRequest.CodInst} | CA: {CurrRequest.CodApp}");
|
||||
await dataService.recordCall(CurrRequest.CodInst, CurrRequest.CodApp, $"POST:api/ticket/sendReq:{CurrRequest.MasterKey}");
|
||||
return result;
|
||||
}
|
||||
|
||||
+17
-10
@@ -98,12 +98,12 @@
|
||||
Controller caricamento file
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:LiMan.APi.Controllers.FilesaveController.dataService">
|
||||
<member name="F:LiMan.APi.Controllers.FilesaveController.Log">
|
||||
<summary>
|
||||
Dataservice x accesso DB
|
||||
Classe per logging
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:LiMan.APi.Controllers.FilesaveController.#ctor(Microsoft.Extensions.Configuration.IConfiguration,LiMan.APi.Data.ApiDataService,Microsoft.AspNetCore.Hosting.IWebHostEnvironment,Microsoft.Extensions.Logging.ILogger{LiMan.APi.Controllers.FilesaveController})">
|
||||
<member name="M:LiMan.APi.Controllers.FilesaveController.#ctor(Microsoft.Extensions.Configuration.IConfiguration,LiMan.APi.Data.ApiDataService,Microsoft.AspNetCore.Hosting.IWebHostEnvironment)">
|
||||
<summary>
|
||||
Init generico
|
||||
</summary>
|
||||
@@ -112,12 +112,17 @@
|
||||
<param name="env"></param>
|
||||
<param name="logger"></param>
|
||||
</member>
|
||||
<member name="M:LiMan.APi.Controllers.FilesaveController.PostSingleFile(System.Int32,Microsoft.AspNetCore.Http.IFormFile)">
|
||||
<member name="P:LiMan.APi.Controllers.FilesaveController.dataService">
|
||||
<summary>
|
||||
Caricamento file effettivo via POST
|
||||
Dataservice x accesso DB
|
||||
</summary>
|
||||
<param name="ticketId">TicketId x riferimento</param>
|
||||
<param name="files">Elenco files da caricare</param>
|
||||
</member>
|
||||
<member name="M:LiMan.APi.Controllers.FilesaveController.list(System.Int32,Core.SupportRequest)">
|
||||
<summary>
|
||||
Richiesta di registrazione ticket supporto
|
||||
</summary>
|
||||
<param name="id"></param>
|
||||
<param name="CurrRequest"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:LiMan.APi.Controllers.FilesaveController.PostFiles(System.Int32,System.Collections.Generic.IEnumerable{Microsoft.AspNetCore.Http.IFormFile})">
|
||||
@@ -128,11 +133,13 @@
|
||||
<param name="files">Elenco files da caricare</param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:LiMan.APi.Controllers.FilesaveController.list(System.Int32,Core.SupportRequest)">
|
||||
<member name="M:LiMan.APi.Controllers.FilesaveController.PostSingleFile(System.Int32,Microsoft.AspNetCore.Http.IFormFile)">
|
||||
<summary>
|
||||
Richiesta di registrazione ticket supporto
|
||||
Caricamento file effettivo via POST
|
||||
</summary>
|
||||
<param name="CurrRequest">Obj Richiesta</param>
|
||||
<param name="ticketId">TicketId x riferimento</param>
|
||||
<param name="files">Elenco files da caricare</param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="T:LiMan.APi.Controllers.InstallazioniController">
|
||||
<summary>
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<Version>1.1.2202.0414</Version>
|
||||
<Version>1.1.2202.0415</Version>
|
||||
<RootNamespace>LiMan.UI</RootNamespace>
|
||||
<AssemblyName>LiMan.UI</AssemblyName>
|
||||
</PropertyGroup>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<body>
|
||||
<i>License Manager</i>
|
||||
<h4>Versione: 1.1.2202.0414</h4>
|
||||
<h4>Versione: 1.1.2202.0415</h4>
|
||||
<br />
|
||||
Note di rilascio:
|
||||
<ul>
|
||||
|
||||
@@ -1 +1 @@
|
||||
1.1.2202.0414
|
||||
1.1.2202.0415
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<item>
|
||||
<version>1.1.2202.0414</version>
|
||||
<version>1.1.2202.0415</version>
|
||||
<url>https://nexus.steamware.net/repository/SWS/LiMan/stable/LAST/LiMan.UI.zip</url>
|
||||
<changelog>https://nexus.steamware.net/repository/SWS/LiMan/stable/LAST/ChangeLog.html</changelog>
|
||||
<mandatory>false</mandatory>
|
||||
|
||||
Reference in New Issue
Block a user