Exemplo n.º 1
0
        public static int DownloadBinary(
            string path,
            string file,
            string saveTo,
            string size,
            bool GUI = true)
        {
            long           num = 0;
            HttpWebRequest wr  = KiesRequest.Create("http://cloud-neofussvr.sslcs.cdngc.net/NF_DownloadBinaryForMass.do?file=" + path + file);

            wr.Method = "GET";
            wr.Headers["Authorization"] = Imports.GetAuthorization(Web.Nonce).Replace("Authorization: ", "").Replace("nonce=\"", "nonce=\"" + Web.Nonce);
            wr.Timeout          = 25000;
            wr.ReadWriteTimeout = 25000;
            if (System.IO.File.Exists(saveTo))
            {
                long length = new FileInfo(saveTo).Length;
                if (long.Parse(size) == length)
                {
                    Logger.WriteLog("File already downloaded.", false);
                    return(200);
                }
                Logger.WriteLog("File exists. Resuming download...", false);
                wr.AddRange((int)length);
                num = length;
            }
            using (HttpWebResponse responseFus = (HttpWebResponse)wr.GetResponseFUS())
            {
                if (responseFus == null)
                {
                    Logger.WriteLog("Error downloading: response is null", false);
                    return(901);
                }
                if (responseFus.StatusCode != HttpStatusCode.OK && responseFus.StatusCode != HttpStatusCode.PartialContent)
                {
                    Logger.WriteLog("Error downloading: " + (object)(int)responseFus.StatusCode, false);
                }
                else
                {
                    long total = long.Parse(responseFus.GetResponseHeader("content-length")) + num;
                    if (!System.IO.File.Exists(saveTo) || new FileInfo(saveTo).Length != total)
                    {
                        byte[]    buffer = new byte[8192];
                        Stopwatch sw     = new Stopwatch();
                        Utility.ResetSpeed(num);
                        try
                        {
                            Utility.PreventDeepSleep(Utility.PDSMode.Start);
                            using (BinaryWriter binaryWriter = new BinaryWriter((Stream) new FileStream(saveTo, FileMode.Append)))
                            {
                                int count;
                                do
                                {
                                    Utility.PreventDeepSleep(Utility.PDSMode.Continue);
                                    if (GUI)
                                    {
                                        if (Web.form.PauseDownload)
                                        {
                                            break;
                                        }
                                    }
                                    num += (long)(count = responseFus.GetResponseStream().Read(buffer, 0, buffer.Length));
                                    if (count > 0)
                                    {
                                        binaryWriter.Write(buffer, 0, count);
                                        if (GUI)
                                        {
                                            int dlspeed = Utility.DownloadSpeed(num, sw);
                                            if (dlspeed != -1)
                                            {
                                                Web.form.lbl_speed.Invoke((Delegate)((Action)(() => Web.form.lbl_speed.Text = dlspeed.ToString() + "kB/s")));
                                            }
                                        }
                                    }
                                    if (GUI)
                                    {
                                        Web.form.SetProgressBar(Utility.GetProgress(num, total));
                                    }
                                    else
                                    {
                                        CmdLine.SetProgress(Utility.GetProgress(num, total));
                                    }
                                }while (count > 0);
                            }
                        }
                        catch (IOException ex)
                        {
                            Logger.WriteLog("Error: Can't access output file " + saveTo, false);
                            if (GUI)
                            {
                                Web.form.PauseDownload = true;
                            }
                            Logger.WriteLog(ex.ToString(), false);
                            return(-1);
                        }
                        catch (WebException ex)
                        {
                            Logger.WriteLog("Error: Connection interrupted", false);
                            Web.SetReconnect();
                        }
                        finally
                        {
                            Utility.PreventDeepSleep(Utility.PDSMode.Stop);
                            if (GUI)
                            {
                                Web.form.lbl_speed.Invoke((Delegate)((Action)(() => Web.form.lbl_speed.Text = "0kB/s")));
                            }
                        }
                    }
                }
                return((int)responseFus.StatusCode);
            }
        }