137 lines
6.6 KiB
C#
137 lines
6.6 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.IO.Compression;
|
|
using System.Collections.Generic;
|
|
using Renci.SshNet;
|
|
using Renci.SshNet.Common;
|
|
using tsUtilidades;
|
|
using tsUtilidades.Extensiones;
|
|
using bdAsegasa.db;
|
|
using tsUtilidades.Enumeraciones;
|
|
|
|
namespace Servicio_Gestion_Asegasa.Procesos
|
|
{
|
|
public class ProcesosCaser
|
|
{
|
|
|
|
public static async Task GuardarEnBDFicherosDisponiblesSFTPAsync()
|
|
{
|
|
try
|
|
{
|
|
using (var bd = bdAsegasa.db.gestionasegasaEntities.NuevoContextoCN())
|
|
using (var bdtmp = bdAsegasa.db.gestionasegasaEntities.NuevoContextoCN())
|
|
{
|
|
var idCaser = bd.companias.First(x => x.Codigo == "0017").idCompania;
|
|
|
|
var keybAuth = new KeyboardInteractiveAuthenticationMethod("ca071106");
|
|
keybAuth.AuthenticationPrompt += HandleKeyEvent;
|
|
var conInfo = new ConnectionInfo("ftp.caser.es", 22, "ca071106", keybAuth);
|
|
|
|
using (var sftp = new SftpClient(conInfo))
|
|
{
|
|
sftp.Connect();
|
|
var lf = sftp.ListDirectory("ca071106").Where(x => x.IsDirectory == false);
|
|
|
|
foreach (var F in lf)
|
|
{
|
|
try
|
|
{
|
|
if (F.Name.StartsWith("EIAC_REC_") || F.Name.StartsWith("EIAC_POL_") || F.Name.StartsWith("EIAC_SIN_"))
|
|
{
|
|
string nomfic;
|
|
bool Eszip;
|
|
if (F.Name.EndsWith(".txt"))
|
|
{
|
|
nomfic = F.Name.Substring(0, F.Name.Length - 4);
|
|
Eszip = false;
|
|
}
|
|
else
|
|
{
|
|
nomfic = Path.ChangeExtension(F.Name, "xml");
|
|
Eszip = true;
|
|
}
|
|
using (var ms = new MemoryStream())
|
|
{
|
|
sftp.DownloadFile(F.FullName, ms);
|
|
ms.Position = 0;
|
|
|
|
byte[] b;
|
|
if (Eszip)
|
|
{
|
|
using (var zip = new ZipArchive(ms, ZipArchiveMode.Read))
|
|
{
|
|
var firstEntry = zip.Entries.First();
|
|
using (var msxml = new MemoryStream())
|
|
{
|
|
using (var entryStream = firstEntry.Open())
|
|
{
|
|
entryStream.CopyTo(msxml);
|
|
}
|
|
msxml.Position = 0;
|
|
b = msxml.ToArray();
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
b = ms.ToArray();
|
|
}
|
|
|
|
var sha1 = tsUtilidades.crypt.SHA1(b);
|
|
if (bdtmp.ficheroscompanias.Any(x => x.SHA1 == sha1 && x.idCompania == idCaser) == false)
|
|
{
|
|
var nf = new bdAsegasa.db.ficheroscompanias();
|
|
nf.FechaCreacion = DateTime.Now;
|
|
nf.idCompania = idCaser;
|
|
nf.Fichero = b;
|
|
nf.NombreFichero = nomfic;
|
|
nf.SHA1 = sha1;
|
|
|
|
switch (F.Name.Split('_')[1])
|
|
{
|
|
case "REC":
|
|
nf.Tipo = (int)bdAsegasa.db.ficheroscompanias.TipoFicheroCompania.RECIBOS_EIAC;
|
|
break;
|
|
case "POL":
|
|
nf.Tipo = (int)bdAsegasa.db.ficheroscompanias.TipoFicheroCompania.POLIZAS_EIAC;
|
|
break;
|
|
case "SIN":
|
|
nf.Tipo = (int)bdAsegasa.db.ficheroscompanias.TipoFicheroCompania.SINIESTROS_EIAC;
|
|
break;
|
|
}
|
|
bd.ficheroscompanias.Add(nf);
|
|
bd.SaveChanges();
|
|
}
|
|
}
|
|
}
|
|
sftp.RenameFile(F.FullName, F.FullName.Replace(F.Name, "PROCESADOS/" + F.Name));
|
|
}
|
|
catch (Exception EX)
|
|
{
|
|
throw new Exception(EX.Message + " Fichero: " + F.FullName, EX);
|
|
}
|
|
}
|
|
sftp.Disconnect();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception EX)
|
|
{
|
|
await tsUtilidades.TsNotificacionesClient.RegistrarAsync("En GuardarEnBDFicherosDisponibles CASER", EX.Message, tsUtilidades.TsNotificacionesClient.TipoNotificacionEnum.ERROR);
|
|
}
|
|
}
|
|
|
|
private static void HandleKeyEvent(object? sender, AuthenticationPromptEventArgs e)
|
|
{
|
|
foreach (var prompt in e.Prompts)
|
|
{
|
|
if (prompt.Request.IndexOf("Password:", StringComparison.InvariantCultureIgnoreCase) != -1)
|
|
{
|
|
prompt.Response = "As69gs73";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|