Source

remoteleecher / ServerBrowser.cs

Full commit
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Net;

namespace RemoteLeecher
{
    public partial class ServerBrowser : Form
    {
        public ServerBrowser()
        {
            InitializeComponent();
        }

        string debugMessage = "";
        public string UserName
        {
            get;
            set;
        }
        public string Password
        {
            get;
            set;
        }
        const string baseServerAddres = "http://192.168.10.114:5000/";
        JArray rootFolder;
        List<returnData> _objectArry = new List<returnData>();
        ServerFolders serverFolders;
        private DriveStatus copyDriveStatus;

        struct returnData
        {
            //Array of folders in format
            //[0] Folder Name
            //[1] Subfolders
            //[2] Files
            public JArray userFiles;
        }

        struct ServerFolders
        {
            //Array of folders
            //[0] Folder Name
            public JArray folders;
        }

        struct DriveStatus
        {
            public JToken status;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            treeView1.Nodes.Clear();
            treeView2.Nodes.Clear();
            lblProgress.Text = "Getting Latest Downloads";
            backgroundWorker.RunWorkerAsync();
            btnRun.Enabled = false;
            btnCopy.Enabled = false;
            btnViewDebug.Enabled = false;
        }

        private void setupTree(returnData objectArry)
        {
            JArray root = (JArray)JsonConvert.DeserializeObject(objectArry.userFiles[0].ToString());

            TreeNode noda = new TreeNode(root[0].ToString().Replace("\"", ""));
            noda.Tag = root;

            foreach (JToken jToken in (JArray)JsonConvert.DeserializeObject(root[1].ToString()))
            {
                SetupNodes(noda, objectArry.userFiles, root[0].ToString(), jToken.ToString());
            }

            treeView1.Nodes.Add(noda);
        }

        private void SetupNodes(TreeNode rootNode, JArray nodes, string rootFolderName, string folderName)
        {
            TreeNode subNode = new TreeNode(folderName.Replace("\"", ""));

            JArray folderStructure = FindFolderInNodes(nodes, folderName);

            if (folderStructure != null)
            {
                foreach (JToken jToken in (JArray)JsonConvert.DeserializeObject(folderStructure[2].ToString()))
                {
                    TreeNode fileNode = new TreeNode(jToken.ToString().Replace("\"", ""));
                    subNode.Nodes.Add(fileNode);
                }

                foreach (JToken jToken in (JArray)JsonConvert.DeserializeObject(folderStructure[1].ToString()))
                {
                    SetupNodes(subNode, nodes, rootFolderName + folderName, jToken.ToString());
                }
            }
            rootNode.Nodes.Add(subNode);
        }

        private JArray FindFolderInNodes(JArray dirs, string folderName)
        {
            for (int i = 0; i < dirs.Count; i++)
            {
                if (dirs[i].ToString().StartsWith("["))
                {
                    JArray node = (JArray)JsonConvert.DeserializeObject(dirs[i].ToString());
                    if (node[0].ToString().Replace("\"", "").ToLower().EndsWith(folderName.ToLower().Replace("\"", "")))
                    {
                        return node;
                    }
                }
            }
            return null;
        }

        private void treeView2_DragDrop(object sender, DragEventArgs e)
        {
            TreeNode NewNode;

            if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false))
            {
                TreeNode destinationNode = treeView2.Nodes[0].Nodes[0];
                NewNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
                destinationNode.Nodes.Add((TreeNode)NewNode.Clone());
                destinationNode.Expand();
                //if (DestinationNode.TreeView != NewNode.TreeView)
                //{
                //    DestinationNode.Nodes.Add((TreeNode)NewNode.Clone());
                //    DestinationNode.Expand();
                //    //Remove Original Node
                //    NewNode.Remove();
                //}
            }
        }

        private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
        {
            this.DoDragDrop(e.Item, DragDropEffects.Move);
        }

        private void treeView2_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }

        private void buttonPerformCopy_Click(object sender, EventArgs e)
        {
            try
            {
                //if (lblDriveStatus.Text == "False")
                //{
                //    MessageBox.Show("The copy drive is offline as such no copies can be performed");
                //    return;
                //}

                List<string> uploadFolders = new List<string>();
                string json = "";
                foreach (TreeNode node in treeView2.Nodes[0].Nodes[0].Nodes)
                {
                    //uploadFolders.Add(FindFolderInNodes(rootFolder, node.Text));
                    var folder = FindFolderInNodes(rootFolder, node.Text);
                    if (folder != null)
                    {
                        uploadFolders.Add(FindFolderInNodes(rootFolder, node.Text)[0].ToString().Replace("\"", ""));
                    }
                    else
                    {
                        uploadFolders.Add(node.Text);
                    }
                }

                json = JsonConvert.SerializeObject(uploadFolders);

                WebClient wc = new WebClient();

                var test = new NameValueCollection { { "folders", json } };
                ServicePointManager.Expect100Continue = false;

                debugMessage = Encoding.ASCII.GetString(wc.UploadValues(baseServerAddres + "copyFolders", test));
                MessageBox.Show("Upload Complete");
            }
            catch (Exception exp)
            {
                debugMessage = exp.Message;
                MessageBox.Show("Upload Failed Check the debug info");
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show(debugMessage);
        }

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            WebClient wc = new WebClient();
            List<string> folders = wc.DownloadString(baseServerAddres + "").Split(new string[] { ",{" }, StringSplitOptions.None).ToList();
            foreach (string folder in folders)
            {
                string serverFolder = folder;
                if (!serverFolder.StartsWith("{"))
                {
                    serverFolder = "{" + serverFolder;
                }
                var returnObject = JsonConvert.DeserializeObject(serverFolder, typeof(returnData));
                _objectArry.Add((returnData)returnObject);
            }

            serverFolders = (ServerFolders)JsonConvert.DeserializeObject(wc.DownloadString(baseServerAddres + "serverFolders"), typeof(ServerFolders));
            copyDriveStatus = (DriveStatus)JsonConvert.DeserializeObject(wc.DownloadString(baseServerAddres + "driveStatus"), typeof(DriveStatus));
            //lblDriveStatus.Text = copyDriveStatus.status.ToString();
        }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            foreach (returnData data in _objectArry)
            {
                setupTree(data);
                rootFolder = data.userFiles;
            }

            if (treeView2.Nodes.Count == 0)
            {
                treeView2.Nodes.Add("Drive");
            }

            treeView2.Nodes[0].Nodes.Add(UserName);
            //treeView2.Nodes[0].Nodes.Add(serverFolders.folders[0].Value<string>());
            treeView2.ExpandAll();

            btnRun.Enabled = true;
            btnCopy.Enabled = true;
            btnViewDebug.Enabled = true;

            lblDriveStatus.Text = copyDriveStatus.status.ToString().Replace("\"", "");
            lblProgress.Text = "Download Complete";
        }

        private void ServerBrowser_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void btnCalc_Click(object sender, EventArgs e)
        {
            try
            {

                List<string> uploadFolders = new List<string>();
                string json = "";
                foreach (TreeNode node in treeView2.Nodes[0].Nodes[0].Nodes)
                {
                    //uploadFolders.Add(FindFolderInNodes(rootFolder, node.Text));
                    var folder = FindFolderInNodes(rootFolder, node.Text);
                    if (folder != null)
                    {
                        uploadFolders.Add(FindFolderInNodes(rootFolder, node.Text)[0].ToString().Replace("\"", ""));
                    }
                    else
                    {
                        uploadFolders.Add(node.Text);
                    }
                }

                json = JsonConvert.SerializeObject(uploadFolders);

                WebClient wc = new WebClient();

                var test = new NameValueCollection { { "folders", json } };
                ServicePointManager.Expect100Continue = false;

                debugMessage = Encoding.ASCII.GetString(wc.UploadValues(baseServerAddres + "calcFolderSize", test));
            }
            catch (Exception exp)
            {
                debugMessage = exp.Message;
                MessageBox.Show("Upload Failed Check the debug info");
            }
        }
    }
}