public List <FtpDownloadMetadata> GetAvailableDownloadList(FtpDownloadSettings ftpDownloadSettings)
        {
            var           returnList  = new List <FtpDownloadMetadata>();
            List <string> patternList = ftpDownloadSettings.FilePatternList;

            if (patternList == null || patternList.Count == 0)
            {
                patternList.Add("*.*");
            }
            foreach (string pattern in patternList)
            {
                var list = _ftpCommunicator.GetFileList(ftpDownloadSettings.SettingSource, ftpDownloadSettings.SettingKey, ftpDownloadSettings.DownloadDirectory, pattern);
                if (list != null && list.Count > 0)
                {
                    foreach (string fileName in list)
                    {
                        var newItem = new FtpDownloadMetadata
                        {
                            Directory = ftpDownloadSettings.DownloadDirectory,
                            FileName  = fileName,
                            Settings  = ftpDownloadSettings
                        };
                        returnList.Add(newItem);
                    }
                }
            }
            return(returnList);
        }
        public FtpInboundData TryCreateJobData(FtpDownloadMetadata item, out bool jobAlreadyExisted)
        {
            FtpInboundData returnValue;

            if (item.Settings.IgnoreDuplicates)
            {
                using (var transaction = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions {
                    IsolationLevel = IsolationLevel.Serializable
                }))
                {
                    returnValue = _documentSession.Query <FtpInboundData>()
                                  .Customize(i => i.WaitForNonStaleResultsAsOfLastWrite(TimeSpan.FromSeconds(120)))
                                  .SingleOrDefault(i => i.Directory == item.Directory && i.FileName == item.FileName);
                    if (returnValue == null)
                    {
                        returnValue = new FtpInboundData
                        {
                            Directory              = item.Directory,
                            FileName               = item.FileName,
                            QueuedDateTimeUtc      = DateTime.UtcNow,
                            InboundQueueIdentifier = item.Settings.InboundQueueIdentifier,
                            AttachmentId           = this.DownloadFile(item)
                        };
                        jobAlreadyExisted = false;

                        _documentSession.Store(returnValue);
                        _documentSession.SaveChanges();
                        transaction.Complete();
                    }
                    else
                    {
                        jobAlreadyExisted = true;
                    }
                }
            }
            else
            {
                returnValue = new FtpInboundData
                {
                    Directory              = item.Directory,
                    FileName               = item.FileName,
                    QueuedDateTimeUtc      = DateTime.UtcNow,
                    InboundQueueIdentifier = item.Settings.InboundQueueIdentifier,
                    AttachmentId           = this.DownloadFile(item)
                };
                jobAlreadyExisted = false;

                _documentSession.Store(returnValue);
                _documentSession.SaveChanges();
            }
            return(returnValue);
        }
        public string DownloadFile(FtpDownloadMetadata item)
        {
            string ftpFilePath;

            if (!string.IsNullOrEmpty(item.Directory))
            {
                if (item.Directory.EndsWith("/"))
                {
                    ftpFilePath = item.Directory + item.FileName;
                }
                else
                {
                    ftpFilePath = item.Directory + "/" + item.FileName;
                }
            }
            else
            {
                ftpFilePath = item.FileName;
            }

            string tempDirectory = Path.Combine(Path.GetTempPath(), "MMDB.DataService");

            if (!Directory.Exists(tempDirectory))
            {
                Directory.CreateDirectory(tempDirectory);
            }
            string tempPath = Path.Combine(tempDirectory, Guid.NewGuid().ToString() + "." + item.FileName);

            try
            {
                _ftpCommunicator.DownloadFile(item.Settings.SettingSource, item.Settings.SettingKey, ftpFilePath, tempPath);
                string attachmentID = Guid.NewGuid().ToString();
                using (FileStream stream = new FileStream(tempPath, FileMode.Open))
                {
                    _ravenManager.SetAttachment(attachmentID, stream);
                }
                return(attachmentID);
            }
            finally
            {
                try
                {
                    if (File.Exists(tempPath))
                    {
                        File.Delete(tempPath);
                        tempPath = null;
                    }
                }
                catch { }
            }
        }