Ejemplo n.º 1
0
        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);
        }