Beispiel #1
0
 public static Result UploadBlock(FileProperty prop, string path, InitUploadResult session, FileStream stream, int blockid, string host, Credential credential)
 {
     try
     {
         using (var wc = new PatientWebClient())
         {
             var boundary = GetBoundary();
             wc.Headers.Add(HttpRequestHeader.Cookie, credential);
             wc.Headers.Add(HttpRequestHeader.ContentType, "multipart/form-data; boundary=" + boundary);
             var str  = "--" + boundary + "\r\nContent-Disposition: form-data; name=\"filename\"; filename=\"name\"\r\nContent-Type: application/octet-stream\r\n\r\n";
             var str2 = "\r\n--" + boundary + "--\r\n";
             stream.Seek((long)blockid * 4 * 1024 * 1024, SeekOrigin.Begin);
             var fdata = new byte[4 * 1024 * 1024];
             var len   = stream.Read(fdata, 0, fdata.Length);
             if (len < fdata.Length)
             {
                 var arr = new byte[len];
                 Array.Copy(fdata, arr, len);
                 fdata = arr;
             }
             var data = Encoding.UTF8.GetBytes(str).Concat(fdata).Concat(Encoding.UTF8.GetBytes(str2)).ToArray();
             var res  = wc.UploadData("http://" + host + "/rest/2.0/pcs/superfile2?app_id=250528&method=upload&path=" + Uri.EscapeDataString(path) + "&uploadid=" + Uri.EscapeDataString(session.uploadid) + "&partseq=" + blockid + "&partoffset=" + (long)blockid * 4 * 1024 * 1024, data);
             var obj  = JsonConvert.DeserializeObject <SuperFileResponse>(Encoding.UTF8.GetString(res));
             if (obj.md5 != prop.blocks[blockid])
             {
                 throw new Exception("MD5 mismatch.");
             }
             return(new Result()
             {
                 success = true
             });
         }
     }
     catch (Exception ex)
     {
         return(new Result()
         {
             exception = ex
         });
     }
 }
Beispiel #2
0
 public static CommitUploadResult CommitUpload(FileProperty prop, string path, InitUploadResult session, Credential credential)
 {
     try
     {
         using (var wc = new WebClient())
         {
             wc.Headers.Add(HttpRequestHeader.Cookie, credential);
             wc.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
             var str = "path=" + path + "&size=" + prop.size + "&isdir=0&uploadid=" + Uri.EscapeDataString(session.uploadid) + "&block_list=[" + string.Join(",", prop.blocks.Select(h => '"' + h + '"')) + "]&method=post&rtype=2&sequence=1&mode=1&local_mtime=" + prop.mtime;
             var res = wc.UploadData("http://pan.baidu.com/api/create?a=commit&clienttype=8", Encoding.UTF8.GetBytes(str));
             var obj = JsonConvert.DeserializeObject <CommitUploadResult>(Encoding.UTF8.GetString(res));
             obj.success = true;
             return(obj);
         }
     }
     catch (Exception ex)
     {
         return(new CommitUploadResult()
         {
             exception = ex
         });
     }
 }