Exemplo n.º 1
0
        private void uploadFileTest(FTPProtocol protocol, int numFiles)
        {
            bool   bError;
            string errorMsg = "",
                   source   = "",
                   target,
                   remoteFolder = GetAppSetting("TargetFolder", "", protocol);

            // Create file(s) to upload
            List <string> sourceFiles = new List <string>();

            for (int i = 0; i < numFiles; i++)
            {
                source = Path.GetTempPath() + "\\" + i.ToString() + ".test";
                db.LogTestFile(source);     // So the cleanup delete it
                sourceFiles.Add(source);

                using (StreamWriter sw = new StreamWriter(source)) {
                    sw.WriteLine(LorumIpsum());
                }
            }

            // Upload the file(s)
            FTPService.FTPService ftpService = FTPService(protocol);
            if (numFiles > 1)
            {
                source = source.FolderName() + "\\*.test";
                target = remoteFolder;
            }
            else
            {
                target = remoteFolder + "/" + source.FileName();
            }
            bError = ftpService.UploadFile(source, target, ref errorMsg);
            Assert.IsTrue(!bError, errorMsg);

            // Delete the original files so that when we download again,
            // we know we have recreated the files
            foreach (var fileName in sourceFiles)
            {
                File.Delete(fileName);
                Assert.IsTrue(!File.Exists(fileName), $"Error: Failed to delete {fileName}");
            }

            // Now try to download the files
            if (numFiles > 1)
            {
                source = remoteFolder;
                target = Path.GetTempFileName().FolderName();
            }
            else
            {
                source = target;
                target = Path.GetTempFileName().FolderName() + "\\" + source.FileName();
                db.LogTestFile(target);     // So the cleanup deletes it
            }
            List <string> downloadedFiles = new List <string>();

            bError = ftpService.DownloadFile(source, target, downloadedFiles, ref errorMsg);
            LogTestFile(downloadedFiles);
            Assert.IsTrue(!bError, errorMsg);

            // Cleanup the host
            foreach (string fileName in sourceFiles)
            {
                bError = ftpService.DeleteFile(remoteFolder + "/" + fileName.FileName(), ref errorMsg);
                Assert.IsTrue(!bError, errorMsg);
            }

            // Check that the file exists
            foreach (string fileName in sourceFiles)
            {
                Assert.IsTrue(File.Exists(fileName), $"Error: Failed to find file {fileName}");
            }
        }
Exemplo n.º 2
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)");
        }
Exemplo n.º 3
0
        private void moveFileTest(FTPProtocol protocol)
        {
            bool   bError;
            string errorMsg   = "",
                   sourceFile = "",
                   targetFile,
                   remoteFolder = GetAppSetting("TargetFolder", "", protocol);

            // Create file(s) to upload
            sourceFile = Path.GetTempPath() + "\\MoveFile.test";
            db.LogTestFile(sourceFile);     // So the cleanup delete it

            using (StreamWriter sw = new StreamWriter(sourceFile)) {
                sw.WriteLine(LorumIpsum());
            }

            // Upload the file
            FTPService.FTPService ftpService = FTPService(protocol);
            targetFile = remoteFolder + "/" + sourceFile.FileName();

            bError = ftpService.UploadFile(sourceFile, targetFile, ref errorMsg);
            Assert.IsTrue(!bError, errorMsg);
            File.Delete(sourceFile);

            // Now download it to check that it got uploaded
            string temp = targetFile;

            targetFile = sourceFile;
            sourceFile = temp;

            List <string> downloadedFiles = new List <string>();

            bError = ftpService.DownloadFile(sourceFile, targetFile, downloadedFiles, ref errorMsg);
            LogTestFile(downloadedFiles);
            Assert.IsTrue(!bError, errorMsg);
            Assert.IsTrue(downloadedFiles.Count == 1, $"Error: {downloadedFiles.Count} file(s) were downloaded when 1 was expected");

            // Check that the file exists
            Assert.IsTrue(File.Exists(targetFile), $"Error: Failed to find file {targetFile}");

            // Now move it on the host
            targetFile = GetAppSetting("ArchiveFolder", "", protocol) + "/" + sourceFile.FileName();
            bError     = ftpService.MoveFile(sourceFile, targetFile, ref errorMsg, true);
            Assert.IsTrue(!bError, errorMsg);

            // Try to download it from original location (should fail because it has been moved)
            string tempFile = Path.GetTempFileName();

            db.LogTestFile(tempFile);     // So the cleanup delete it

            bError = ftpService.DownloadFile(sourceFile, tempFile, downloadedFiles, ref errorMsg);
            Assert.IsTrue(bError, "Error: Download should have failed because the source file has been moved. This error means that the move failed");

            // Now try to download it from the archive location
            bError = ftpService.DownloadFile(targetFile, tempFile, downloadedFiles, ref errorMsg);
            Assert.IsTrue(!bError, errorMsg);

            // Now delete it in the archive
            bError = ftpService.DeleteFile(targetFile, ref errorMsg);
            Assert.IsTrue(!bError, errorMsg);

            // Finally, try to download the archived file again (should fail)
            bError = ftpService.DownloadFile(targetFile, tempFile, downloadedFiles, ref errorMsg);
            Assert.IsTrue(bError, "Error: Download should have failed because the previous delete should have removed the file. This error means that the delete failed");
        }
        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);
        }