|
|
@@ -1,5 +1,4 @@
|
|
1
|
1
|
using BulkPrintingAPI.Configuration;
|
|
2
|
|
-using CoreFtp;
|
|
3
|
2
|
using MAX.Models;
|
|
4
|
3
|
using Microsoft.AspNetCore.Http;
|
|
5
|
4
|
using Microsoft.EntityFrameworkCore;
|
|
|
@@ -310,60 +309,69 @@ namespace BulkPrintingAPI.Controllers
|
|
310
|
309
|
}
|
|
311
|
310
|
}
|
|
312
|
311
|
|
|
313
|
|
- public static async Task DownloadVouchersAsync(FTPOptions ftpOptions, MAXContext context,
|
|
|
312
|
+ public static async Task DownloadVouchersAsync(SFTPOptions sftpOptions, MAXContext context,
|
|
314
|
313
|
ILogger logger, Batch batch)
|
|
315
|
314
|
{
|
|
316
|
315
|
var remoteFileName = string.Format("{0}_{1}.dat", batch.Account.Id, batch.Id);
|
|
317
|
316
|
using (var voucherStream = new MemoryStream())
|
|
318
|
317
|
{
|
|
319
|
|
- using (var ftp = new FtpClient(new FtpClientConfiguration()
|
|
|
318
|
+ var connectionInfo = new Renci.SshNet.ConnectionInfo(
|
|
|
319
|
+ sftpOptions.Host,
|
|
|
320
|
+ sftpOptions.Port,
|
|
|
321
|
+ sftpOptions.Username,
|
|
|
322
|
+ new Renci.SshNet.PasswordAuthenticationMethod(sftpOptions.Username, sftpOptions.Password)
|
|
|
323
|
+ );
|
|
|
324
|
+ connectionInfo.Timeout = TimeSpan.FromSeconds(sftpOptions.ConnectTimeout);
|
|
|
325
|
+
|
|
|
326
|
+ await Task.Run(() =>
|
|
320
|
327
|
{
|
|
321
|
|
- Host = ftpOptions.Host,
|
|
322
|
|
- Username = ftpOptions.Username,
|
|
323
|
|
- Password = ftpOptions.Password
|
|
324
|
|
- }))
|
|
325
|
|
- {
|
|
326
|
|
- await ftp.LoginAsync().ConfigureAwait(false);
|
|
327
|
|
- using (var downloadStream = await ftp.OpenFileReadStreamAsync(remoteFileName)
|
|
328
|
|
- .ConfigureAwait(false))
|
|
|
328
|
+ using (var sshClient = new Renci.SshNet.SftpClient(connectionInfo))
|
|
329
|
329
|
{
|
|
330
|
|
- await downloadStream.CopyToAsync(voucherStream).ConfigureAwait(false);
|
|
|
330
|
+ sshClient.Connect();
|
|
|
331
|
+ sshClient.DownloadFile(remoteFileName, voucherStream);
|
|
331
|
332
|
}
|
|
|
333
|
+ });
|
|
332
|
334
|
|
|
333
|
|
- voucherStream.Position = 0;
|
|
334
|
|
- using (var streamReader = new StreamReader(voucherStream))
|
|
|
335
|
+ voucherStream.Position = 0;
|
|
|
336
|
+ using (var streamReader = new StreamReader(voucherStream))
|
|
|
337
|
+ {
|
|
|
338
|
+ while (streamReader.Peek() >= 0)
|
|
335
|
339
|
{
|
|
336
|
|
- while (streamReader.Peek() >= 0)
|
|
|
340
|
+ var line = streamReader.ReadLine();
|
|
|
341
|
+ var parts = line.Split('|');
|
|
|
342
|
+
|
|
|
343
|
+ VoucherSanityCheck(batch, decimal.Parse(parts[4]), int.Parse(parts[5]),
|
|
|
344
|
+ parts[7]);
|
|
|
345
|
+
|
|
|
346
|
+ context.Add(new Voucher()
|
|
337
|
347
|
{
|
|
338
|
|
- var line = streamReader.ReadLine();
|
|
339
|
|
- var parts = line.Split('|');
|
|
340
|
|
-
|
|
341
|
|
- VoucherSanityCheck(batch, decimal.Parse(parts[4]), int.Parse(parts[5]),
|
|
342
|
|
- parts[7]);
|
|
343
|
|
-
|
|
344
|
|
- context.Add(new Voucher()
|
|
345
|
|
- {
|
|
346
|
|
- Id = int.Parse(parts[0]),
|
|
347
|
|
- ExpiryDate = DateTime.Parse(parts[1]),
|
|
348
|
|
- Serial = parts[2],
|
|
349
|
|
- EncryptedPIN = parts[3],
|
|
350
|
|
- SequenceNumber = int.Parse(parts[6]),
|
|
351
|
|
- Batch = batch
|
|
352
|
|
- });
|
|
353
|
|
- }
|
|
|
348
|
+ Id = int.Parse(parts[0]),
|
|
|
349
|
+ ExpiryDate = DateTime.Parse(parts[1]),
|
|
|
350
|
+ Serial = parts[2],
|
|
|
351
|
+ EncryptedPIN = parts[3],
|
|
|
352
|
+ SequenceNumber = int.Parse(parts[6]),
|
|
|
353
|
+ Batch = batch
|
|
|
354
|
+ });
|
|
354
|
355
|
}
|
|
|
356
|
+ }
|
|
355
|
357
|
|
|
356
|
|
- batch.ReadyForDownload = true;
|
|
357
|
|
- await context.SaveChangesAsync().ConfigureAwait(false);
|
|
|
358
|
+ batch.ReadyForDownload = true;
|
|
|
359
|
+ await context.SaveChangesAsync().ConfigureAwait(false);
|
|
358
|
360
|
|
|
359
|
|
- try
|
|
360
|
|
- {
|
|
361
|
|
- await ftp.DeleteFileAsync(remoteFileName).ConfigureAwait(false);
|
|
362
|
|
- }
|
|
363
|
|
- catch (Exception e)
|
|
|
361
|
+ try
|
|
|
362
|
+ {
|
|
|
363
|
+ await Task.Run(() =>
|
|
364
|
364
|
{
|
|
365
|
|
- logger.LogWarning("Failed to delete file on FTP server: {0}: {1}", remoteFileName, e.Message);
|
|
366
|
|
- }
|
|
|
365
|
+ using (var sshClient = new Renci.SshNet.SftpClient(connectionInfo))
|
|
|
366
|
+ {
|
|
|
367
|
+ sshClient.Connect();
|
|
|
368
|
+ sshClient.DeleteFile(remoteFileName);
|
|
|
369
|
+ }
|
|
|
370
|
+ });
|
|
|
371
|
+ }
|
|
|
372
|
+ catch (Exception e)
|
|
|
373
|
+ {
|
|
|
374
|
+ logger.LogWarning("Failed to delete file on FTP server: {0}: {1}", remoteFileName, e.Message);
|
|
367
|
375
|
}
|
|
368
|
376
|
}
|
|
369
|
377
|
}
|