Example #1
0
        public ResultStatus Download(string fname)
        {
            ResultStatus r = this.Stat(fname);

            if (r.Result == ResultStatus.Statuses.Error)
            {
                return(r);
            }

            PyFile f = r.Data as PyFile;

            if (this.BeginFileProgress != null)
            {
                this.BeginFileProgress(this, new FileProgressEventArgs(fname, f.FileSize, 0));
            }

            this.WriteLine($"f=open('{fname}', 'rb')");
            this.ReadLine();

            string rec   = "";
            string res   = "";
            int    count = 0;

            do
            {
                this.WriteLine("f.read(32)");
                try
                {
                    rec = this.ReadLine();

                    if (rec != ">>> f.read(32)")
                    {
                        Thread.Sleep(50);
                        rec = this.ReadExisting();
                        return(new ResultStatus(ResultStatus.Statuses.Error, "Unknow line!\r\n" + rec));
                    }

                    // b''
                    rec  = this.ReadLine();
                    rec  = rec.Remove(0, 2);
                    rec  = rec.Remove(rec.Length - 1, 1);
                    res += rec;

                    // a little cheat :)
                    count += 32;
                    if (count > f.FileSize)
                    {
                        count = f.FileSize;
                    }

                    if (FileProgress != null)
                    {
                        this.FileProgress(this, new FileProgressEventArgs(fname, f.FileSize, count));
                    }
                }
                catch
                {
                    break;
                }
            } while (rec != "");

            this.WriteLine("f.close()");
            rec = this.ReadLine();
            byte[] buff = BinaryString.Text2Bin(res);
            if (this.EndFileProgress != null)
            {
                this.EndFileProgress(this, new FileProgressEventArgs(f.FileName, f.FileSize, buff.Length));
            }

            if (buff.Length == f.FileSize)
            {
                return(new ResultStatus(ResultStatus.Statuses.Success, buff));
            }

            return(new ResultStatus(ResultStatus.Statuses.Error, "File Sizes are differents!"));
        }