Exemple #1
0
        void getFTPFileListTest(FTPProtocol protocol)
        {
            int           numFiles   = 10;
            string        errorMsg   = "";
            List <string> sourceList = new List <string>();
            List <string> foundList  = new List <string>();

            FTPService.FTPService ftpService = FTPService(protocol);

            for (int i = 0; i < numFiles; i++)
            {
                var tempFile = GetTempFile(".ftptest2");
                using (var sw = new StreamWriter(tempFile)) {
                    sw.WriteLine(LorumIpsum());
                }
                sourceList.Add(tempFile);
            }
            sourceList = sourceList.OrderBy(sl => sl.Substring(0))
                         .ToList();

            // Upload the test files - this uses GetLocalFileList()
            string remoteFolder = GetAppSetting("TargetFolder", "", protocol);
            bool   bError       = ftpService.UploadFile(sourceList[0].FolderName() + "\\*.ftptest2",
                                                        remoteFolder,
                                                        ref errorMsg);

            if (bError)
            {
                foreach (string fileName in sourceList)
                {
                    ftpService.DeleteFile(remoteFolder + "/" + fileName.FileName(), ref errorMsg);
                }
            }
            Assert.IsTrue(!bError, errorMsg);

            // Now get the FTP file list
            bError = ftpService.GetFTPFileList(remoteFolder, ref foundList, ref errorMsg);
            if (bError)
            {
                foreach (string fileName in sourceList)
                {
                    ftpService.DeleteFile(remoteFolder + "/" + fileName.FileName(), ref errorMsg);
                }
            }
            Assert.IsTrue(!bError, errorMsg);
            Assert.IsTrue(foundList.Count() == numFiles, $"Error: (Protocol: {protocol}) {foundList.Count()} files were returned when {numFiles} were expected");

            foundList = foundList.OrderBy(sl => sl.Substring(0))
                        .ToList();
            string expected = "",
                   actual   = "";

            for (int i = 0; i < numFiles; i++)
            {
                expected = sourceList[i].FileName();
                actual   = foundList[i].FileName();

                if (expected != actual)
                {
                    i = numFiles;
                }
            }

            // Cleanup before the assert so we don't leave files on the server
            foreach (string fileName in sourceList)
            {
                try {
                    File.Delete(fileName);
                } catch { }
                ftpService.DeleteFile(remoteFolder + "/" + fileName.FileName(), ref errorMsg);
            }

            Assert.IsTrue(expected == actual, $"Error: '{actual}' was returned when '{expected}' was expected");
        }
        public override int DoProcessing(string[] args)
        {
            // This task retrieves unpack slips from a warehouse.
            // It uses the transfers configured in the task properties.
            // TaskProcessor.exe UNPACKRECEIVER datatransferprofilename
            if (args.Length != 2)
            {
                WriteTaskLog($"Error: Incorrect parameters!\r\nUsage: TaskProcessor.exe UNPACKRECEIVER datatransferprofilename");
            }
            else
            {
                string errorMsg = "";

                user = GetTaskUser();
                if (user != null)
                {
                    DataTransferService.DataTransferService dts = new DataTransferService.DataTransferService(_db);

                    string taskName = args[1];
                    var    config   = dts.FindDataTransferConfigurationModel(taskName);
                    if (config == null)
                    {
                        WriteTaskLog($"Warning: Task {taskName} cound not be found!", LogSeverity.Warning);
                    }
                    else
                    {
                        // Retrieve files from the warehouse
                        WriteTaskLog("Downloading files from: " + config.TransferName);
                        var ftpService = new FTPService.FTPService(config.FTPHost, config.UserId, config.Password, config.Protocol);

                        var fileList = new List <string>();
                        if (ftpService.GetFTPFileList(config.SourceFolder, ref fileList, ref errorMsg))
                        {
                            WriteTaskLog(errorMsg, LogSeverity.Severe);
                        }
                        else
                        {
                            // Download files one at a time and as each is successfully downloaded,
                            // archive of delete it on the FTP host.
                            // This ensures that if we have a comms failure, the local drive and FTP host are in
                            // sync and we prevent repeated loads of the same file.
                            FileManagerService.FileManagerService.CreateFolder(config.TargetFolder.TrimEnd('\\'));

                            foreach (var fileName in fileList)
                            {
                                var downloadedFile = new List <string>();
                                var result         = ftpService.DownloadFile(fileName,
                                                                             config.TargetFolder.TrimEnd('\\') + "\\" + fileName.FileName(),
                                                                             downloadedFile,
                                                                             ref errorMsg);
                                _db.LogTestFile(downloadedFile);
                                if (result)
                                {
                                    WriteTaskLog(errorMsg, LogSeverity.Severe);
                                }
                                else
                                {
                                    // Delete or archive the source files of those which were retrieved
                                    result = ftpService.DeleteFile(fileName, ref errorMsg);
                                    if (result)
                                    {
                                        WriteTaskLog(errorMsg, LogSeverity.Severe);
                                    }
                                }
                            }

                            // Process the files received
                            processFiles(config);
                        }
                    }
                }
            }

            return(0);
        }
        private void receiveFiles(FileTransferConfigurationModel config)
        {
            int numFiles   = 0,
                numSuccess = 0,
                numErrors  = 0;

            WriteTaskLog($"Receiving file(s) from '{config.SourceFolder}' to '{config.TargetFolder}'");

            // Start the FTP service
            var ftpService = new FTPService.FTPService(config.FTPHost, config.UserId, config.Password, config.Protocol);

            // Get a list of files on the remote folder to receive
            string        errorMsg = "";
            List <string> fileList = new List <string>();

            if (ftpService.GetFTPFileList(config.SourceFolder, ref fileList, ref errorMsg))
            {
                WriteTaskLog(errorMsg, LogSeverity.Severe);
            }
            else
            {
                if (fileList != null)
                {
                    numFiles = fileList.Count();

                    string targetFolder = config.TargetFolder;
                    FileManagerService.FileManagerService.CreateFolder(targetFolder);

                    foreach (var fileName in fileList)
                    {
                        // Receiving a file
                        string targetFile = targetFolder + "\\" + fileName.FileName();

                        WriteTaskLog($"Receiving '{fileName}' to '{targetFile}'");

                        if (ftpService.DownloadFile(fileName,
                                                    targetFile,
                                                    null,
                                                    ref errorMsg))
                        {
                            // Failed to perform transfer
                            WriteTaskLog(errorMsg, LogSeverity.Severe);
                            numErrors++;

                            // Upon error, delete or archive remote file
                            bool bError;
                            if (!string.IsNullOrEmpty(config.ErrorFolder))
                            {
                                // File is configured to be archived
                                bError = ftpService.MoveFile(fileName, config.ErrorFolder, ref errorMsg);
                            }
                            else
                            {
                                // No archive configuration, so just delete the file
                                bError = ftpService.DeleteFile(fileName, ref errorMsg);
                            }
                            if (bError)
                            {
                                WriteTaskLog(errorMsg, LogSeverity.Severe);
                            }

                            // Delete the local file so we don't double-up
                            FileManagerService.FileManagerService.DeleteFile(targetFile);
                        }
                        else
                        {
                            // Transfer successful
                            WriteTaskLog(errorMsg, LogSeverity.Normal);
                            numSuccess++;

                            // Upon successful receipt, delete or archive remote file
                            bool bError;
                            if (!string.IsNullOrEmpty(config.ArchiveFolder))
                            {
                                // File is configured to be archived
                                bError = ftpService.MoveFile(fileName, config.ArchiveFolder, ref errorMsg);
                            }
                            else
                            {
                                // No archive configuration, so just delete the file
                                bError = ftpService.DeleteFile(fileName, ref errorMsg);
                            }
                            if (bError)
                            {
                                WriteTaskLog(errorMsg, LogSeverity.Severe);
                            }
                        }
                    }
                }
            }

            WriteTaskLog($"{numFiles} found, {numSuccess} received successfully, {numErrors} error(s)");
        }
Exemple #4
0
        public void DoProcessingTest()
        {
            // Get a test company
            var testUser    = GetTestUser();
            var testCompany = GetTestCompany(testUser);
            //CreateTestTransfers(testCompany, testUser);
            var testLocation = LookupService.FindLocationModel(testCompany.DefaultLocationID.Value);

            // Create some test transfers
            var testFolder = Path.GetTempPath() + RandomString();

            LogTestFolder(testFolder);
            Directory.CreateDirectory(testFolder);

            TestFileTransfer config = new TestFileTransfer(FileTransferDataType.WarehousePick, "Warehouse_Picks.xml", "{PICKNO}.{EXTN}");
            var sendConfig          = GetTestTransfer(testCompany, testUser, testLocation, FileTransferType.Send, FTPProtocol.FTP, testFolder, "/test/Evolution/", @"\Development\Evolution\DataTransfers\Test\Archive", @"\Development\Evolution\DataTransfers\Test\Error", config);
            var receiveConfig       = GetTestTransfer(testCompany, testUser, testLocation, FileTransferType.Receive, FTPProtocol.FTP, "/test/Evolution/", testFolder, "", "", config);

            // Create some test files
            int           numFiles = 6;
            List <string> fileList = new List <string>();

            for (int i = 0; i < numFiles; i++)
            {
                var fileName = sendConfig.SourceFolder + "\\" + RandomString() + ".txt";
                File.WriteAllText(fileName, LorumIpsum());
                fileList.Add(fileName);
            }

            // Upload the files
            var task = new DataTransferTask(db);

            string[] args = { TaskName.DataTransferTask,
                              sendConfig.TransferName };
            task.Run(args);

            // Check that the local files have been deleted
            foreach (var fileName in fileList)
            {
                Assert.IsTrue(!File.Exists(fileName), $"Error: File {fileName} was found in {testFolder} when it was expected to be deleted after sending");
            }

            // Download the files
            args = new string[] { TaskName.DataTransferTask,
                                  receiveConfig.TransferName };
            task.Run(args);

            // Check that the files exist
            foreach (var fileName in fileList)
            {
                Assert.IsTrue(File.Exists(fileName), $"Error: File {fileName} was not found in {testFolder} when it was expected to be");
            }

            // Check that the files have been deleted on the FTP site
            FTPService.FTPService ftpService = FTPService(FTPProtocol.FTP);

            List <string> foundList = new List <string>();
            var           errorMsg  = "";

            bool bError = ftpService.GetFTPFileList(receiveConfig.SourceFolder, ref foundList, ref errorMsg);

            if (bError)
            {
                foreach (string fileName in foundList)
                {
                    LogTestFile(fileName);
                    ftpService.DeleteFile(receiveConfig.SourceFolder + "/" + fileName.FileName(), ref errorMsg);
                }
            }
            Assert.IsTrue(!bError, errorMsg);
            Assert.IsTrue(foundList.Count() == 0, $"Error: {foundList.Count()} files were found on the remote server when 0 were expected");
        }