Пример #1
0
        private static string DownloadFile(string serverUrl, string localPath, string fileName, string fileExtension,
                                           string userName,
                                           string userPassword,
                                           FlexiCapture.Cloud.ServiceAssist.DB.FTPConversionSettings ftpConvSettings, FTPSetting ftpSettings)
        {
            try
            {
                string bbaseUri  = "ftp://" + serverUrl;
                string uri       = Path.Combine(localPath, fileName);
                Uri    baseUri   = new Uri(bbaseUri);
                Uri    serverUri = new Uri(baseUri, uri);

                if (serverUri.Scheme != Uri.UriSchemeFtp)
                {
                    return("");
                }
                FtpWebRequest reqFTP;
                reqFTP             = (FtpWebRequest)FtpWebRequest.Create(serverUri.AbsoluteUri);
                reqFTP.Credentials = new NetworkCredential(userName, PasswordHelper.Crypt.DecryptString(userPassword));
                reqFTP.KeepAlive   = false;
                reqFTP.Method      = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.Timeout     = 10000;
                reqFTP.KeepAlive   = false;
                reqFTP.ServicePoint.ConnectionLeaseTimeout = 20000;
                reqFTP.ServicePoint.MaxIdleTime            = 20000;
                //reqFTP.UseBinary = true;
                //reqFTP.Proxy = null;
                //reqFTP.UsePassive = false;
                FtpWebResponse response       = (FtpWebResponse)reqFTP.GetResponse();
                Stream         responseStream = response.GetResponseStream();
                FileStream     writeStream    =
                    new FileStream(Path.Combine(SettingsHelper.GetSettingsValueByName("MainPath"), "data", "uploads", fileName),
                                   FileMode.Create);

                int    Length    = 2048;
                Byte[] buffer    = new Byte[Length];
                int    bytesRead = responseStream.Read(buffer, 0, Length);
                while (bytesRead > 0)
                {
                    writeStream.Write(buffer, 0, bytesRead);
                    bytesRead = responseStream.Read(buffer, 0, Length);
                }
                writeStream.Close();
                string pathToPut = "/.";
                if (ftpConvSettings.MoveProcessed && ftpSettings.ServiceType == 1)
                {
                    pathToPut = "/DCC_Processed/.";
                }


                PutFileOnFtpServer(
                    new FileInfo(Path.Combine(
                                     SettingsHelper.GetSettingsValueByName("MainPath"), "data", "uploads",
                                     fileName)), fileName, ftpSettings, pathToPut);

                reqFTP             = (FtpWebRequest)FtpWebRequest.Create(serverUri.AbsoluteUri);
                reqFTP.Credentials = new NetworkCredential(userName,
                                                           PasswordHelper.Crypt.DecryptString(userPassword));
                reqFTP.KeepAlive  = false;
                reqFTP.Method     = WebRequestMethods.Ftp.DeleteFile;
                reqFTP.UseBinary  = true;
                reqFTP.Proxy      = null;
                reqFTP.UsePassive = false;
                reqFTP.KeepAlive  = false;
                reqFTP.ServicePoint.ConnectionLeaseTimeout = 20000;
                reqFTP.ServicePoint.MaxIdleTime            = 20000;
                reqFTP.Timeout = 10000;
                response       = (FtpWebResponse)reqFTP.GetResponse();

                response.Close();

                return(fileName);
            }
            catch (Exception ex)
            {
                string innerException = ex.InnerException == null ? "" : ex.InnerException.Message;
                string methodName     = System.Reflection.MethodBase.GetCurrentMethod().Name;
                LogHelper.AddLog("Error in method: " + methodName + "; Exception: " + ex.Message + " Innner Exception: " +
                                 innerException);
                return("");
            }
        }
Пример #2
0
        public static List <Tuple <string, string> > ExtractFiles(FtpWebResponse response, FTPSetting ftpSettings)
        {
            FlexiCapture.Cloud.ServiceAssist.DB.FTPConversionSettings ftpConvSettings;

            using (var db = new FCCPortalEntities2())
            {
                if (AcceptableTypes == null)
                {
                    AcceptableTypes = db.DocumentTypes
                                      .Select(x => x).ToList();
                }

                ftpConvSettings = db.FTPConversionSettings
                                  .SingleOrDefault(x => x.UserId == ftpSettings.UserId);
            }

            Stream       responseStream = response.GetResponseStream();
            StreamReader reader         = new StreamReader(responseStream);

            List <Tuple <string, string> > fileNameExtensionTuples =
                new List <Tuple <string, string> >();

            List <Tuple <string, string> > newFileNameExtensionTuples =
                new List <Tuple <string, string> >();

            string line = reader.ReadLine();

            while (!string.IsNullOrEmpty(line))
            {
                var fileName = line;//.Split(' ').LastOrDefault();

                foreach (var type in AcceptableTypes)
                {
                    foreach (var splittedType in type.Extension.Split(';'))
                    {
                        if (fileName.Contains(splittedType))
                        {
                            fileNameExtensionTuples
                            .Add(Tuple.Create(fileName, splittedType));
                            break;
                        }
                    }
                }
                line = reader.ReadLine();
            }

            reader.Close();
            response.Close();

            fileNameExtensionTuples.ForEach(x =>
            {
                string storedFilename = DownloadFile(ftpSettings.Host, ftpSettings.Path,
                                                     x.Item1, x.Item2, ftpSettings.UserName, ftpSettings.Password, ftpConvSettings, ftpSettings);
                newFileNameExtensionTuples.Add(Tuple.Create(storedFilename, x.Item2));
            });

            return(newFileNameExtensionTuples);
        }