コード例 #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
         });
     }
 }
コード例 #2
0
        public static FileProperty GetFileProperty(string path)
        {
            var ret = new FileProperty()
            {
                path = path
            };
            var info = new FileInfo(path);

            ret.size  = info.Length;
            ret.mtime = (long)(info.LastAccessTime.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
            ret.md5   = GetMD5HashFromFile(path);
            using (var fs = new FileStream(path, FileMode.Open))
            {
                var md5 = new MD5CryptoServiceProvider();
                var arr = new byte[4 * 1024 * 1024];
                var len = fs.Read(arr, 0, 256 * 1024);
                ret.slice_md5 = ByteArrayToHexString(md5.ComputeHash(arr, 0, len));
                fs.Seek(0, SeekOrigin.Begin);
                var blocks = new List <string>();
                while (true)
                {
                    len = fs.Read(arr, 0, 4 * 1024 * 1024);
                    if (len <= 0)
                    {
                        break;
                    }
                    blocks.Add(ByteArrayToHexString(md5.ComputeHash(arr, 0, len)));
                }
                ret.blocks = blocks.ToArray();
                fs.Seek(0, SeekOrigin.Begin);
                var crc32 = new Crc32();
                ret.crc32 = string.Empty;
                foreach (byte b in crc32.ComputeHash(fs))
                {
                    ret.crc32 += b.ToString("x2").ToLower();
                }
            }
            return(ret);
        }
コード例 #3
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
         });
     }
 }
コード例 #4
0
 public static RapidUploadResult RapidUpload(FileProperty prop, string path, 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=" + Uri.EscapeDataString(path) + "&content-length=" + prop.size + "&content-md5=" + prop.md5 + "&slice-md5=" + prop.slice_md5 + "&content-crc32=" + prop.crc32 + "&local_mtime=" + prop.mtime + "&block_list=[" + string.Join(",", prop.blocks.Select(h => '"' + h + '"')) + "]&rtype=2";
             var res = wc.UploadData("http://pan.baidu.com/api/rapidupload?clienttype=8", Encoding.UTF8.GetBytes(str));
             var obj = JsonConvert.DeserializeObject <RapidUploadResult>(Encoding.UTF8.GetString(res));
             obj.success = true;
             return(obj);
         }
     }
     catch (Exception ex)
     {
         return(new RapidUploadResult()
         {
             exception = ex
         });
     }
 }