From 927dc4f52dabbd04eb8647a522cd9c670c974d19 Mon Sep 17 00:00:00 2001 From: Marco Audiffredi Date: Thu, 18 Sep 2025 14:59:07 +0200 Subject: [PATCH] gestione Upload Foto --- ApiPolo/Controllers/PoloController.cs | 175 ++++++++++++++++++++++++++ ApiPolo/Models/Configurazione_out.cs | 17 +++ ApiPolo/Models/Configurazioni.cs | 17 +++ ApiPolo/Startup.cs | 2 +- ApiPolo/Storico versioni.txt | 4 + ApiPolo/appsettings.json | 8 +- 6 files changed, 219 insertions(+), 4 deletions(-) diff --git a/ApiPolo/Controllers/PoloController.cs b/ApiPolo/Controllers/PoloController.cs index 5800d44..9b997f5 100644 --- a/ApiPolo/Controllers/PoloController.cs +++ b/ApiPolo/Controllers/PoloController.cs @@ -379,6 +379,13 @@ namespace ApiPolo.Controllers c.desc_interv_stampa = a.desc_interv_stampa != null ? a.desc_interv_stampa : false; c.note_interv_stampa = a.note_interv_stampa != null ? a.note_interv_stampa :false; + + c.ftp_url_stor = a.ftp_url_stor != null ? a.ftp_url_stor : string.Empty; + c.ftp_usr_stor = a.ftp_usr_stor != null ? a.ftp_usr_stor : string.Empty; + c.ftp_pwd_stor = a.ftp_pwd_stor != null ? a.ftp_pwd_stor : string.Empty; + c.root_stor = a.root_stor != null ? a.root_stor : string.Empty; + c.abilita_foto = a.abilita_foto != null ? a.abilita_foto : false; + } return c; } @@ -2851,6 +2858,7 @@ namespace ApiPolo.Controllers string ten = getClaimValueByToken(token, "tenant"); string tecnico = getClaimValueByToken(token, "tccodice"); string tenConf = getClaimValueByToken(token, "tenantConfigurazioni"); + if (string.IsNullOrEmpty(ten)) { ten = tenConf; @@ -6815,6 +6823,31 @@ namespace ApiPolo.Controllers files.CopyTo(ftpStream); } } + private static async Task FtpSendStorage(string ftpUrl, string ftp_usr, string ftp_pwd, IFormFile files, string filename) + { + //ftpUrl = "ftp://u460585.your-storagebox.de/MARRO/" + filename; + //ftp_usr = "u460585"; + //ftp_pwd = "FygAh5sGcQbAt9aV"; + + ftpUrl = ftpUrl + filename; + FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpUrl); + request.Method = WebRequestMethods.Ftp.UploadFile; + request.Credentials = new NetworkCredential(ftp_usr, ftp_pwd); + request.UseBinary = true; + request.KeepAlive = false; + request.ContentLength = files.Length; + + using (Stream requestStream = request.GetRequestStream()) + using (Stream fileStream = files.OpenReadStream()) + { + await fileStream.CopyToAsync(requestStream); + } + + using (FtpWebResponse response = (FtpWebResponse)await request.GetResponseAsync()) + { + Console.WriteLine($"Upload completato, stato: {response.StatusDescription}"); + } + } private static string CheckFtp(string url, string ftp_usr, string ftp_pwd) { string _esito = "OK"; @@ -6881,6 +6914,67 @@ namespace ApiPolo.Controllers } } } + /// + private async Task updPathFoto(string tenant, string seriale_rapportino, string pathFoto, int posizione, Rapp_New entitasViewModel) + { + using (var transaction = _VT_rapptable.Database.BeginTransaction()) + { + switch (posizione) + { + case 1: + entitasViewModel.rafoto1 = pathFoto; + break; + case 2: + entitasViewModel.rafoto2 = pathFoto; + break; + case 3: + entitasViewModel.rafoto3 = pathFoto; + break; + case 4: + entitasViewModel.rafoto4 = pathFoto; + break; + case 5: + entitasViewModel.rafoto5 = pathFoto; + break; + case 6: + entitasViewModel.rafoto6 = pathFoto; + break; + case 7: + entitasViewModel.rafoto7 = pathFoto; + break; + case 8: + entitasViewModel.rafoto8 = pathFoto; + break; + case 9: + entitasViewModel.rafoto9 = pathFoto; + break; + case 10: + entitasViewModel.rafoto10 = pathFoto; + break; + } + _VT_rapptable.Entry(entitasViewModel).State = EntityState.Modified; + await _VT_rapptable.SaveChangesAsync(); + transaction.Commit(); + } + return entitasViewModel; + } + private int TrovaPrimoCampoLibero(Rapp_New entity) + { + int pos = -1; + + if (string.IsNullOrEmpty(entity.rafoto1)) return 1; + if (string.IsNullOrEmpty(entity.rafoto2)) return 2; + if (string.IsNullOrEmpty(entity.rafoto3)) return 3; + if (string.IsNullOrEmpty(entity.rafoto4)) return 4; + if (string.IsNullOrEmpty(entity.rafoto5)) return 5; + if (string.IsNullOrEmpty(entity.rafoto6)) return 6; + if (string.IsNullOrEmpty(entity.rafoto7)) return 7; + if (string.IsNullOrEmpty(entity.rafoto8)) return 8; + if (string.IsNullOrEmpty(entity.rafoto9)) return 9; + if (string.IsNullOrEmpty(entity.rafoto10)) return 10; + + return pos; // tutti pieni + } /// Upload return:ActionResult [HttpPost] @@ -7173,6 +7267,83 @@ namespace ApiPolo.Controllers } } + /// Upload foto return:ActionResult + [HttpPost] + [Route("{serialeRapportino}/uploadFoto")] + public async Task UploadFoto(string serialeRapportino, IFormFile file, string token) + { + string ten = getClaimValueByToken(token, "tenant"); + string ten2 = getClaimValueByToken(token, "tenant2"); + string tenConf = getClaimValueByToken(token, "tenantConfigurazioni"); + string tecnico = getClaimValueByToken(token, "tccodice"); + string url = string.Empty; + string usr = string.Empty; + string pwd = string.Empty; + string url_storage = string.Empty; + string usr_storage = string.Empty; + string pwd_storage = string.Empty; + string ip = string.Empty; + int? port = 0; + string fileName = string.Empty; + + if (file == null || file.Length == 0) + return StatusCode(StatusCodes.Status500InternalServerError, "Error: file non valido"); + + try + { + if (ten.Equals(Clienti.VT)) + { + _confLette = await readConf(ten2); + } + else + { + _confLette = await readConf(tenConf); + } + _rapp_new = _VT_rapptable.Rapps; + Rapp_New entitasViewModel = _rapp_new.Where(p => p.seriale_rapportino.Equals(serialeRapportino)).FirstOrDefault(); + if (entitasViewModel == null) + { + return StatusCode(StatusCodes.Status500InternalServerError, "Errore: seriale non valido " + serialeRapportino); + } + + int posiz = TrovaPrimoCampoLibero(entitasViewModel); + string strPosiz = Convert.ToString(posiz); + + if (posiz < 1 || posiz > 10) + return StatusCode(StatusCodes.Status500InternalServerError, "Raggiunto limite max numero foto"); + + long size = file.Length; + string root = _confLette.root_stor != null ? _confLette.root_stor.Trim() : string.Empty; + fileName = String.Format("{0}_{1}{2}", serialeRapportino, strPosiz, Path.GetExtension(file.FileName)); + //string relativePath = $"/uploads/{serialeRapportino}/{fileName}"; + + string absolutePath = root + fileName; + url_storage = _confLette.ftp_url_stor != null ? _confLette.ftp_url_stor.Trim() : string.Empty; + usr_storage = _confLette.ftp_usr_stor != null ? _confLette.ftp_usr_stor.Trim() : string.Empty; + pwd_storage = _confLette.ftp_pwd_stor != null ? _confLette.ftp_pwd_stor.Trim() : string.Empty; + + await FtpSendStorage(url_storage, usr_storage, pwd_storage, file, fileName); + await updPathFoto(ten, serialeRapportino, absolutePath, posiz, entitasViewModel); + + + return Ok(new { count = posiz, size }); + } + catch (Exception ex) + { + StringBuilder sb = new StringBuilder(); + string errMsg = string.Empty; + if (ex.Message != null) + { + sb.AppendLine(ex.Message); + } + if (ex.InnerException != null) + { + sb.AppendLine(ex.InnerException.Message); + } + errMsg = sb.ToString(); + return StatusCode(StatusCodes.Status500InternalServerError, "Error in uploadFoto: " + errMsg); + } + } #endregion #region IMPIANTI COMPONENTI @@ -12419,6 +12590,10 @@ namespace ApiPolo.Controllers //} _chiamate = getChiamateByTenant(ten); + var chiamata_new = _chiamate.Where(x => x.chserial!=null && x.chserial.Equals(t.chserial)).FirstOrDefault(); + c = fillChiamateOut(chiamata_new, ten); + + //switch (ten) //{ // case Clienti.LW: diff --git a/ApiPolo/Models/Configurazione_out.cs b/ApiPolo/Models/Configurazione_out.cs index 2fb3523..72d6c1c 100644 --- a/ApiPolo/Models/Configurazione_out.cs +++ b/ApiPolo/Models/Configurazione_out.cs @@ -212,5 +212,22 @@ namespace ApiPolo.Models /// Status public string? err_status_code { get; set; } + + + + /// url ftp per upload delle immagini nello STORAGE WEB + public string? ftp_url_stor { get; set; } + + /// utente ftp per upload delle immagini nello STORAGE WEB + public string? ftp_usr_stor { get; set; } + + /// password ftp per upload delle immagini nello STORAGE WEB + public string? ftp_pwd_stor { get; set; } + + /// abilita l'upload delle foto impianto + public bool? abilita_foto { get; set; } + + /// root (mappatura) dello STORAGE WEB + public string? root_stor { get; set; } } } diff --git a/ApiPolo/Models/Configurazioni.cs b/ApiPolo/Models/Configurazioni.cs index a50ec5f..5513bcc 100644 --- a/ApiPolo/Models/Configurazioni.cs +++ b/ApiPolo/Models/Configurazioni.cs @@ -219,5 +219,22 @@ namespace ApiPolo.Models /// stampa note intervento nel buono public bool? desc_interv_stampa { get; set; } + + + /// url ftp per upload delle immagini nello STORAGE WEB + public string? ftp_url_stor { get; set; } + + /// utente ftp per upload delle immagini nello STORAGE WEB + public string? ftp_usr_stor { get; set; } + + /// password ftp per upload delle immagini nello STORAGE WEB + public string? ftp_pwd_stor { get; set; } + + /// abilita l'upload delle foto impianto + public bool? abilita_foto { get; set; } + + /// root (mappatura) dello STORAGE WEB + public string? root_stor { get; set; } + } } diff --git a/ApiPolo/Startup.cs b/ApiPolo/Startup.cs index 8cc17ce..ce89de9 100644 --- a/ApiPolo/Startup.cs +++ b/ApiPolo/Startup.cs @@ -145,7 +145,7 @@ namespace ApiPolo //gen.SwaggerDoc("v1", new OpenApiInfo { Title = "POLO API WS2016", Version = "v3.3" }); DateTime oggi = DateTime.Now; string dt = oggi.ToString(); - gen.SwaggerDoc("v1", new OpenApiInfo { Title = "API VIRTUAL TASK - v.app 1.30", Version = dt }); + gen.SwaggerDoc("v1", new OpenApiInfo { Title = "API VIRTUAL TASK - v.app 1.34", Version = dt }); var filePath = Path.Combine(System.AppContext.BaseDirectory, "ApiPolo.xml"); gen.IncludeXmlComments(filePath); diff --git a/ApiPolo/Storico versioni.txt b/ApiPolo/Storico versioni.txt index a9b923b..a77f11a 100644 --- a/ApiPolo/Storico versioni.txt +++ b/ApiPolo/Storico versioni.txt @@ -5,4 +5,8 @@ la chiamata appena inserita per poterla poi lavorare come una normale chiamata e - aggiunta la gestione da Api della parte del buono che descrive l'impianto, al solito in formato html campo riferimento_impianto su Chiamate_out +************************************************************************************* +verione 1.34 + +- Upload foto ************************************************************************************* \ No newline at end of file diff --git a/ApiPolo/appsettings.json b/ApiPolo/appsettings.json index 38fec48..fe4e343 100644 --- a/ApiPolo/appsettings.json +++ b/ApiPolo/appsettings.json @@ -11,9 +11,11 @@ //connessione non sicura: Encrypt=False //"ApiStr": "Data Source=172.25.30.1;Initial Catalog=API_POLO;User Id=sa; Password=p0l01nf.;Integrated Security=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False", - "ApiStr": "Data Source=dbsql01.poloinformatico.it;Initial Catalog=API_POLO;User Id=apipolo; Password=38HdflydkDrXI4l;Integrated Security=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;Encrypt=False;TrustServerCertificate=True", - "VIRTUAL_TASK": "Data Source=dbsql01.poloinformatico.it;Initial Catalog=VIRTUAL_TASK;User Id=apipolo; Password=38HdflydkDrXI4l;Integrated Security=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;Encrypt=False;TrustServerCertificate=True", - /*"VIRTUAL_TASK": "Data Source=MARCO_PC\\SQL_2022;Initial Catalog=VIRTUAL_TASK;User Id=sa; Password=p0l01nf.;Integrated Security=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False",*/ + //"ApiStr": "Data Source=dbsql01.poloinformatico.it;Initial Catalog=API_POLO;User Id=apipolo; Password=38HdflydkDrXI4l;Integrated Security=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;Encrypt=False;TrustServerCertificate=True", + "ApiStr": "Data Source=MARCO_PC\\SQL_2022;Initial Catalog=API_POLO;User Id=sa; Password=p0l01nf.;Integrated Security=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;Encrypt=False;TrustServerCertificate=True", + + //"VIRTUAL_TASK": "Data Source=dbsql01.poloinformatico.it;Initial Catalog=VIRTUAL_TASK;User Id=apipolo; Password=38HdflydkDrXI4l;Integrated Security=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;Encrypt=False;TrustServerCertificate=True" + "VIRTUAL_TASK": "Data Source=MARCO_PC\\SQL_2022;Initial Catalog=VIRTUAL_TASK;User Id=sa; Password=p0l01nf.;Integrated Security=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False", //"VIRTUAL_TASK": "Data Source=172.25.30.1;Initial Catalog=VIRTUAL_TASK;User Id=sa; Password=p0l01nf.;Integrated Security=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" },