Exemplo n.º 1
0
        /**************************************************************************************\
         * Parse timestamp and size, and then fill directory object (for each file in folder) *
        \**************************************************************************************/
        private void ParseSizeAndTimestamp(Directory directory, string fileName, List<string> lines)
        {
            /* Define path of file or folder */
            string filePath = String.Empty;
            filePath = _Host + "/" + fileName;

            /* Select line to parse */
            string lineToParse = String.Empty;
            foreach (string line in lines)
            {
                if (line.Contains(fileName))
                {
                    lineToParse = line;
                    break;
                }
            }

            /* Test all regex to find the good timestamp/size patern, then create file object */
            Regex regex = new Regex("(.*)([A-Z][a-z]{2}[ ]{1,2}[0-9]{1,2} [0-9]{2}:[0-9]{2})(.*)k <A HREF"); // Regex for the format 'Jan 14 11:01  68773k'.
            Regex regex2 = new Regex("(.*)([0-9]{2}-[0-9]{2}-[0-9]{2} )([0-9]{2}:[0-9]{2}[A|M|P]{2})(.*)k <A HREF"); // Regex for the format '04-22-13 08:18PM  53019k'.
            Regex regex3 = new Regex("(.*)([A-Z][a-z]{2}[ ]{1,2}[0-9]{1,2}  [0-9]{4})(.*)k <A HREF"); // Regex for the format 'Dec  9  2013  37922k'.

            if (regex.IsMatch(lineToParse))
            {
                Match m = regex.Match(lineToParse);

                /* Set size, and timestamp */
                int size = int.Parse(m.Groups[3].Value.Replace(" ", ""));
                int month;
                int day;
                int hour;
                int minute;

                switch (m.Groups[2].Value.Substring(0, 3))
                {
                    case "Jan": month = 1; break;
                    case "Feb": month = 2; break;
                    case "Mar": month = 3; break;
                    case "Apr": month = 4; break;
                    case "May": month = 5; break;
                    case "Jun": month = 6; break;
                    case "Jul": month = 7; break;
                    case "Aug": month = 8; break;
                    case "Sep": month = 9; break;
                    case "Oct": month = 10; break;
                    case "Nov": month = 11; break;
                    case "Dec": month = 12; break;
                    default: month = 1; break;
                }

                day = int.Parse(m.Groups[2].Value.Substring(4, 2));
                hour = int.Parse(m.Groups[2].Value.Substring(7, 2));
                minute = int.Parse(m.Groups[2].Value.Substring(10, 2));

                DateTime dt = new DateTime(System.DateTime.Now.Year, month, day, hour, minute, 0);

                /* Create a file.*/
                directory.Get_FilesList().Add(new File(fileName, filePath, size, dt));
            }

            else if (regex2.IsMatch(lineToParse))
            {
                Match m = regex2.Match(lineToParse);

                /* Set size, and timestamp */
                int month = int.Parse(m.Groups[2].Value.Substring(0, 2));
                int day = int.Parse(m.Groups[2].Value.Substring(3, 2));
                int year = int.Parse(m.Groups[2].Value.Substring(6, 2)) + 2000;

                string ampm = m.Groups[3].Value.Substring(5, 2);
                int hour = int.Parse(m.Groups[3].Value.Substring(0, 2));
                if ((ampm.Equals("PM") || ampm.Equals("pm")) && hour != 12)
                    hour += 12;
                int minute = int.Parse(m.Groups[3].Value.Substring(3, 2));

                int size = int.Parse(m.Groups[4].Value.Replace(" ", ""));

                DateTime dt = new DateTime(year, month, day, hour, minute, 0);

                /* Create a file.*/
                directory.Get_FilesList().Add(new File(fileName, filePath, size, dt));
            }

            else if (regex3.IsMatch(lineToParse))
            {
                Match m = regex3.Match(lineToParse);

                /* Set size, and timestamp */
                int size = int.Parse(m.Groups[3].Value.Replace(" ", ""));
                int month;

                switch (m.Groups[2].Value.Substring(0, 3))
                {
                    case "Jan": month = 1; break;
                    case "Feb": month = 2; break;
                    case "Mar": month = 3; break;
                    case "Apr": month = 4; break;
                    case "May": month = 5; break;
                    case "Jun": month = 6; break;
                    case "Jul": month = 7; break;
                    case "Aug": month = 8; break;
                    case "Sep": month = 9; break;
                    case "Oct": month = 10; break;
                    case "Nov": month = 11; break;
                    case "Dec": month = 12; break;
                    default: month = 1; break;
                }

                int day = int.Parse(m.Groups[2].Value.Substring(4, 2));
                int year = int.Parse(m.Groups[2].Value.Substring(8, 4));

                DateTime dt = new DateTime(year, month, day, 0, 0, 0);

                /* Create a file.*/
                directory.Get_FilesList().Add(new File(fileName, filePath, size, dt));
            }

            /* Else, create a directory */
            else if (!fileName.Equals("Parent Directory")
                && !fileName.Equals("Root Directory")
                && !_Host.Replace("%20", " ").Contains(fileName)
                && !fileName.Contains(_User + "@")
                && !directory.Get_Name().Replace("%20"," ").Contains(fileName))
            {
                directory.Get_FoldersList().Add(new Directory(fileName, filePath, false));
            }
        }
Exemplo n.º 2
0
        /************************************\
         * File Explorer double click event *
         *    change directory              *
        \************************************/
        private void FileExplorerListView_DoubleClick(object sender, EventArgs e)
        {
            if (!_Worker.IsBusy && FileExplorerListView.SelectedItems.Count > 0)
            {
                if (FileExplorerListView.FocusedItem.ImageIndex == 1)
                {
                    /* Reset File Explorer Sorting */
                    FileExplorerListView.SetSortIcon(0, SortOrder.None);
                    FileExplorerListView.ListViewItemSorter = new ListViewItemComparer(0, SortOrder.Ascending);

                    /* Get directory to go */
                    Directory directoryToGo = new Directory();
                    foreach (Directory dir in _CurrentDirectory.Get_FoldersList())
                    {
                        if (dir.Get_Name().Equals(FileExplorerListView.SelectedItems[0].Text) || dir.Get_Name().Equals(FileExplorerListView.SelectedItems[0].Text.Replace(" ","%20")))
                        {
                            directoryToGo = dir;
                            directoryToGo.Set_ParentFolder(_CurrentDirectory);
                            break;
                        }
                    }

                    /* Change directory */
                    ChangeDirectory(directoryToGo);
                }

                else if (FileExplorerListView.FocusedItem.ImageIndex == 2)
                {
                    /* Go to parent */
                    LogcatRichTextBox.AppendText("Changing directory to : " + _CurrentDirectory.Get_ParentFolder().Get_Path() + "... ");
                    _CurrentDirectory = _CurrentDirectory.Get_ParentFolder();
                    FileExplorerListView.Items.Clear();
                    FillFileExplorerListView();
                    LogcatRichTextBox.AppendText("Done !\n");
                    LogcatRichTextBox.ScrollToCaret();

                    /* Update Files/dir counter label */
                    DirFileCounterLabel.Text = _CurrentDirectory.Get_FoldersList().Count + " dir / " + _CurrentDirectory.Get_FilesList().Count + " files";
                }
            }
        }
Exemplo n.º 3
0
        /*************************************************\
         * List Directory Contents File/Folder Name Only *
        \*************************************************/
        public void GetDirectoryList(Directory directory, BackgroundWorker worker, DoWorkEventArgs e)
        {
            /* Set parameters and return values */
            List<object> parameters = e.Argument as List<object>;
            List<object> returnArguments = new List<object>();

            if (((string)parameters[0]).Equals("GetDir"))
            {
                if (directory.Get_IsRoot())
                    returnArguments.Add("GetDir");
            }
            else if (((string)parameters[0]).Equals("DlLastUp"))
            {
                returnArguments.Add("DlLastUp");
            }
            else if (((string)parameters[0]).Equals("SyncLF"))
            {
                returnArguments.Add("SyncLF");
            }
            else if (((string)parameters[0]).Equals("ChDir"))
            {
                returnArguments.Add("ChDir");
            }

            if (directory.Get_FoldersList().Count == 0 && directory.Get_FilesList().Count == 0)
            {
                try
                {
                    // FYI
                    // http://stackoverflow.com/questions/1016690/how-to-improve-the-performance-of-ftpwebrequest

                    /* Instanciate FTP request */
                    worker.ReportProgress(0);
                    _FtpRequest = (FtpWebRequest)FtpWebRequest.Create(_Host);

                    /* Log in */
                    _FtpRequest.Credentials = new NetworkCredential(_User, _Password);

                    /* Set options */
                    _FtpRequest.UseBinary = true;
                    _FtpRequest.UsePassive = true;
                    _FtpRequest.KeepAlive = true;
                    _FtpRequest.ConnectionGroupName = "GetDirectoryList"; // To ensure that the connection pool is used
                    _FtpRequest.KeepAlive = true;
                    _FtpRequest.ServicePoint.ConnectionLimit = 2;

                    /* Specify type of request */
                    _FtpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
                    worker.ReportProgress(30);

                    /* Establish return communication with ftp server */
                    _FtpResponse = (FtpWebResponse)_FtpRequest.GetResponse();
                    worker.ReportProgress(60);

                    /* Establish return communication with ftp server */
                    //_FtpStream = _FtpResponse.GetResponseStream();
                    using (Stream ftpStream = _FtpResponse.GetResponseStream())
                    {
                        if (worker.CancellationPending)
                        {
                            e.Cancel = true;
                        }

                        else
                        {
                            /* get FTP server's response stream */
                            StreamReader ftpReader = new StreamReader(ftpStream, Encoding.ASCII);

                            /* Read Each Line of the Response and Append a Pipe to Each Line for Easy Parsing */
                            List<string> lines = new List<string>(); // temporary list
                            string htmlResult = String.Empty;
                            string line = ftpReader.ReadLine();
                            while (line != null && line != String.Empty)
                            {
                                if (worker.CancellationPending)
                                {
                                    e.Cancel = true;
                                    break;
                                }

                                lines.Add(line);
                                htmlResult += line;
                                line = ftpReader.ReadLine();
                            }
                            worker.ReportProgress(80);

                            /* parse HTML output (first, get only the name of file, then re-parse to get the timestamp/size */
                            try
                            {
                                //List<string> content = new List<string>();
                                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                                doc.LoadHtml(htmlResult);
                                foreach (HtmlAgilityPack.HtmlNode node in doc.DocumentNode.SelectNodes("//a[@href]"))
                                {
                                    if (worker.CancellationPending)
                                    {
                                        e.Cancel = true;
                                        break;
                                    }

                                    else if (!node.InnerText.Contains("&gt") && !String.IsNullOrEmpty(node.InnerText))
                                    {
                                        /* suppress last char '/' */
                                        string stringRaw = node.InnerText;
                                        if (stringRaw[stringRaw.Length - 1] == '/')
                                            stringRaw = stringRaw.Substring(0, stringRaw.Length - 1);
                                        string[] split = stringRaw.Split('/');

                                        /* Parse timestamp and size, and then fill directory object */
                                        ParseSizeAndTimestamp(directory, split[split.Length - 1], lines);
                                    }
                                    else if (node.InnerText.Contains("&gt") && !String.IsNullOrEmpty(node.InnerText))
                                    {
                                        /* suppress last char '/' */
                                        string stringRaw = node.Attributes[0].Value;
                                        if (stringRaw[stringRaw.Length - 1] == '/')
                                            stringRaw = stringRaw.Substring(0, stringRaw.Length - 1);
                                        string[] split = stringRaw.Split('/');

                                        /* Parse timestamp and size, and then fill directory object */
                                        ParseSizeAndTimestamp(directory, split[split.Length - 1], lines);
                                    }
                                }
                            }
                            catch (Exception ex) { returnArguments.Add(ex.ToString()); }

                            /* Resource Cleanup */
                            ftpReader.Close();
                            _FtpResponse.Close();
                            _FtpRequest = null;
                        }
                    }
                }
                catch (Exception ex) { returnArguments.Add(ex.ToString()); }
            }

            worker.ReportProgress(100);
            e.Result = returnArguments;
        }
Exemplo n.º 4
0
        private void Worker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            StopButton.Enabled = ButtonEnabled.False;

            if (e.Error != null)
            {
                LogcatRichTextBox.AppendText("Error ! Details : \n" + e.Error.Message + "\n");
                LogcatRichTextBox.ScrollToCaret();
                _FilesToDownload.Clear();
                _FtpAPI.Reset_DownloadedFilesTarget();
            }
            else if (e.Cancelled)
            {
                LogcatRichTextBox.AppendText("Cancelled !\n");
                LogcatRichTextBox.ScrollToCaret();
                ProgressBar.Value = 0;
                ProgressLabel.Text = " - ";
                _FilesToDownload.Clear();
                _FtpAPI.Reset_DownloadedFilesTarget();
            }
            else
            {
                List<object> returnArguments = e.Result as List<object>;
                if (returnArguments.Count > 1)
                {
                    for (int i = 1; i < returnArguments.Count; i++)
                    {
                        LogcatRichTextBox.AppendText("Error ! :\n" + (string)returnArguments[i]);
                    }
                    _FilesToDownload.Clear();
                    _FtpAPI.Reset_DownloadedFilesTarget();
                }

                else
                {
                    switch ((string)returnArguments[0])
                    {
                        case "GetDir":
                            LogcatRichTextBox.AppendText("Done !\n");
                            LogcatRichTextBox.ScrollToCaret();
                            _CurrentDirectory = _Root;
                            FillFileExplorerListView();
                            DirFileCounterLabel.Text = _Root.Get_FoldersList().Count + " dir / " + _Root.Get_FilesList().Count + " files";
                            break;

                        case "ChDir":
                            LogcatRichTextBox.AppendText("Done !\n");
                            LogcatRichTextBox.ScrollToCaret();
                            FillFileExplorerListView();
                            DirFileCounterLabel.Text = _CurrentDirectory.Get_FoldersList().Count + " dir / " + _CurrentDirectory.Get_FilesList().Count + " files";
                            break;

                        case "Download":
                            LogcatRichTextBox.AppendText("Downloaded to " + _FtpAPI.Get_DownloadedFilesTarget()[0] + ".\n");
                            for(int i = 1;i<_FtpAPI.Get_DownloadedFilesTarget().Count;i++)
                            {
                                LogcatRichTextBox.AppendText("................................................... Downloaded to " + _FtpAPI.Get_DownloadedFilesTarget()[i] + ".\n");
                            }
                            LogcatRichTextBox.ScrollToCaret();
                            AddDownloadLog();
                            break;

                        case "DlLastUp":
                            LogcatRichTextBox.AppendText("Done !\n");
                            LogcatRichTextBox.AppendText("Downloading last updates... ");
                            LogcatRichTextBox.ScrollToCaret();
                            _CurrentDirectory = _Root;
                            List<object> arguments = new List<object>();
                            arguments.Add("DlLastUpBis");

                            SetFilesToDownload();

                            if (_FilesToDownload.Count > 0)
                            {
                                if (String.IsNullOrEmpty(_ConnectedRegion.Get_TargetDirectory()) || !System.IO.Directory.Exists(_ConnectedRegion.Get_TargetDirectory()))
                                {
                                    FolderBrowserDialog openFolderDialog = new FolderBrowserDialog();
                                    openFolderDialog.SelectedPath = @"V:\DONNEES\INTERNATIONAL\DATA";
                                    openFolderDialog.Description = "Please select target folder";
                                    DialogResult result = openFolderDialog.ShowDialog();
                                    if (result == DialogResult.OK)
                                    {
                                        arguments.Add(openFolderDialog.SelectedPath);
                                    }
                                    else
                                    {
                                        LogcatRichTextBox.AppendText("Cancelled !\n");
                                        LogcatRichTextBox.ScrollToCaret();
                                        ProgressBar.Value = 0;
                                        ProgressLabel.Text = " - ";
                                        _FilesToDownload.Clear();
                                        _FtpAPI.Reset_DownloadedFilesTarget();
                                        break;
                                    }
                                }
                                else
                                {
                                    arguments.Add(_ConnectedRegion.Get_TargetDirectory());
                                }

                                StopButton.Enabled = ButtonEnabled.True;
                                _Worker.RunWorkerAsync(arguments);
                            }
                            else LogcatRichTextBox.AppendText("Already up to date !\n");
                            break;
                        case "SyncLF":
                            LogcatRichTextBox.AppendText("Done !\n");
                            LogcatRichTextBox.AppendText("Synchronizing with local file... ");
                            LogcatRichTextBox.ScrollToCaret();
                            _CurrentDirectory = _Root;
                            List<object> arguments2 = new List<object>();
                            arguments2.Add("SyncLFBis");

                            SetFilesToDownloadBis();

                            if (_FilesToDownload.Count > 0)
                            {
                                arguments2.Add(_ConnectedRegion.Get_TargetDirectory());
                                StopButton.Enabled = ButtonEnabled.True;
                                _Worker.RunWorkerAsync(arguments2);
                            }
                            else LogcatRichTextBox.AppendText("Already up to date !\n");
                            break;
                    }
                }
            }
        }