Esempio n. 1
0
        private void btnOk_Click(object sender, EventArgs e)
        {
            string username = txUserName.Text.Trim();
            string password = txPassword.Text.Trim();

            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                MessageBox.Show("Please input User Name and Password");
                return;
            }
            btnOk.Enabled     = false;
            btnOk.Text        = "Login...";
            btnCancel.Enabled = false;
            PcsRes rc = pcs.login(username, password);

            if (rc != PcsRes.PCS_OK)
            {
                string errmsg = pcs.getError();
                MessageBox.Show("Failed to login: "******"Login";
                btnCancel.Enabled = true;
                return;
            }
            DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }
Esempio n. 2
0
        public virtual void Download()
        {
            if (Downloading)
            {
                throw new Exception("Can't download, since the previous download is not complete.");
            }
            FileStream stream = null;

            DoneSize    = 0;
            Success     = false;
            IsCancelled = false;
            Error       = null;
            Downloading = true;
            try
            {
                BaiduPCS pcs = this.pcs.clone();
                pcs.Write += onWrite;
                CreateDirectory(to);
                stream            = new FileStream(to, FileMode.Create, FileAccess.Write);
                pcs.WriteUserData = stream;
                fireThreadChanged(new ThreadCountChangedEventArgs(1, 1));
                PcsRes rc = pcs.download(from.path, 0, 0, 0);
                if (rc == PcsRes.PCS_OK)
                {
                    Success     = true;
                    IsCancelled = false;
                }
                else if (IsCancelled)
                {
                    Success     = false;
                    IsCancelled = true;
                }
                else
                {
                    Success     = false;
                    IsCancelled = false;
                    if (Error == null)
                    {
                        Error = new Exception(pcs.getError());
                    }
                }
            }
            catch (Exception ex)
            {
                Success     = false;
                IsCancelled = false;
                Error       = ex;
            }
            if (stream != null)
            {
                stream.Close();
            }
            Downloading = false;
            fireCompleted(new CompletedEventArgs(Success, IsCancelled, Error));
            fireThreadChanged(new ThreadCountChangedEventArgs(0, 1));
        }
Esempio n. 3
0
 public virtual void Upload()
 {
     if (Uploading)
     {
         throw new Exception("Can't upload, since the previous upload is not complete.");
     }
     DoneSize    = 0;
     Success     = false;
     IsCancelled = false;
     Error       = null;
     Uploading   = true;
     try
     {
         BaiduPCS pcs = this.pcs.clone();
         pcs.Progress += onProgress;
         fireThreadChanged(new ThreadCountChangedEventArgs(1, 1));
         Result = pcs.upload(to, from, IsOverWrite);
         if (!Result.IsEmpty)
         {
             Success     = true;
             IsCancelled = false;
         }
         else if (IsCancelled)
         {
             Success     = false;
             IsCancelled = true;
         }
         else
         {
             Success     = false;
             IsCancelled = false;
             if (Error == null)
             {
                 Error = new Exception(pcs.getError());
             }
         }
     }
     catch (Exception ex)
     {
         Success     = false;
         IsCancelled = false;
         Error       = ex;
     }
     Uploading = false;
     fireCompleted(new CompletedEventArgs(Success, IsCancelled, Error));
     fireThreadChanged(new ThreadCountChangedEventArgs(0, 1));
 }
Esempio n. 4
0
 public override void Upload()
 {
     if (Uploading)
     {
         throw new Exception("Can't upload, since the previous upload is not complete.");
     }
     DoneSize    = 0;
     Success     = false;
     IsCancelled = false;
     Error       = null;
     Uploading   = true;
     try
     {
         BaiduPCS pcs      = this.pcs.clone();
         string   filemd5  = null;
         string   slicemd5 = null;
         Result  = pcs.rapid_upload(to, from, ref filemd5, ref slicemd5, IsOverWrite);
         FileMD5 = filemd5;
         if (!Result.IsEmpty)
         {
             Success     = true;
             IsCancelled = false;
         }
         else if (IsCancelled)
         {
             Success     = false;
             IsCancelled = true;
         }
         else
         {
             Success     = false;
             IsCancelled = false;
             if (Error == null)
             {
                 Error = new Exception(pcs.getError());
             }
         }
     }
     catch (Exception ex)
     {
         Success     = false;
         IsCancelled = false;
         Error       = ex;
     }
     Uploading = false;
     fireCompleted(new CompletedEventArgs(Success, IsCancelled, Error));
 }
Esempio n. 5
0
 public override void Upload()
 {
     if (Uploading)
     {
         throw new Exception("Can't upload, since the previous upload is not complete.");
     }
     DoneSize      = 0;
     Success       = false;
     IsCancelled   = false;
     Error         = null;
     Uploading     = true;
     SliceFileName = null;
     SliceList     = null;
     fromFileInfo  = null;
     try
     {
         BaiduPCS pcs = this.pcs.clone();
         string   key;
         if (string.IsNullOrEmpty(FileMD5))
         {
             string validate_md5, validate2_md5;
             fromFileInfo = new FileInfo(from);
             if (!pcs.md5(from, 0, MIN_SLICE_SIZE, out validate_md5))
             {
                 throw new Exception("Can't calculate md5 for " + from + ".");
             }
             if (!pcs.md5(from, fromFileInfo.Length - MIN_SLICE_SIZE, MIN_SLICE_SIZE, out validate2_md5))
             {
                 throw new Exception("Can't calculate md5 for " + from + ".");
             }
             key = from + " => " + to + ", size=" + fromFileInfo.Length.ToString() + ", validate1_md5=" + validate_md5 + ", validate2_md5=" + validate2_md5;
             key = Utils.md5(key.ToLower());
         }
         else
         {
             key = FileMD5;
         }
         SliceFileName = "upload-" + key + ".slice";
         SliceFileName = Path.Combine(WorkFolder, pcs.getUID(), SliceFileName);
         StateFileNameDecideEventArgs args = new StateFileNameDecideEventArgs()
         {
             SliceFileName = SliceFileName
         };
         fireStateFileNameDecide(args);
         SliceFileName = args.SliceFileName;
         CreateOrRestoreSliceList(); // 创建或还原分片列表
         foreach (Slice slice in SliceList)
         {
             if (slice.status != SliceStatus.Successed)
             {
                 slice.status   = SliceStatus.Pending; //重新上传未成功的分片
                 slice.doneSize = 0;
             }
             else
             {
                 DoneSize += slice.doneSize;
             }
         }
         UploadSliceList();        // 启动线程来下载分片
         Wait();                   // 等待所有线程退出
         List <string> md5list = new List <string>();
         if (CheckResult(md5list)) // 检查下载结果
         {
             Result = pcs.create_superfile(to, md5list.ToArray(), IsOverWrite);
             if (!Result.IsEmpty)
             {
                 Success     = true;
                 IsCancelled = false;
             }
             else
             {
                 Success     = false;
                 IsCancelled = false;
                 Error       = new Exception(pcs.getError());
             }
         }
         else
         {
             Success = false;
         }
     }
     catch (Exception ex)
     {
         Success     = false;
         IsCancelled = false;
         Error       = ex;
     }
     if (Success)
     {
         SliceHelper.DeleteSliceFile(SliceFileName);
     }
     Uploading = false;
     fireCompleted(new CompletedEventArgs(Success, IsCancelled, Error));
 }