1
0
Эх сурвалжийг харах

Factoring out GetOnlyFileOperation

Source commit: 9ba325c75ef64d4dbcf43b2bdd89e54ea86cbc9c
Martin Prikryl 5 жил өмнө
parent
commit
e811586564
1 өөрчлөгдсөн 18 нэмэгдсэн , 20 устгасан
  1. 18 20
      dotnet/Session.cs

+ 18 - 20
dotnet/Session.cs

@@ -10,6 +10,7 @@ using Microsoft.Win32;
 using System.Diagnostics;
 using System.Security;
 using System.Text.RegularExpressions;
+using System.Linq;
 
 namespace WinSCP
 {
@@ -804,16 +805,7 @@ namespace WinSCP
                 TransferOperationResult operationResult =
                     PutFilesToDirectory(localDirectory, remoteDirectory, filemask, remove, options);
                 operationResult.Check();
-                // Should not happen
-                if (operationResult.Transfers.Count == 0)
-                {
-                    throw Logger.WriteException(new FileNotFoundException("File not found"));
-                }
-                if (operationResult.Transfers.Count > 1)
-                {
-                    throw Logger.WriteException(new InvalidOperationException("More then one file has been unexpectedly found"));
-                }
-                return operationResult.Transfers[0];
+                return GetOnlyFileOperation(operationResult.Transfers);
             }
         }
 
@@ -940,17 +932,23 @@ namespace WinSCP
                 TransferOperationResult operationResult =
                     DoGetFilesToDirectory(remoteDirectory, localDirectory, filemask, remove, options, "-onlyfile");
                 operationResult.Check();
-                // Should happen only when the filename is mask-like, otherwise "get" throws straight away
-                if (operationResult.Transfers.Count == 0)
-                {
-                    throw Logger.WriteException(new FileNotFoundException("File not found"));
-                }
-                if (operationResult.Transfers.Count > 1)
-                {
-                    throw Logger.WriteException(new InvalidOperationException("More then one file has been unexpectedly found"));
-                }
-                return operationResult.Transfers[0];
+                return GetOnlyFileOperation(operationResult.Transfers);
+            }
+        }
+
+        private T GetOnlyFileOperation<T>(ICollection<T> operations)
+        {
+            // For "get", this should happen only when the filename is mask-like, otherwise "get" throws straight away.
+            // For "put", this should not happen.
+            if (operations.Count == 0)
+            {
+                throw Logger.WriteException(new FileNotFoundException("File not found"));
+            }
+            if (operations.Count > 1)
+            {
+                throw Logger.WriteException(new InvalidOperationException("More then one file has been unexpectedly found"));
             }
+            return operations.First();
         }
 
         public RemovalOperationResult RemoveFiles(string path)