Beispiel #1
0
        public static bool DownloadFile(Uri DlUrl, string OutName = "")
        {
            bool Finished = false;

            Terminal.CWriteLine("$c [DOWNLOAD]");
            Terminal.CWriteLine("$fDownload Server: $a{0}", DlUrl.Host);

            string Filename = null;
            string MimeType = "";

            try
            {
                WebRequest hwr = HttpWebRequest.Create(DlUrl);
                hwr.Method = "HEAD";
                WebResponse wr = hwr.GetResponse();
                if (wr.Headers["Content-Disposition"] != null)
                {
                    Match m = FilenameMatch.Match(wr.Headers["Content-Disposition"]);
                    if (m.Success)
                    {
                        Filename = m.Groups[1].Value;
                    }
                }
                if (wr.Headers["Content-Type"] != null)
                {
                    MimeType = wr.Headers["Content-Type"];
                }
            } catch (Exception) { }
            if (Filename != null)
            {
                Filename = InvalidPath.Replace(Filename, "_");
                Terminal.CWriteLine("$fOnline Filename: $a{0}", Filename);
            }
            else
            {
                try
                {
                    Filename = Path.GetFileName(DlUrl.LocalPath);
                }
                catch (Exception)
                {
                    Filename = RandomString(4);
                };
            }
            if (MimeType.Length > 0)
            {
                Terminal.CWriteLine("$fMime: $a{0}", Mime.ParseMime(MimeType));
            }
            FileInfo FullFile = new FileInfo(OutName.Length == 0 ? Filename : OutName);

            if (FullFile.Extension.Length > 1)
            {
                Filename = FullFile.Name;
            }
            else
            {
                string Format = Mime.MimeToType(MimeType);
                Filename = Path.GetFileNameWithoutExtension(Filename) + "." + Format;
            }
            FileInfo Output = new FileInfo(Path.Combine(Environment.CurrentDirectory, Filename));

            Terminal.CWriteLine("$fOutput: $a{0}", Output.Name);
            //return false;

            WebClient wc = new WebClient();

            WorkingWC.Add(wc);
            wc.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36";
            CursorPosition p = Terminal.GetPos();

            int Begin   = 0;
            int BarSize = 25 + 3;
            Action <int, float, bool> UpdateProgress = (b, speed, l) =>
            {
                int Progress = (int)Math.Round(((double)b / 100.0) * (BarSize - 3.0));
                Terminal.SetPos(p);
                Terminal.CWriteLine("$a[{0}] {1:0.00} kb/s", ">".PadLeft(Progress, '=').PadRight(BarSize - 3, '-'), speed);
            };
            AntiSpam Spam = new AntiSpam();

            wc.DownloadProgressChanged += (o, e) =>
            {
                if (Spam.Ignore())
                {
                    return;
                }
                float speed = (float)((double)e.BytesReceived / ((double)Environment.TickCount - (double)Begin));
                UpdateProgress(e.ProgressPercentage, speed, false);
            };
            wc.DownloadFileCompleted += (o, e) =>
            {
                Finished = true;
            };
            UpdateProgress(0, 0, true);
            Begin = Environment.TickCount;
            wc.DownloadFileAsync(DlUrl, Output.FullName);
            while (!Finished)
            {
                Thread.Sleep(250);
                if (wc == null)
                {
                    return(false);
                }
            }
            Thread.Sleep(250);
            UpdateProgress(100, 0, true);
            Terminal.WriteLine();
            return(true);
        }