2025-04-15 12:10:19 +02:00

192 lines
6.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Collections.Specialized;
using System.Text;
using System.Diagnostics;
using System.Data;
using System.IO;
using System.Linq;
namespace ArchiviazionePDF
{
public class RestorePdf
{
public LogFile logFile;
DBA dba = new DBA();
public void Start() {
try {
logFile = new LogFile();
logFile.namebatch = "RestorePdf.RestoreArchive";
RestoreArchive();
} catch (Exception ex) {
logFile.scrivoErrore(ex.Message);
}
logFile.WriteEnd();
try {
logFile = new LogFile();
logFile.namebatch = "RestorePdf.Bprestore";
Bprestore();
} catch (Exception ex) {
logFile.scrivoErrore(ex.Message);
}
logFile.WriteEnd();
try {
logFile = new LogFile();
logFile.namebatch = "RestorePdf.RestoreRetrive";
RestoreRetrive();
} catch (Exception ex) {
logFile.scrivoErrore(ex.Message);
}
logFile.WriteEnd();
}
public void RestoreArchive()
{
DataTable dt = dba.DaRestorare();
logFile.trovati = dt.Rows.Count;
foreach (DataRow dr in dt.Rows)
{
long idPdf = long.Parse(dr["idPdf"].ToString());
logFile.idPdfCorrente = idPdf;
FileInfo f = new FileInfo(Config.pathArchive + @"\P" + idPdf + ".pdf");
if (f.Exists)
{
logFile.Elaborati++;
FileStream fs;
try { fs = f.OpenRead(); } catch (Exception ex) {
logFile.scrivoErrore( ex.Message);
continue;
}
try
{
byte[] pdfData = new byte[fs.Length];
fs.Read(pdfData, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
dba.RestorePdf(pdfData, idPdf);
logFile.eseguiti++;
}
catch (Exception ex)
{
logFile.scrivoErrore( ex.Message);
}
}
}
}
public void Bprestore()
{
try
{
DataTable dt = dba.DaRestorare();
logFile.trovati = dt.Rows.Count;
StreamWriter InputList = new StreamWriter("InputList", false);
foreach (DataRow dr in dt.Rows)
{
logFile.Elaborati++;
long idPdf = long.Parse(dr["idPdf"].ToString());
logFile.idPdfCorrente = idPdf;
InputList.WriteLine(Config.pathArchive + @"\P" + idPdf + ".pdf");
}
InputList.Close();
string cmd = Config.pathBprestore + @"\bprestore.exe";
string arg = "-A -R " + Config.renamelist + " -w -f InputList";
ProcessStartInfo startinfo = new ProcessStartInfo(cmd, arg);
Process.Start(startinfo);
foreach (string f in Directory.GetFiles(Config.pathRetrive)) {
bool trovato = false;
foreach (DataRow dr in dt.Rows){
if (long.Parse(dr["idPdf"].ToString()) == getIdPdf(f)) {
logFile.eseguiti++;
trovato = true;
break;
}
}
if (!trovato) {
logFile.scrivoWarning( "non restorato da bprestore.exe");
}
}
}catch(Exception ex){
logFile.scrivoErrore( ex.Message);
}
}
public void RestoreRetrive()
{
string[] files = Directory.GetFiles(Config.pathRetrive);
logFile.trovati = files.Length;
foreach (string f in files)
{
FileStream fs;
if (f.IndexOf(".pdf") > 0)
{
logFile.Elaborati++;
long idPdf = getIdPdf(f);
logFile.idPdfCorrente = idPdf;
try { fs = new FileStream(f, FileMode.OpenOrCreate, FileAccess.Read); } catch (Exception ex) {
logFile.scrivoErrore( ex.Message);
continue;
}
try
{
byte[] pdfData = new byte[fs.Length];
fs.Read(pdfData, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
dba.RestorePdf(pdfData, idPdf);
try { File.Delete(f); }
catch (Exception ex) { logFile.scrivoWarning( ex.Message); }
logFile.eseguiti++;
}
catch (Exception ex)
{
logFile.scrivoErrore(ex.Message);
}
}
}
//DeleteAllFiles();
}
public long getIdPdf(string name) {
string o = "";
for (int i = name.Length - 5; i != 0 && name[i] >= '0' && name[i] <= '9'; i--) o = name[i] + o;
return long.Parse(o);
}
//private static void DeleteAllFiles()
//{
// string[] files = Directory.GetFiles(Config.pathRetrive);
// foreach (string f in files)
// {
// if (f.IndexOf(".pdf") > 0)
// {
// File.Delete(f);
// }
// }
//}
}
}