byte[] ScpGetData(PerformContext hangfireContext) { string ldbUrl = Configuration["LDB_URL"]; string lbUsername = Configuration["LDB_USERNAME"]; string ldbPassword = Configuration["LDB_PASSWORD"]; // check that configuration is valid. if (string.IsNullOrEmpty(ldbUrl) || string.IsNullOrEmpty(lbUsername) || string.IsNullOrEmpty(ldbPassword)) { return(null); } byte[] result; // login to the scp service. if (hangfireContext != null) { hangfireContext.WriteLine($"Connecting to SCP server {ldbUrl}"); } using (var sftp = new SftpClient(ldbUrl, lbUsername, ldbPassword)) { sftp.Connect(); if (sftp.IsConnected) { if (hangfireContext != null) { hangfireContext.WriteLine($"Connected to SCP server {ldbUrl}"); } var status = sftp.GetStatus("."); var files = sftp.ListDirectory(""); foreach (var file in files) { if (hangfireContext != null) { hangfireContext.WriteLine($"Found file {file.FullName}"); } } } sftp.Disconnect(); } result = null; return(result); }
private void EnsureFreeSpace(SftpClient client) { // ensure that we have enough space before we continue uploading // TODO: this is blocking, make sure we keep this off the UI thread var status = client.GetStatus(this.config.SFTPPublishPath); // a block size appears to be around 4k on my current Raspbian version. // we'll go with just using available percentage for now. var currentAvailable = status.AvailableBlocks / (double)status.TotalBlocks; var formattedCurrentAvailable = (currentAvailable * 100).ToString("0.###"); if (currentAvailable <= this.config.MinAvailableSpaceOnPi) { throw new ApplicationException($"Cannot continue, the server only has {formattedCurrentAvailable}% space available!"); } else { this.logger.Verbose($"the server has {formattedCurrentAvailable}% space available, continuing with upload..."); } }
public bool UploadSurveyLogs() { if (password == null || password.Length < 1) { if (debug) { Debug.LogError("sftpaccess >> No Password specified!"); } return(false); } using (var client = new SftpClient(host, username, password)) { client.Connect(); if (debug) { Debug.Log("Is connected? " + client.IsConnected); } string baseReadWriteDir = "/surveylog/"; string playerName = PlayerPrefs.GetString("name"); //check if /$name exists, if not create it, then switch the workdir client.ChangeDirectory(baseReadWriteDir); if (!client.Exists(playerName)) { client.CreateDirectory(playerName); } client.ChangeDirectory(playerName); //check if /$name/$type exists, if not create it, then switch the workdir if (!client.Exists(gameTypeString)) { client.CreateDirectory(gameTypeString); } client.ChangeDirectory(gameTypeString); if (debug) { Debug.Log("Changed directory to " + baseReadWriteDir); } // no. of files currently int inherentNoOfFiles = client.ListDirectory(client.WorkingDirectory).Count(); //store logs string logDir = Application.persistentDataPath + "/SurveyLog/"; string[] files = Directory.GetFiles(logDir); foreach (var uploadFile in files) { if (debug) { Debug.Log("Filename=" + uploadFile + " | contains typeString=" + gameTypeString + "? " + uploadFile.ToUpper().Contains(gameTypeString.ToUpper())); } if (uploadFile.ToUpper().Contains(gameTypeString.ToUpper()) && uploadFile.ToUpper().Contains(PlayerPrefs.GetString("name").ToUpper())) { using (var fileStream = new FileStream(uploadFile, FileMode.Open)) { client.BufferSize = 4 * 1024; // bypass Payload error large files client.UploadFile(fileStream, Path.GetFileName(uploadFile), true); } } } if (debug) { Debug.Log(client.GetStatus(client.WorkingDirectory)); } int differenceOfFileNumbers = client.ListDirectory(client.WorkingDirectory).Count() - inherentNoOfFiles; if (debug) { Debug.Log("Successful? diffInFiles=" + differenceOfFileNumbers); } if (differenceOfFileNumbers >= 0) { client.Disconnect(); return(true); } client.Disconnect(); return(false); } }
public void GetStatusTest() { 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 SftpFileSytemInformation expected = null; // TODO: Initialize to an appropriate value SftpFileSytemInformation actual; actual = target.GetStatus(path); Assert.AreEqual(expected, actual); Assert.Inconclusive("Verify the correctness of this test method."); }