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 = "";

        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 JArray rootFolder;
        private returnData _objectArry;
        private ServerFolders serverFolders;

        private void btnRun_Click(object sender, EventArgs e)
        {
            treeView1.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 button1_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[0] != null)
                    {
                        uploadFolders.Add(FindFolderInNodes(rootFolder, node.Text)[0].ToString().Replace("\"", ""));
                    }
                }

                json = JsonConvert.SerializeObject(uploadFolders);

                WebClient wc = new WebClient();

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

                debugMessage = Encoding.ASCII.GetString(wc.UploadValues("http://republiccommandos.co.za/filelist/copyFolders", test));
                MessageBox.Show("Upload Complete");
            }
            catch (Exception exp)
            {
                debugMessage = exp.Message;
                MessageBox.Show("Upload Failed Check the debug info");
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //WebClient wc = new WebClient();

            //var test = new NameValueCollection();
            //test.Add("username", "jameel");
            //if(false.ToString().ToLower() == Encoding.ASCII.GetString(wc.UploadValues("http://overlord:5000/login", test)).ToLower())
            //{
            //    Application.Exit();
            //}
        }

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

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            WebClient wc = new WebClient();

            _objectArry = (returnData)JsonConvert.DeserializeObject(wc.DownloadString("http://republiccommandos.co.za/filelist/"), typeof(returnData));
            serverFolders = (ServerFolders)JsonConvert.DeserializeObject(wc.DownloadString("http://republiccommandos.co.za/filelist/serverFolders"), typeof(ServerFolders));
            var copyDriveStatus = (DriveStatus)JsonConvert.DeserializeObject(wc.DownloadString("http://republiccommandos.co.za/filelist/driveStatus"), typeof(DriveStatus));
            //lblDriveStatus.Text = copyDriveStatus.status.ToString();
        }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            setupTree(_objectArry);
            rootFolder = _objectArry.userFiles;

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

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

            lblProgress.Text = "Download Complete";
        }
    }
}