Просмотр исходного кода

Delete voucher files from the FTP server after a successful import.

Andrew Klopper лет назад: 8
Родитель
Сommit
847559b7a1
2 измененных файлов с 33 добавлено и 24 удалено
  1. 2 2
      BulkPrintingAPI/Controllers/BatchesController.cs
  2. 31 22
      BulkPrintingAPI/Controllers/Utils.cs

+ 2 - 2
BulkPrintingAPI/Controllers/BatchesController.cs

86
 
86
 
87
             if (!batch.ReadyForDownload)
87
             if (!batch.ReadyForDownload)
88
             {
88
             {
89
-                await Utils.DownloadVouchersAsync(_ftpOptions, _context, batch);
89
+                await Utils.DownloadVouchersAsync(_ftpOptions, _context, _logger, batch);
90
             }
90
             }
91
 
91
 
92
             return Ok(batch);
92
             return Ok(batch);
130
 
130
 
131
             try
131
             try
132
             {
132
             {
133
-                await Utils.DownloadVouchersAsync(_ftpOptions, _context, orderResponse.Batch);
133
+                await Utils.DownloadVouchersAsync(_ftpOptions, _context, _logger, orderResponse.Batch);
134
             }
134
             }
135
             catch (Exception e)
135
             catch (Exception e)
136
             {
136
             {

+ 31 - 22
BulkPrintingAPI/Controllers/Utils.cs

306
         }
306
         }
307
 
307
 
308
         public static async Task DownloadVouchersAsync(FTPOptions ftpOptions, MAXContext context,
308
         public static async Task DownloadVouchersAsync(FTPOptions ftpOptions, MAXContext context,
309
-            Batch batch)
309
+            ILogger logger, Batch batch)
310
         {
310
         {
311
             var remoteFileName = string.Format("{0}_{1}.dat", batch.Account.Id, batch.Id);
311
             var remoteFileName = string.Format("{0}_{1}.dat", batch.Account.Id, batch.Id);
312
             using (var voucherStream = new MemoryStream())
312
             using (var voucherStream = new MemoryStream())
324
                     {
324
                     {
325
                         await downloadStream.CopyToAsync(voucherStream).ConfigureAwait(false);
325
                         await downloadStream.CopyToAsync(voucherStream).ConfigureAwait(false);
326
                     }
326
                     }
327
-                }
328
 
327
 
329
-                voucherStream.Position = 0;
330
-                using (var streamReader = new StreamReader(voucherStream))
331
-                {
332
-                    while (streamReader.Peek() >= 0)
328
+                    voucherStream.Position = 0;
329
+                    using (var streamReader = new StreamReader(voucherStream))
333
                     {
330
                     {
334
-                        var line = streamReader.ReadLine();
335
-                        var parts = line.Split('|');
331
+                        while (streamReader.Peek() >= 0)
332
+                        {
333
+                            var line = streamReader.ReadLine();
334
+                            var parts = line.Split('|');
336
 
335
 
337
-                        VoucherSanityCheck(batch, decimal.Parse(parts[4]), int.Parse(parts[5]),
338
-                            parts[7]);
336
+                            VoucherSanityCheck(batch, decimal.Parse(parts[4]), int.Parse(parts[5]),
337
+                                parts[7]);
339
 
338
 
340
-                        context.Add(new Voucher()
341
-                        {
342
-                            Id = int.Parse(parts[0]),
343
-                            ExpiryDate = DateTime.Parse(parts[1]),
344
-                            Serial = parts[2],
345
-                            EncryptedPIN = parts[3],
346
-                            SequenceNumber = int.Parse(parts[6]),
347
-                            Batch = batch
348
-                        });
339
+                            context.Add(new Voucher()
340
+                            {
341
+                                Id = int.Parse(parts[0]),
342
+                                ExpiryDate = DateTime.Parse(parts[1]),
343
+                                Serial = parts[2],
344
+                                EncryptedPIN = parts[3],
345
+                                SequenceNumber = int.Parse(parts[6]),
346
+                                Batch = batch
347
+                            });
348
+                        }
349
+                    }
350
+
351
+                    batch.ReadyForDownload = true;
352
+                    await context.SaveChangesAsync().ConfigureAwait(false);
353
+
354
+                    try
355
+                    {
356
+                        await ftp.DeleteFileAsync(remoteFileName).ConfigureAwait(false);
357
+                    }
358
+                    catch (Exception e)
359
+                    {
360
+                        logger.LogWarning("Failed to delete file on FTP server: {0}: {1}", remoteFileName, e.Message);
349
                     }
361
                     }
350
                 }
362
                 }
351
             }
363
             }
352
-
353
-            batch.ReadyForDownload = true;
354
-            await context.SaveChangesAsync().ConfigureAwait(false);
355
         }
364
         }
356
     }
365
     }
357
 }
366
 }