コード例 #1
0
ファイル: PersistService.cs プロジェクト: jetelain/Arma3Event
        public PersistBackup GetBackup(string server, string backupName)
        {
            var servers = _config.GetSection("Servers");
            var entry   = servers.GetSection(server);

            if (entry == null)
            {
                return(null);
            }
            PersistBackup backup   = null;
            var           hostName = entry.Key;
            var           password = entry.Value;

            using (var client = new SftpClient(hostName, "arma3-public", password))
            {
                client.Connect();
                if (client.Exists(filePath))
                {
                    var mem = new MemoryStream();
                    client.DownloadFile(filePath, mem);
                    mem.Position = 0;
                    backup       = PersistBackup.Read(mem, client.GetLastWriteTime(filePath), hostName).FirstOrDefault(b => b.Name == backupName);
                }
                client.Disconnect();
            }
            return(backup);
        }
コード例 #2
0
        public void Test_Sftp_Connect_Via_HttpProxy()
        {
            var connInfo = new PasswordConnectionInfo(Resources.HOST, Resources.USERNAME, Resources.PASSWORD, ProxyTypes.Http, Resources.PROXY_HOST, int.Parse(Resources.PROXY_PORT));

            using (var client = new SftpClient(connInfo))
            {
                client.Connect();

                var ret = client.GetLastWriteTime(".");
                Debug.WriteLine(ret, "GetLastWriteTime");

                client.Disconnect();
            }
        }
コード例 #3
0
ファイル: PersistService.cs プロジェクト: jetelain/Arma3Event
        public List <PersistBackup> GetBackups()
        {
            var servers = _config.GetSection("Servers");
            var backups = new List <PersistBackup>();

            foreach (var entry in servers.GetChildren())
            {
                var hostName = entry.Key;
                var password = entry.Value;
                using (var client = new SftpClient(hostName, "gamemanager", password))
                {
                    client.Connect();
                    if (client.Exists(filePath))
                    {
                        var mem = new MemoryStream();
                        client.DownloadFile(filePath, mem);
                        mem.Position = 0;
                        backups.AddRange(PersistBackup.Read(mem, client.GetLastWriteTime(filePath), hostName));
                    }
                    client.Disconnect();
                }
            }
            return(backups.OrderByDescending(b => b.LastChange).ToList());
        }
コード例 #4
0
        public async Task <Cennik> Get()
        {
            string host     = _configuration.GetSection("FtpConn").GetSection("Address").Value;
            string username = _configuration.GetSection("FtpConn").GetSection("Login").Value;
            string password = _configuration.GetSection("FtpConn").GetSection("Pass").Value;
            string imageUrl = _configuration.GetSection("ImageFolder").Value;

            string localPath  = Path.Combine(System.IO.Directory.GetCurrentDirectory(), "cennik.json");
            string remotePath = "/srv/kasy/cennik.json";
            string imageFile  = Path.Combine(System.IO.Directory.GetCurrentDirectory(), "urls/images/");


            using (SftpClient sftp = new SftpClient(host, 11122, username, password))
            {
                try
                {
                    sftp.Connect();

                    var sftpFile  = sftp.GetLastWriteTime(remotePath);
                    var localFile = System.IO.File.GetLastWriteTime(localPath);

                    var fileExists = System.IO.File.Exists(localPath);

                    if (fileExists)
                    {
                        if (sftpFile > localFile)
                        {
                            await using Stream fileStream = System.IO.File.OpenWrite(localPath);
                            sftp.DownloadFile(remotePath, fileStream);
                        }
                    }
                    else
                    {
                        await using Stream fileStream = System.IO.File.OpenWrite(localPath);
                        sftp.DownloadFile(remotePath, fileStream);
                    }

                    sftp.Disconnect();

                    var json = System.IO.File.ReadAllText(localPath);

                    Cennik Cennik = JsonConvert.DeserializeObject <Cennik>(json);

                    Cennik.cennik = Cennik.cennik.Where(x => x.promocja || x.rabat > 0).ToList();

                    DirectoryInfo dir = new DirectoryInfo(imageFile);
                    foreach (var item in Cennik.cennik)
                    {
                        FileInfo[] files = dir.GetFiles(item.index + ".*");
                        if (files.Length > 0)
                        {
                            var name = files[0].Name;
                            item.imageUrl = imageUrl + name;
                        }
                    }

                    return(Cennik);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    System.IO.File.Delete(localPath);
                }
            }

            return(null);
        }
コード例 #5
0
ファイル: SftpClientTest.cs プロジェクト: pecegit/sshnet
 public void GetLastWriteTimeTest()
 {
     ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
     SftpClient target = new SftpClient(connectionInfo); // TODO: Initialize to an appropriate value
     string path = string.Empty; // TODO: Initialize to an appropriate value
     DateTime expected = new DateTime(); // TODO: Initialize to an appropriate value
     DateTime actual;
     actual = target.GetLastWriteTime(path);
     Assert.AreEqual(expected, actual);
     Assert.Inconclusive("Verify the correctness of this test method.");
 }