private bool MoveFileInternal(FileTransferInfo f, string searchPath)
        {
            _logger.Info("File found: " + f.FileName);
            var           fileIx = System.Threading.Interlocked.Increment(ref _filesCount);
            IFileTransfer output = null, input = null;

            // try to execte trasnfer
            try
            {
                var destinationPath = _options.Get("outputPath", "");

                // open destination
                // use a lock to avoid hitting the server to open multiple connections at the same time
                lock (_fileTransferService)
                {
                    input  = _fileTransferService.Open(searchPath, _options);
                    output = _fileTransferService.Open(destinationPath, _options);
                    if (!output.IsOpened())
                    {
                        throw new Exception(String.Format("Invalid destinationPath, {0}: {1}", output.LastError ?? "", destinationPath));
                    }
                }

                // get input stream
                var inputStream = input.GetFileStream(f.FileName);

                // upload file
                var fileName = output.Details.GetDestinationPath(f.FileName);
                if (!output.SendFile(inputStream.FileStream, fileName, true))
                {
                    _logger.Error(output.LastError);

                    // move to error folder
                    MoveToErrorLocation(f.FileName, searchPath);
                    // continue to next file
                    return(true);
                }

                _logger.Debug("File moved: " + f.FileName);

                // emit file info
                lock (_context)
                {
                    _context.Emit(_layout.Create()
                                  .Set("fileName", fileName)
                                  .Set("fileNumber", _filesCount)
                                  .Set("filePath", destinationPath)
                                  .Set("sourcePath", searchPath)
                                  .Set("sourceFileName", f.FileName));
                }

                // If backup folder exists, move file
                MoveToBackupLocation(f.FileName, searchPath);

                // If DeleSource is set
                if (_deleteSourceFile)
                {
                    TryDeleteFile(f.FileName, searchPath);
                }
            }
            catch (Exception ex)
            {
                _context.Error = ex.Message;
                _logger.Error(ex);
                MoveToErrorLocation(f.FileName, searchPath);
                return(false);
            }
            finally
            {
                if (input != null)
                {
                    input.Dispose();
                }
                if (output != null)
                {
                    output.Dispose();
                }
            }
            return(true);
        }
Exemple #2
0
        public bool Execute(ISessionContext context)
        {
            var _logger  = context.GetLogger();
            var _options = context.Options;

            IFileTransfer input               = null;
            string        lastFile            = null;
            int           filesCount          = 0;
            int           maxFilesCount       = Int32.MaxValue;
            var           fileTransferService = context.GetContainer().GetInstanceOf <IFileService> ();

            try
            {
                var searchPath = _options.Get("inputPath", "");
                if (String.IsNullOrEmpty(searchPath))
                {
                    throw new ArgumentNullException("inputPath");
                }

                var deleteSourceFile = _options.Get <bool>("deleteSourceFile", false);

                // prepare paths
                input = fileTransferService.Open(searchPath, _options);
                if (!input.IsOpened())
                {
                    throw new Exception("Invalid searchPath: " + searchPath);
                }

                maxFilesCount = _options.Get("maxFileCount", maxFilesCount);
                if (maxFilesCount <= 0)
                {
                    maxFilesCount = Int32.MaxValue;
                }

                var defaultEncoding = Encoding.GetEncoding(_options.Get("encoding", "ISO-8859-1"));

                // open connection
                string line;
                Layout layout = new Layout();

                foreach (var f in input.GetFileStreams())
                {
                    lastFile = f.FileName;
                    _logger.Info("File found: " + lastFile);
                    filesCount++;

                    // read file
                    using (var reader = new StreamReader(f.FileStream, defaultEncoding, true))
                    {
                        int n = 1;
                        while ((line = reader.ReadLine()) != null)
                        {
                            context.Emit(layout.Create()
                                         .Set("fileName", f.FileName)
                                         .Set("fileNumber", filesCount)
                                         .Set("filePath", searchPath)
                                         .Set("lineNumber", n++)
                                         .Set("line", line));
                        }
                    }

                    // If backup folder exists, move file
                    if (!String.IsNullOrWhiteSpace(_options.Get("backupLocation", "")))
                    {
                        // TODO: implement move operation if location are the same!
                        using (var backupLocation = fileTransferService.Open(_options.Get("backupLocation", ""), _options))
                        {
                            var destName = backupLocation.Details.GetDestinationPath(f.FileName);
                            backupLocation.SendFile(input.GetFileStream(f.FileName).FileStream, destName, true);
                            _logger.Info("Backup file created: " + destName);
                        }
                    }

                    // If DeleSource is set
                    if (deleteSourceFile)
                    {
                        input.RemoveFile(f.FileName);
                        _logger.Info("File deleted: " + f.FileName);
                    }

                    // limit
                    if (filesCount >= maxFilesCount)
                    {
                        break;
                    }
                }

                if (filesCount > 0)
                {
                    _logger.Success("Done");
                    return(true);
                }
                else
                {
                    _logger.Debug("No Files Found on: " + searchPath);
                    return(true);
                }
            }
            catch (Exception ex)
            {
                context.Error = ex.Message;
                _logger.Error(ex);
                try
                {
                    if (lastFile != null && input != null && !String.IsNullOrEmpty(_options.Get("errorLocation", "")))
                    {
                        // move files
                        using (var parsedErrorLocation = fileTransferService.Open(_options.Get("errorLocation", ""), _options))
                        {
                            var destName = parsedErrorLocation.Details.GetDestinationPath(lastFile);
                            parsedErrorLocation.SendFile(input.GetFileStream(lastFile).FileStream, destName, true);
                        }
                    }
                }
                catch { }
                return(false);
            }
            finally
            {
                if (input != null)
                {
                    input.Dispose();
                }
            }
        }
        public bool Execute(ISessionContext context)
        {
            var _logger  = context.GetLogger();
            var _options = context.Options;


            try
            {
                var searchPath = _options.Get("searchPath", "");
                if (String.IsNullOrEmpty(searchPath))
                {
                    throw new ArgumentNullException("searchPath");
                }

                var destinationPath = _options.Get("destinationPath", "");
                if (String.IsNullOrEmpty(destinationPath))
                {
                    throw new ArgumentNullException("destinationPath");
                }

                var deleteSourceFile = _options.Get <bool> ("deleteSourceFile", false);

                maxFilesCount = _options.Get("maxFileCount", maxFilesCount);
                if (maxFilesCount <= 0)
                {
                    maxFilesCount = Int32.MaxValue;
                }

                var defaultEncoding = Encoding.GetEncoding(_options.Get("encoding", "ISO-8859-1"));

                // prepare paths
                var parsedInput = FileTransferService.Create(searchPath, _options);
                if (!parsedInput.HasConnectionString)
                {
                    throw new Exception("Invalid searchPath: " + searchPath);
                }
                var parsedOutput = FileTransferService.Create(destinationPath, _options);
                if (!parsedOutput.HasConnectionString)
                {
                    throw new Exception("Invalid destinationPath: " + destinationPath);
                }

                var parsedBackupLocation = FileTransferService.Create(_options.Get("backupLocation", ""), _options);
                parsedErrorLocation.Parse(_options.Get("errorLocation", ""), _options);

                // open connection
                Layout layout = new Layout();
                input  = parsedInput.OpenConnection();
                output = parsedOutput.OpenConnection();

                // try get files
                foreach (var f in input.GetFileStreams())
                {
                    lastFile = f.FileName;
                    _logger.Info("File found: " + lastFile);
                    filesCount++;

                    // upload file
                    var fileName = parsedOutput.GetDestinationPath(f.FileName);
                    output.SendFile(f.FileStream, fileName, true);

                    context.Emit(layout.Create()
                                 .Set("FileName", fileName)
                                 .Set("FileCount", filesCount)
                                 .Set("FilePath", destinationPath)
                                 .Set("SourcePath", searchPath)
                                 .Set("SourceFileName", f.FileName));

                    // If backup folder exists, move file
                    if (parsedBackupLocation.HasConnectionString)
                    {
                        // TODO: implement move operation if location are the same!
                        var destName = parsedBackupLocation.GetDestinationPath(f.FileName);
                        using (var backup = parsedBackupLocation.OpenConnection())
                            backup.SendFile(input.GetFileStream(f.FileName).FileStream, destName, true);
                    }

                    // If DeleSource is set
                    if (deleteSourceFile)
                    {
                        input.RemoveFile(f.FileName);
                    }

                    // limit
                    if (filesCount >= maxFilesCount)
                    {
                        break;
                    }
                }

                if (filesCount > 0)
                {
                    _logger.Success("Done");
                    return(true);
                }
                else
                {
                    _logger.Debug("No Files Found on: " + searchPath);
                    return(true);
                }
            }
            catch (Exception ex)
            {
                context.Error = ex.Message;
                _logger.Error(ex);
                try
                {
                    if (lastFile != null && input != null && parsedErrorLocation.HasConnectionString)
                    { // move files
                        var destName = parsedErrorLocation.GetDestinationPath(lastFile);
                        using (var backup = parsedErrorLocation.OpenConnection())
                            backup.SendFile(input.GetFileStream(lastFile).FileStream, destName, true);
                    }
                }
                catch { }
                return(false);
            }
            finally
            {
                if (output != null)
                {
                    output.Dispose();
                }
                if (input != null)
                {
                    input.Dispose();
                }
            }
        }