public static string DeleteFile(string server, string port, string selectedFile, string path, string checksumPath) { int portNumber = Int32.Parse(port); TcpClient client = new TcpClient(server, portNumber); string checksumFile = Checksums.getChecksumFile(path, checksumPath, selectedFile); Byte[] data = System.Text.Encoding.Unicode.GetBytes(FileHandler.DeleteToJSON(selectedFile, checksumFile)); NetworkStream stream = client.GetStream(); // Send the message to the connected TcpServer. stream.Write(data, 0, data.Length); // Buffer to store the response bytes. data = new Byte[1024 * 1024]; // String to store the response ASCII representation. String responseData = String.Empty; // Read the first batch of the TcpServer response bytes. Int32 bytes = stream.Read(data, 0, data.Length); responseData = System.Text.Encoding.Unicode.GetString(data, 0, bytes); responseData = cleanMessage(data); System.Diagnostics.Debug.WriteLine("Received: {0}", responseData); // Close everything. stream.Close(); client.Close(); string cleanData = SplitString(responseData); FileHandler response = JsonConvert.DeserializeObject <FileHandler>(cleanData); if (response.Status == "200") { File.Delete(path + selectedFile); Checksums.UpdateNewChecksums(path + checksumPath); return(response.Status); } else if (response.Status == "404") { return(response.Status); } else if (response.Status == "412") { return(response.Status); } return(""); }
public static void ExistsChecksums(string path) { if (Directory.Exists(path)) { if (!File.Exists(path + file)) { CreateChecksumsFile(path, file); } } else { // Try to create the directory. DirectoryInfo di = Directory.CreateDirectory(path); Checksums.ExistsChecksums(path); } }
public static void CreateFolderChecksum(string path, string pathChecksum) { // Specify the directory you want to manipulate. try { // Determine whether the directory exists. if (Directory.Exists(path + pathChecksum)) { Checksums.ExistsChecksums(path + pathChecksum); } else { // Try to create the directory. DirectoryInfo di = Directory.CreateDirectory(path + pathChecksum); Checksums.ExistsChecksums(path + pathChecksum); } } finally { } }
public static string FileToJSON(string selectedFile) { string fileName = Base64.Base64Encode(selectedFile); string path = "C:/idh14Client/" + selectedFile; Byte[] bytes = File.ReadAllBytes(path); String content = Convert.ToBase64String(bytes); string checksum = Checksums.GetSha1Hash(path); string originalchecksum = "test"; string str = "PUT {"; str += " 'filename': '" + fileName + "',"; str += " 'content': '" + content + "',"; str += " 'checksum': '" + checksum + "',"; str += " 'originalchecksum': '" + originalchecksum + "'"; str += "}"; return(str); }
public static List <FileHandler> GetFiles() { var model = new List <FileHandler>(); // Set c so folder can be checked by client DirectoryInfo c = new DirectoryInfo(@"C:\idh14Client\"); //Get all files FileInfo[] Files2 = c.GetFiles("*.*"); foreach (FileInfo file in Files2) { FileHandler tempFile = new FileHandler(); tempFile.FileName = file.Name; string filePath = c + file.Name; //Show SHA1 hash of current version of the file tempFile.Checksum = Checksums.GetSha1Hash(filePath); model.Add(tempFile); } return(model); }
public static string GetFile(string server, string port, string selectedFile, string path, string checksumPath) { int portNumber = Int32.Parse(port); TcpClient client = new TcpClient(server, portNumber); Byte[] data = System.Text.Encoding.Unicode.GetBytes(FileHandler.FileNameToJSON(selectedFile)); NetworkStream stream = client.GetStream(); // Send the message to the connected TcpServer. stream.Write(data, 0, data.Length); // Buffer to store the response bytes. data = new Byte[1024 * 1024]; // String to store the response ASCII representation. String responseData = String.Empty; // Read the first batch of the TcpServer response bytes. Int32 bytes = stream.Read(data, 0, data.Length); responseData = System.Text.Encoding.Unicode.GetString(data, 0, bytes); responseData = cleanMessage(data); System.Diagnostics.Debug.WriteLine("Received: {0}", responseData); // Close everything. stream.Close(); client.Close(); string cleanData = SplitString(responseData); FileHandler response = JsonConvert.DeserializeObject <FileHandler>(cleanData); if (response.Status == "200") { //3 stappen: als het bestand lokaal niet bestaat opslaan //Als het wel bestaat en checksum komt overeen: melding geven (Bestanden zijn al gelijk) //Als het wel bestaat en checksum komt niet overeen: melding geven (Conflict) List <string> filenames = FileHandler.GetFilenames(); var fileName = Base64.Base64Decode(response.FileName); if (filenames.Contains(fileName)) { bool fileInChecksum = Checksums.checksumFileCorrect(path, checksumPath, fileName); if (fileInChecksum == true) { File.Delete(path + fileName); var createFile = File.Create(path + fileName); createFile.Close(); File.WriteAllBytes(path + fileName, Convert.FromBase64String(response.Content)); Checksums.UpdateChecksums(path + checksumPath, response.Checksum, fileName); return(response.Status); } } else { var createFile = File.Create(path + fileName); createFile.Close(); File.WriteAllBytes(path + fileName, Convert.FromBase64String(response.Content)); Checksums.UpdateNewChecksums(path + checksumPath); return(response.Status); } } else if (response.Status == "404") { return(response.Status); } return(""); }