- aggiunta recupero files conf
- aggiunta files IOB
- aggiunta zip (no pwd)
This commit is contained in:
Samuele Locatelli
2024-10-17 18:57:56 +02:00
parent d8c8d8dbb9
commit 7ce1a7b54e
8 changed files with 386 additions and 24 deletions
+53
View File
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MP.AppAuth
{
public class MeasureUtils
{
#region Public Fields
public static readonly string[] SizeSuffixes = { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
#endregion Public Fields
#region Public Methods
/// <summary>
/// Calcola dimensione file automaticamwente secondo dimensione
/// </summary>
/// <param name="value"></param>
/// <param name="decimalPlaces"></param>
/// <returns></returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public static string SizeSuffix(Int64 value, int decimalPlaces = 1)
{
if (decimalPlaces < 0) { throw new ArgumentOutOfRangeException("decimalPlaces"); }
if (value < 0) { return "-" + SizeSuffix(-value, decimalPlaces); }
if (value == 0) { return string.Format("{0:n" + decimalPlaces + "} bytes", 0); }
// mag is 0 for bytes, 1 for KB, 2, for MB, etc.
int mag = (int)Math.Log(value, 1024);
// 1L << (mag * 10) == 2 ^ (10 * mag) [i.e. the number of bytes in the unit
// corresponding to mag]
decimal adjustedSize = (decimal)value / (1L << (mag * 10));
// make adjustment when the value is large enough that it would round up to 1000 or more
if (Math.Round(adjustedSize, decimalPlaces) >= 1000)
{
mag += 1;
adjustedSize /= 1024;
}
return string.Format("{0:n" + decimalPlaces + "} {1}",
adjustedSize,
SizeSuffixes[mag]);
}
#endregion Public Methods
}
}
+8 -1
View File
@@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>MP.Land</RootNamespace>
<Version>6.16.2409.1212</Version>
<Version>6.16.2410.1718</Version>
</PropertyGroup>
<ItemGroup>
@@ -55,6 +55,7 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NLog.Web.AspNetCore" Version="5.3.12" />
<PackageReference Include="RestSharp" Version="112.0.0" />
<PackageReference Include="SharpZipLib" Version="1.4.2" />
<PackageReference Include="StackExchange.Redis" Version="2.8.12" />
</ItemGroup>
@@ -63,9 +64,15 @@
</ItemGroup>
<ItemGroup>
<None Update="logs\.placeholder">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="post-build.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="temp\.placeholder">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
+7 -6
View File
@@ -14,22 +14,23 @@
<p>Scaricare uno o più pacchetti selezionando dall'elenco riportato.</p>
</div>
<div class="col-12 col-md-3 text-center">
<button class="btn btn-primary w-100" @onclick="() => UploadBackupConfig()"><i class="fas fa-upload"></i> Upload Config Backup <i class="fas fa-upload"></i></button>
<div class="h2">
ALL Packages <i class="fas fa-download"></i>
ALL Packages
</div>
<button class="btn btn-success w-100" @onclick="() => DownloadAll()">Download ALL Latest</button>
<button class="btn btn-success w-100" @onclick="() => DownloadAll()"> <i class="fas fa-download"></i> Download ALL Latest <i class="fas fa-download"></i></button>
</div>
</div>
</div>
@if (showUpdate)
{
<div class="alert alert-success alert-dismissible fade show">
<button type="button" class="close" data-dismiss="alert">&times;</button>
<div class="row">
<div class="col-4">
<button type="button" class="btn-close" @onclick="CloseProgUpdate"></button>
<div class="d-flex justify-content-between">
<div class="px-0">
@outMessages
</div>
<div class="col-8">
<div class="px-0" style="min-width: 20rem;">
@if (showProgress)
{
<div class="progress">
+314 -14
View File
@@ -1,11 +1,15 @@
using Microsoft.AspNetCore.Components;
using ICSharpCode.SharpZipLib.Zip;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using MP.AppAuth;
using MP.Land.Data;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
namespace MP.Land.Pages
@@ -30,7 +34,7 @@ namespace MP.Land.Pages
protected int totalCount = 0;
protected double TotalMb = 0;
protected long TotalMb = 0;
protected UpdateMan updateManAuth = new UpdateMan("SWDownloader", "viaD@nte16");
@@ -59,6 +63,21 @@ namespace MP.Land.Pages
#region Protected Methods
/// <summary>
/// Restituisce size calcolata
/// </summary>
/// <param name="origSize"></param>
/// <returns></returns>
protected string CalcSize(long origSize)
{
return MeasureUtils.SizeSuffix(origSize, 1);
}
protected void CloseProgUpdate()
{
showUpdate = false;
}
/// <summary>
/// Cicla su tutti i record ed effettua il download
/// </summary>
@@ -68,7 +87,7 @@ namespace MP.Land.Pages
showProgress = true;
showUpdate = true;
outMessages = $"Iniziato download per {numTot} packages";
StateHasChanged();
await InvokeAsync(StateHasChanged);
percLoading = 0;
TotalMb = 0;
numDone = 0;
@@ -90,20 +109,16 @@ namespace MP.Land.Pages
numTot = authList.Count;
foreach (var item in authList)
{
long size = 0;
size = await scaricaSingolo(item);
TotalMb += (double)size / (1024 * 1024);
outMessages = $"Scaricati {numDone}/{numTot} packages | {TotalMb:N2} Mb";
StateHasChanged();
await Task.Delay(1);
ScaricaSingolo(item);
await InvokeAsync(StateHasChanged);
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
outMessages = $"Effettuato download di {numDone} update | {TotalMb:N2} Mb in {ts.TotalSeconds:N2} s";
StateHasChanged();
outMessages = $"Effettuato download di {numDone} update | {CalcSize(TotalMb)} in {ts.TotalSeconds:N2} s";
await InvokeAsync(StateHasChanged);
await Task.Delay(1000);
showProgress = false;
StateHasChanged();
await InvokeAsync(StateHasChanged);
}
protected string localPath(string localRepo)
@@ -130,6 +145,64 @@ namespace MP.Land.Pages
await Task.Delay(1);
}
/// <summary>
/// Cicla su tutti i record applicativi, crea ZIP cifrato + upload su LiMan
/// </summary>
protected async Task UploadBackupConfig()
{
// init progress...
showProgress = true;
showUpdate = true;
outMessages = $"Inizio preparazione per upload configurazioni di {numTot} packages";
await InvokeAsync(StateHasChanged);
percLoading = 0;
TotalMb = 0;
numDone = 0;
int numIOB = 0;
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
// ciclo su tutti quelli con licenza valida...
List<AppAuth.Models.UpdMan> rawList = ListRecords
.Where(x => !string.IsNullOrEmpty(x.LicenseKey)).ToList();
List<AppAuth.Models.UpdMan> authList = new List<AppAuth.Models.UpdMan>();
// ciclo SOLO tra quelli davvero autorizzati...
foreach (var item in rawList)
{
if (LicServ.checkLicenseActive(item.LicenseKey))
{
authList.Add(item);
}
}
// numero app + IOB + ZIP
numTot = authList.Count + 2;
// recupero conf files
foreach (var item in authList)
{
// cerco i file di conf e li copio
RecuperaConf(item);
await Task.Delay(10);
await InvokeAsync(StateHasChanged);
}
// recupero i file IOB
RecuperaIobConf();
// ora creo il file zip
await PrepareZip();
// effettuo upload
await UploadZip();
// concludo!
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
outMessages = $"Effettuato Upload backup configurazioni per {numDone} app + {numIOB} IOB | {CalcSize(TotalMb)} | {ts.TotalSeconds:N2} s";
await InvokeAsync(StateHasChanged);
await Task.Delay(1000);
showProgress = false;
await InvokeAsync(StateHasChanged);
}
#endregion Protected Methods
#region Private Fields
@@ -138,9 +211,223 @@ 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
#endregion Private Properties
#region Private Methods
private async Task<long> scaricaSingolo(AppAuth.Models.UpdMan item)
/// <summary>
/// Preparazione ZIP con password = AuthKey
/// </summary>
private async Task PrepareZip()
{
string srcPath = Path.Combine(AppDir.FullName, "temp", "orig");
string destPath = Path.Combine(AppDir.FullName, "temp", "zip");
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))
{
zipFile.BeginUpdate();
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>
/// Recupera fdile di conf da app indicata
/// </summary>
/// <param name="item"></param>
private void RecuperaConf(AppAuth.Models.UpdMan item)
{
long size = 0;
if (item.IsAuth)
{
string dstDir = Path.Combine(AppDir.FullName, "temp", "orig", item.PackName);
if (!Directory.Exists(dstDir))
{
Directory.CreateDirectory(dstDir);
}
string srcDir = Path.Combine(AppDir.Parent.FullName, item.PackName);
// recupero elenco files tipo appsettings*.json
if (Directory.Exists(srcDir))
{
var dirInfo = new DirectoryInfo(srcDir);
// recupero files CORE
List<FileInfo> fileList = dirInfo.GetFiles("appsettings*.json").ToList();
// recupero files Framework
List<FileInfo> fileListFram = dirInfo.GetFiles("web*.config").ToList();
// merge...
foreach (var fileFrm in fileListFram)
{
fileList.Add(fileFrm);
}
// procedo!
foreach (var file in fileList)
{
string fileDestPath = Path.Combine(dstDir, file.Name);
file.CopyTo(fileDestPath, true);
size += file.Length;
}
numDone++;
percLoading = 100 * numDone / numTot;
TotalMb += size;
outMessages = $"Configurazioni preparate: {numDone}/{numTot} | {CalcSize(TotalMb)}";
}
}
}
/// <summary>
/// recupera tutte le conf IOB trasferite all'MP-IO dai vari IOB-WIN
/// </summary>
private void RecuperaIobConf()
{
long size = 0;
string dstDir = Path.Combine(AppDir.FullName, "temp", "orig", "IOB");
if (!Directory.Exists(dstDir))
{
Directory.CreateDirectory(dstDir);
}
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;
}
numDone++;
percLoading = 100 * numDone / numTot;
TotalMb += size;
outMessages = $"Configurazioni preparate: {numDone}/{numTot} | {CalcSize(TotalMb)}";
}
}
/// <summary>
/// Effettua download di una singola app
/// </summary>
/// <param name="item"></param>
private void ScaricaSingolo(AppAuth.Models.UpdMan item)
{
long size = 0;
if (item.IsAuth)
@@ -153,9 +440,22 @@ namespace MP.Land.Pages
}
numDone++;
percLoading = 100 * numDone / numTot;
return await Task.FromResult(size);
TotalMb += size;
outMessages = $"Scaricati {numDone}/{numTot} packages | {CalcSize(TotalMb)}";
}
/// <summary>
/// Invio zip al server LiMan remoto
/// </summary>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
private async Task UploadZip()
{
await Task.Delay(200);
}
#endregion Private Methods
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
<body>
<i>Modulo Tablet MAPO - DotNet6</i>
<h4>Versione: 6.16.2409.1212</h4>
<h4>Versione: 6.16.2410.1718</h4>
<br />
Note di rilascio:
<ul>
+1 -1
View File
@@ -1 +1 @@
6.16.2409.1212
6.16.2410.1718
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<item>
<version>6.16.2409.1212</version>
<version>6.16.2410.1718</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>
+1
View File
@@ -0,0 +1 @@