示例#1
0
        public void GetFileFromBarclaysNewSiteUsingSFTP()
        {
            var file1 = "mapleb_PM28C5-1_S_287054_20150209.dat"; var file2 = "mapleb_PM28C5-1_S_287054_20150210.dat";

            File.Delete(file1); File.Delete(file2);

            string output;

            Maple.FTP.SFTP sftp = new Maple.FTP.SFTP();

            sftp.SFtpGet(new List <string>()
            {
                file1, file2
            },
                         "ftp.prime.barcap.com",
                         "in", "maplecft", "mmaboss14", out output, 2222);

            Assert.IsTrue(File.Exists(file1)); Assert.IsTrue(File.Exists(file2));
        }
示例#2
0
        private string FetchRemoteFile(FtpDetail detail, RetrieveItem item)
        {
            string uri = "", userName = "", pw = "", folder = "", ftpStore = "", domain = "";

            ftpStore = FindFileLocation(detail, item);

            string ext = Path.GetExtension(item.FileName);

            folder = detail.Folder;

            // Temp area to store files before we move them
            string tempFolder = Path.GetTempPath();

            switch (detail.FptType)
            {
            case FtpDetail.eFtpType.ftp:
                if (!folder.EndsWith("/"))
                {
                    folder += "/";
                }
                Database.FetchLoginInfo(detail.FtpLookupValue, out uri, out userName, out pw);
                uri = "ftp://" + uri + "/" + folder + item.FileName;
                Maple.FTP.FTP.Download(uri, userName, pw, tempFolder + item.FileName, true);
                break;

            case FtpDetail.eFtpType.sftp:
                if (!folder.EndsWith("/"))
                {
                    folder += "/";
                }
                int portNumberOverride;
                Database.FetchLoginInfo(detail.FtpLookupValue, out uri, out userName, out pw, out portNumberOverride);
                Maple.FTP.SFTP ftp           = new Maple.FTP.SFTP();
                string         sessionOutput = string.Empty;
                List <string>  files         = new List <string>();
                files.Add(item.FileName);
                if (portNumberOverride == -1)
                {
                    ftp.SFtpGet(files, uri, detail.Folder, userName, pw, out sessionOutput);
                }
                else
                {
                    ftp.SFtpGet(files, uri, detail.Folder, userName, pw, out sessionOutput, portNumberOverride);
                }
                File.Move(Path.GetDirectoryName(Application.ExecutablePath) + "\\" + files[0], tempFolder + files[0]);
                break;

            case FtpDetail.eFtpType.UnixShare:
                if (!folder.EndsWith("\\"))
                {
                    folder += "\\";
                }
                File.Delete(tempFolder + item.FileName);
                File.Copy(folder + item.FileName, tempFolder + item.FileName);
                break;

            case FtpDetail.eFtpType.Https:
                if (!folder.EndsWith("/"))
                {
                    folder += "/";
                }
                Database.FetchLoginInfo(detail.FtpLookupValue, out uri, out userName, out pw);
                uri = uri + "/" + item.FileName;

                domain   = Path.GetDirectoryName(userName);
                userName = Path.GetFileName(userName);

                WebClient webClient = new WebClient();
                webClient.Credentials = new NetworkCredential(userName, pw, domain);

                //create temp folder first
                Directory.CreateDirectory(tempFolder + Path.GetDirectoryName(item.FileName));
                webClient.DownloadFile(uri, tempFolder + item.FileName);

                break;
            }

            string toReturn = "";

            // Check that file is there
            if (File.Exists(tempFolder + item.FileName))
            {
                string newFileName = item.FileName;
                string lastdir     = "";
                int    number;

                if (detail.PGP)
                {
                    item.FileName = Decryption.PGP(tempFolder, item.FileName);
                }
                if (detail.ZIP)
                {
                    lastdir = Path.GetDirectoryName(item.FileName);
                    if (int.TryParse(lastdir, out number))
                    {
                        //directory includes date
                        Directory.CreateDirectory(ftpStore + lastdir);
                    }
                    newFileName = Decryption.ZIP(tempFolder, item.FileName, domain);
                    ext         = Path.GetExtension(newFileName);
                }
                if (detail.DES)
                {
                    item.FileName = Decryption.DES(tempFolder, item.FileName);
                }

                // Move to permanent location
                Directory.CreateDirectory(ftpStore);

                //int count = 1;
                newFileName = Path.GetFileNameWithoutExtension(newFileName);

                string add = "";
                // This part finds a new filename if it already exists
                //while (File.Exists(ftpStore + newFileName + add + ext)) {
                //    add = string.Format(" ({0})", count++);
                //}

                if (lastdir != "")
                {
                    string newFileLocation = lastdir + "/" + newFileName + ext;
                    File.Copy(tempFolder + newFileLocation, ftpStore + newFileLocation, true);
                    toReturn = ftpStore + newFileLocation;
                    File.Delete(tempFolder + newFileLocation);
                }
                else
                {
                    File.Copy(tempFolder + item.FileName, ftpStore + newFileName + add + ext, true);
                    toReturn = ftpStore + newFileName + add + ext;
                    File.Delete(tempFolder + item.FileName);
                }
            }

            return(toReturn);
        }