- ok spedizione ZIP
- ok password zip
- ok test in DEV
This commit is contained in:
Samuele Locatelli
2024-10-18 15:10:29 +02:00
parent 7d2dd68344
commit 76708b49fb
9 changed files with 193 additions and 133 deletions
+17
View File
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MP.AppAuth.DTO
{
public class UploadResult
{
public bool Uploaded { get; set; }
public string FileName { get; set; } = "";
public string StoredFileName { get; set; } = "";
public int ErrorCode { get; set; }
}
}
+71 -9
View File
@@ -2,13 +2,18 @@
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using MP.AppAuth.DTO;
using MP.AppAuth.Models;
using Newtonsoft.Json;
using NLog;
using RestSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using static Egw.Core.LiManObj;
using static System.Net.Mime.MediaTypeNames;
namespace MP.Land.Data
@@ -21,10 +26,8 @@ namespace MP.Land.Data
/// Init classe
/// </summary>
/// <param name="configuration"></param>
/// <param name="logger"></param>
public SyncService(IConfiguration configuration, ILogger<LicenseService> logger)
public SyncService(IConfiguration configuration)
{
_logger = logger;
_configuration = configuration;
}
@@ -147,6 +150,55 @@ namespace MP.Land.Data
return await Task.FromResult(answ);
}
/// <summary>
/// Invio file zip di backup al server centrale
/// </summary>
/// <param name="CodApp">Cod Applicazione</param>
/// <param name="CodInst">CLiente / Installazione</param>
/// <param name="DoUnzip">Invia richiesta UnZip</param>
/// <param name="ForceApprov">Invia richiesta Approvazione</param>
/// <param name="ZipFile">ZipFile da inviare</param>
/// <returns></returns>
public async Task<bool> SendZipFile(string CodApp, string CodInst, bool DoUnzip, bool ForceApprov, FileInfo ZipFile)
{
bool answ = false;
try
{
// client chiamate rest
var client = new RestClient(restOptStd);
// Chiamo il metodo!
var actReq = new RestRequest($"/api/filesave/zipbackup", Method.Post);
actReq.AddParameter("CodApp", $"{CodApp}");
actReq.AddParameter("CodInst", $"{CodInst}");
actReq.AddParameter("DoUnzip", $"{DoUnzip}");
actReq.AddParameter("ForceApprov", $"{ForceApprov}");
actReq.AddFile("ZipFile", ZipFile.FullName);
actReq.AddHeader("Content-Type", "multipart/form-data");
// effettuo vera chiamata
var currResp = await client.ExecuteAsync(actReq);
if (currResp.StatusCode == System.Net.HttpStatusCode.OK && currResp.Content != null)
{
// mi restituisce esito upload
var currList = JsonConvert.DeserializeObject<List<UploadResult>>(currResp.Content);
if (currList != null)
{
// se contiene anche la richiesta è ok...
var recUpd = currList.FirstOrDefault(x => x.FileName == ZipFile.Name);
if (recUpd != null)
{
answ = recUpd.Uploaded;
}
}
}
}
catch (Exception exc)
{
Log.Error($"Eccezione in fase gestione REST services SendZipFile{Environment.NewLine}{exc}");
}
return answ;
}
#endregion Public Methods
#region Protected Methods
@@ -179,14 +231,24 @@ namespace MP.Land.Data
/// <summary>
/// URL dell'API x chiamate gestione licenze
/// </summary>
#if DEBUG
private static string apiUrl = "https://localhost:5003/";
#else
private static string apiUrl = "https://liman.egalware.com/ELM.API/";
#endif
/// <summary>
/// Classe logger
/// </summary>
private static Logger Log = LogManager.GetCurrentClassLogger();
/// <summary>
/// Conf client RestSharp standard:
/// - base URI al sito
/// - timeout 1 min
/// </summary>
private static RestClientOptions restOptStd = new RestClientOptions { Timeout = TimeSpan.FromSeconds(60), BaseUrl = new Uri(apiUrl) };
#endregion Private Fields
#region Private Properties
private static ILogger<LicenseService> _logger { get; set; } = null!;
#endregion Private Properties
}
}
+1 -1
View File
@@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>MP.Land</RootNamespace>
<Version>6.16.2410.1719</Version>
<Version>6.16.2410.1815</Version>
</PropertyGroup>
<ItemGroup>
-4
View File
@@ -1,8 +1,4 @@
@page "/About"
@using MP.Land.Data
@inject MessageService AppMService
@inject LicenseService LicServ
<div class="row mx-2">
<div class="col-12 col-xl-10 offset-xl-1">
+10
View File
@@ -1,11 +1,21 @@
using Microsoft.AspNetCore.Components;
using MP.Land.Data;
using NLog;
using System;
using System.Net.NetworkInformation;
using System.Threading.Tasks;
namespace MP.Land.Pages
{
public partial class About
{
[Inject]
protected MessageService AppMService { get; set; } = null!;
[Inject]
protected LicenseService LicServ{get;set;}=null!;
#region Protected Methods
protected override async Task OnInitializedAsync()
+91 -116
View File
@@ -7,6 +7,7 @@ using MP.Land.Data;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
@@ -18,6 +19,32 @@ namespace MP.Land.Pages
{
#region Public Methods
/// <summary>
/// Procedura di aggiunta folder a ZipFile, ricorsiva
/// </summary>
/// <param name="f"></param>
/// <param name="root"></param>
/// <param name="folder"></param>
public void AddFolderToZip(ZipFile f, string root, string folder)
{
string relative = folder.Substring(root.Length);
if (relative.Length > 0)
{
f.AddDirectory(relative);
}
foreach (string file in Directory.GetFiles(folder))
{
relative = file.Substring(root.Length);
f.Add(file, relative);
}
foreach (string subFolder in Directory.GetDirectories(folder))
{
this.AddFolderToZip(f, root, subFolder);
}
}
public void Dispose()
{
ListRecords = null;
@@ -59,6 +86,9 @@ namespace MP.Land.Pages
protected bool showProgress { get; set; } = false;
protected bool showUpdate { get; set; } = false;
[Inject]
protected SyncService SyncServ { get; set; } = null!;
#endregion Protected Properties
#region Protected Methods
@@ -174,8 +204,8 @@ namespace MP.Land.Pages
authList.Add(item);
}
}
// numero app + IOB + ZIP
numTot = authList.Count + 2;
// numero app + IOB + DB + ZIP
numTot = authList.Count + 3;
// recupero conf files
foreach (var item in authList)
@@ -187,9 +217,11 @@ namespace MP.Land.Pages
}
// recupero i file IOB
RecuperaIobConf();
// salvo Tab Db come json
SaveDbConfAsJson();
// ora creo il file zip
await PrepareZip();
PrepareZip();
// effettuo upload
await UploadZip();
@@ -212,12 +244,18 @@ namespace MP.Land.Pages
#endregion Private Fields
#region Private Properties
#if DEBUG
private DirectoryInfo AppDir => new DirectoryInfo(Path.Combine("\\\\iis01.egalware.com", "c$\\inetpub\\wwwroot\\MP\\LAND"));
#else
private DirectoryInfo AppDir => new DirectoryInfo(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location));
#endif
/// <summary>
/// Nome del file ZIP da gestire
/// </summary>
private string zFileName => Path.Combine(AppDir.FullName, "temp", "zip", "MAPO.zip");
#endregion Private Properties
#region Private Methods
@@ -225,126 +263,22 @@ namespace MP.Land.Pages
/// <summary>
/// Preparazione ZIP con password = AuthKey
/// </summary>
private async Task PrepareZip()
private void PrepareZip()
{
string srcPath = Path.Combine(AppDir.FullName, "temp", "orig");
string destPath = Path.Combine(AppDir.FullName, "temp", "zip");
if(!Directory.Exists(destPath))
if (!Directory.Exists(destPath))
{
Directory.CreateDirectory(destPath);
}
string zFile = Path.Combine(AppDir.FullName, "temp", "zip", "MAPO.zip");
#if false
ZipDirectory(folderName, zFile, 9);
await Task.Delay(1);
#endif
using (ZipFile zipFile = ZipFile.Create(zFile))
using (ZipFile zipFile = ZipFile.Create(zFileName))
{
zipFile.Password = LicServ.MasterKey;
zipFile.BeginUpdate();
addFolderToZip(zipFile, srcPath, srcPath);
AddFolderToZip(zipFile, srcPath, srcPath);
zipFile.CommitUpdate();
zipFile.Close();
}
}
public void addFolderToZip(ZipFile f, string root, string folder)
{
string relative = folder.Substring(root.Length);
if (relative.Length > 0)
{
f.AddDirectory(relative);
}
foreach (string file in Directory.GetFiles(folder))
{
relative = file.Substring(root.Length);
f.Add(file, relative);
}
foreach (string subFolder in Directory.GetDirectories(folder))
{
this.addFolderToZip(f, root, subFolder);
}
}
/// <summary>
/// Method that compress all the files inside a folder (non-recursive) into a zip file.
/// </summary>
/// <param name="DirectoryPath"></param>
/// <param name="OutputFilePath"></param>
/// <param name="CompressionLevel"></param>
private void ZipDirectory(string DirectoryPath, string OutputFilePath, int CompressionLevel = 9)
{
try
{
// Depending on the directory this could be very large and would require more attention
// in a commercial package.
string[] filenames = Directory.GetFiles(DirectoryPath);
// 'using' statements guarantee the stream is closed properly which is a big source
// of problems otherwise. Its exception safe as well which is great.
using (ZipOutputStream OutputStream = new ZipOutputStream(File.Create(OutputFilePath)))
{
// Define the compression level
// 0 - store only to 9 - means best compression
OutputStream.SetLevel(CompressionLevel);
byte[] buffer = new byte[4096];
foreach (string file in filenames)
{
// Using GetFileName makes the result compatible with XP
// as the resulting path is not absolute.
ZipEntry entry = new ZipEntry(Path.GetFileName(file));
// Setup the entry data as required.
// Crc and size are handled by the library for seakable streams
// so no need to do them here.
// Could also use the last write time or similar for the file.
entry.DateTime = DateTime.Now;
OutputStream.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(file))
{
// Using a fixed size buffer here makes no noticeable difference for output
// but keeps a lid on memory usage.
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
OutputStream.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
}
// Finish/Close arent needed strictly as the using statement does this automatically
// Finish is important to ensure trailing information for a Zip file is appended. Without this
// the created file would be invalid.
OutputStream.Finish();
// Close is important to wrap things up and unlock the file.
OutputStream.Close();
Console.WriteLine("Files successfully compressed");
}
}
catch (Exception ex)
{
// No need to rethrow the exception as for our purposes its handled.
Console.WriteLine("Exception during processing {0}", ex);
}
}
/// <summary>
@@ -423,6 +357,46 @@ namespace MP.Land.Pages
}
}
/// <summary>
/// Salvataggio tabelle di configurazione specifica come tracciati json
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private void SaveDbConfAsJson()
{
long size = 0;
string dstDir = Path.Combine(AppDir.FullName, "temp", "orig", "DB");
if (!Directory.Exists(dstDir))
{
Directory.CreateDirectory(dstDir);
}
// va fatta 1:1 per ogni tabella
#if false
string srcIobDir = Path.Combine(AppDir.Parent.FullName, "IO", "fileUpload");
// recupero elenco files tipo appsettings*.json
if (Directory.Exists(srcIobDir))
{
var dirInfo = new DirectoryInfo(srcIobDir);
// recupero files CORE
List<FileInfo> fileList = dirInfo.GetFiles().ToList();
// procedo!
foreach (var file in fileList)
{
string fileDestPath = Path.Combine(dstDir, file.Name);
file.CopyTo(fileDestPath, true);
size += file.Length;
}
}
#endif
numDone++;
percLoading = 100 * numDone / numTot;
TotalMb += size;
outMessages = $"Configurazioni preparate: {numDone}/{numTot} | {CalcSize(TotalMb)}";
}
/// <summary>
/// Effettua download di una singola app
/// </summary>
@@ -449,13 +423,14 @@ namespace MP.Land.Pages
/// </summary>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
private async Task UploadZip()
private async Task<bool> UploadZip()
{
await Task.Delay(200);
// chiamo SendZipFile di SyncService...
FileInfo zFileInfo = new FileInfo(zFileName);
var fatto = await SyncServ.SendZipFile(LicServ.Applicazione, LicServ.Installazione, true, false, zFileInfo);
return fatto;
}
#endregion Private Methods
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
<body>
<i>Modulo Tablet MAPO - DotNet6</i>
<h4>Versione: 6.16.2410.1719</h4>
<h4>Versione: 6.16.2410.1815</h4>
<br />
Note di rilascio:
<ul>
+1 -1
View File
@@ -1 +1 @@
6.16.2410.1719
6.16.2410.1815
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<item>
<version>6.16.2410.1719</version>
<version>6.16.2410.1815</version>
<url>https://nexus.steamware.net/repository/SWS/MP-LAND/stable/LAST/MP.Land.zip</url>
<changelog>https://nexus.steamware.net/repository/SWS/MP-LAND/stable/LAST/ChangeLog.html</changelog>
<mandatory>false</mandatory>