Example #1
0
 public static Task <bool> Download(ResourceDownloadType type, Action <int, string, string> downloadInfo)
 {
     return(Task.Run(() =>
     {
         return DownloadSync(type, downloadInfo);
     }));
 }
Example #2
0
        public static bool DownloadSync(ResourceDownloadType type, Action <int, string, string> downloadInfo)
        {
            try
            {
                if (!Exists(type))
                {
                    switch (type)
                    {
                    case ResourceDownloadType.MPV:
                        bool b = DownloadWebClient(type, downloadInfo);
                        if (b)
                        {
                            return(Unzip(type, downloadInfo));
                        }
                        break;

                    case ResourceDownloadType.Phantom:
                        b = DownloadWebClient(type, downloadInfo);
                        if (b)
                        {
                            return(Unzip(type, downloadInfo));
                        }
                        break;

                    case ResourceDownloadType.YT_DL:
                        b = DownloadWebClient(type, downloadInfo);
                        if (b)
                        {
                            return(DownloadSync(ResourceDownloadType.Phantom, downloadInfo));
                        }
                        break;

                    case ResourceDownloadType.Aria2c:
                        b = DownloadWebClient(type, downloadInfo);
                        if (b)
                        {
                            return(Unzip(type, downloadInfo));
                        }
                        break;
                    }
                }
                else
                {
                    return(true);
                }
            }
            catch (Exception x)
            {
            }
            return(false);
        }
Example #3
0
        public static bool DownloadWebClient(ResourceDownloadType type, Action <int, string, string> onStdoutReadLine)
        {
            var request = (HttpWebRequest)WebRequest.Create(urls[type]);

            request.AutomaticDecompression = DecompressionMethods.GZip;
            using (var filestream = new FileStream(zipNames[type], FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                if (filestream.Length > 0)
                {
                    request.AddRange(filestream.Length);
                }
                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    float length = response.ContentLength;
                    using (var stream = response.GetResponseStream())
                    {
                        float    speed  = 0f;
                        int      aspd   = 0;
                        DateTime dt     = DateTime.UtcNow;
                        byte[]   buffer = new byte[1024];// HTTP_Stream_Server.MEGABYTE];
                        while (true)
                        {
                            int r = stream.Read(buffer, 0, buffer.Length);
                            if (r <= 0)
                            {
                                break;
                            }
                            aspd += r;
                            filestream.Write(buffer, 0, r);
                            if (DateTime.UtcNow.Subtract(dt).TotalSeconds >= 1.0)
                            {
                                speed = aspd / 1024f;
                                aspd  = 0;

                                onStdoutReadLine?.Invoke((int)((filestream.Position / length) * 100),
                                                         speed.ToString("F1") + "kiB/s", zipNames[type]);

                                dt = DateTime.UtcNow;
                            }
                        }
                        onStdoutReadLine?.Invoke(-1, "", "");
                        return(filestream.Length > LiXMath.MIBIBYTE && filestream.Length >= length);
                    }
                }
            }
        }
Example #4
0
        public static bool Unzip(ResourceDownloadType type, Action <int, string, string> onStdoutReadLine, params string[] names)
        {
            try
            {
                if (zipNames[type].EndsWith("exe"))
                {
                    return(true);
                }
                using (Process p = new Process())
                {
                    p.StartInfo.Arguments              = "e -y -r " + zipNames[type] + " " + string.Join(" ", fileNames[type]);
                    p.StartInfo.FileName               = "7za.exe";
                    p.StartInfo.WorkingDirectory       = Path.GetFullPath(".\\");
                    p.StartInfo.CreateNoWindow         = true;
                    p.StartInfo.UseShellExecute        = false;
                    p.StartInfo.RedirectStandardOutput = true;
                    p.Start();

                    while (!p.StandardOutput.EndOfStream)
                    {
                        var ps = p.StandardOutput.ReadLine();
                        if (!string.IsNullOrWhiteSpace(ps))
                        {
                            ps = ps.Trim();
                            //int.TryParse(ps.GetBetweenN("(", "%)"), out int perc);
                            onStdoutReadLine?.Invoke(100, ps, string.Join(",", fileNames[type]));
                        }
                    }
                    onStdoutReadLine?.Invoke(-1, "", "");
                    if (names.Count(t => File.Exists(t)) == names.Length)
                    {
                        File.Delete(zipNames[type]);
                        return(true);
                    }
                    return(false);
                }
            }
            catch
            {
            }
            return(false);
        }
Example #5
0
 private static bool Exists(ResourceDownloadType type)
 {
     return(File.Exists(Path.Combine(Path.GetFullPath(".\\"), fileNames[type][0])));
 }