private BlkputRet bput(WebClient wc, string url, byte[] bdata)
 {
     for (int i = 0; i < RETRYTIMES; i++)
     {
         try{
             byte[]    rawBytes = wc.UploadData(url, bdata);
             BlkputRet putRet   = JsonConvert.DeserializeObject <BlkputRet>(Encoding.UTF8.GetString(rawBytes));
             if (CRC32.CheckSumBytes(bdata) != putRet.crc32)
             {
                 if (i == (RETRYTIMES - 1))
                 {
                     throw new Exception("crc32 check error");
                 }
             }
             else
             {
                 return(putRet);
             }
         }catch (Exception e) {
             if (i == (RETRYTIMES - 1))
             {
                 throw e;
             }
         }
     }
     return(null);
 }
Example #2
0
        private void makeBlock(string upHost, long offset, int blockSize, int chunkSize,
                               ProgressHandler progressHandler, CompletionHandler completionHandler)
        {
            string url = string.Format("{0}/mkblk/{1}", upHost, blockSize);

            try
            {
                this.fileStream.Read(this.chunkBuffer, 0, chunkSize);
            }
            catch (Exception ex)
            {
                this.upCompletionHandler(this.key, ResponseInfo.fileError(ex), "");
                return;
            }
            this.crc32 = CRC32.CheckSumBytes(this.chunkBuffer, chunkSize);
            post(url, this.chunkBuffer, chunkSize, progressHandler, completionHandler);
        }
Example #3
0
        private void putChunk(string upHost, long offset, int chunkSize, string context,
                              ProgressHandler progressHandler, CompletionHandler completionHandler)
        {
            int    chunkOffset = (int)(offset % Config.BLOCK_SIZE);
            string url         = string.Format("{0}/bput/{1}/{2}", upHost, context, chunkOffset);

            try
            {
                this.fileStream.Read(this.chunkBuffer, 0, chunkSize);
            }
            catch (Exception ex)
            {
                this.upCompletionHandler(this.key, ResponseInfo.fileError(ex), "");
                return;
            }
            this.crc32 = CRC32.CheckSumBytes(this.chunkBuffer, chunkSize);
            post(url, this.chunkBuffer, chunkSize, progressHandler, completionHandler);
        }
Example #4
0
        private async Task <BlkputRet> ResumableBlockPut(Client client,
                                                         byte[] body, int blkIdex,
                                                         int blkSize)
        {
            #region Mkblock

            uint crc32 = CRC32.CheckSumBytes(body, blkSize);
            for (int i = 0; i < PutSetting.TryTimes; i++)
            {
                try
                {
                    Extra.Progresses[blkIdex] = await Mkblock(client, body, blkSize);
                }
                catch (Exception ee)
                {
                    if (i == PutSetting.TryTimes - 1)
                    {
                        throw;
                    }
                    await Task.Delay(1000);

                    //System.Threading.Thread.Sleep(1000);
                    continue;
                }
                if ((Extra.Progresses[blkIdex] == null) || (crc32 != Extra.Progresses[blkIdex].crc32))
                {
                    if (i == PutSetting.TryTimes - 1)
                    {
                        return(null);
                    }
                    await Task.Delay(1000);

                    //System.Threading.Thread.Sleep(1000);
                }
                else
                {
                    break;
                }
            }

            #endregion

            return(Extra.Progresses[blkIdex]);
        }
Example #5
0
        private async Task <BlkputRet> ResumableBlockPutAsync(Client client, byte[] body, int blkIdex, int blkSize)
        {
            #region Mkblock
            uint crc32 = CRC32.CheckSumBytes(body, blkSize);
            for (int i = 0; i < putSetting.TryTimes; i++)
            {
                try
                {
                    extra.Progresses[blkIdex] = await MkblockAsync(client, body, blkSize);
                }
                catch (Exception ee)
                {
                    if (i == (putSetting.TryTimes - 1))
                    {
                        throw ee;
                    }
                    await Task.Delay(1000);

                    continue;
                }
                if (extra.Progresses[blkIdex] == null || crc32 != extra.Progresses[blkIdex].crc32)
                {
                    if (i == (putSetting.TryTimes - 1))
                    {
                        return(null);
                    }
                    await Task.Delay(1000);

                    continue;
                }
                else
                {
                    break;
                }
            }
            #endregion

            return(extra.Progresses[blkIdex]);
        }
Example #6
0
        private BlkputRet ResumableBlockPut(Client client, byte[] body, int blkIdex, int blkSize)
        {
            #region Mkblock
            uint crc32 = CRC32.CheckSumBytes(body, blkSize);
            for (int i = 0; i < putSetting.TryTimes; i++)
            {
                try
                {
                    extra.Progresses[blkIdex] = Mkblock(client, body, blkSize);
                }
                catch (Exception ee)
                {
                    if (i == (putSetting.TryTimes - 1))
                    {
                        throw ee;
                    }
                    System.Threading.Thread.Sleep(1000);
                    continue;
                }
                if (extra.Progresses[blkIdex] == null || crc32 != extra.Progresses[blkIdex].crc32)
                {
                    if (i == (putSetting.TryTimes - 1))
                    {
                        return(null);
                    }
                    System.Threading.Thread.Sleep(1000);
                    continue;
                }
                else
                {
                    break;
                }
            }
            #endregion

            return(extra.Progresses[blkIdex]);
        }
Example #7
0
        /// <summary>
        /// 上传数据流
        /// </summary>
        /// <param name="stream">(确定长度的)数据流</param>
        /// <param name="key">要保存的key</param>
        /// <param name="token">上传凭证</param>
        /// <param name="putExtra">上传可选设置</param>
        /// <returns>上传数据流后的返回结果</returns>
        public HttpResult UploadStream(Stream stream, string key, string token, PutExtra putExtra)
        {
            if (putExtra == null)
            {
                putExtra = new PutExtra();
            }
            if (string.IsNullOrEmpty(putExtra.MimeType))
            {
                putExtra.MimeType = "application/octet-stream";
            }
            if (putExtra.ProgressHandler == null)
            {
                putExtra.ProgressHandler = DefaultUploadProgressHandler;
            }
            if (putExtra.UploadController == null)
            {
                putExtra.UploadController = DefaultUploadController;
            }
            string fname = key;

            if (string.IsNullOrEmpty(key))
            {
                fname = "fname_temp";
            }

            HttpResult result = new HttpResult();

            try
            {
                string        boundary    = HttpManager.CreateFormDataBoundary();
                StringBuilder bodyBuilder = new StringBuilder();
                bodyBuilder.AppendLine("--" + boundary);

                if (key != null)
                {
                    //write key when it is not null
                    bodyBuilder.AppendLine("Content-Disposition: form-data; name=\"key\"");
                    bodyBuilder.AppendLine();
                    bodyBuilder.AppendLine(key);
                    bodyBuilder.AppendLine("--" + boundary);
                }

                //write token
                bodyBuilder.AppendLine("Content-Disposition: form-data; name=\"token\"");
                bodyBuilder.AppendLine();
                bodyBuilder.AppendLine(token);
                bodyBuilder.AppendLine("--" + boundary);

                //write extra params
                if (putExtra.Params != null && putExtra.Params.Count > 0)
                {
                    foreach (var p in putExtra.Params)
                    {
                        if (p.Key.StartsWith("x:"))
                        {
                            bodyBuilder.AppendFormat("Content-Disposition: form-data; name=\"{0}\"", p.Key);
                            bodyBuilder.AppendLine();
                            bodyBuilder.AppendLine();
                            bodyBuilder.AppendLine(p.Value);
                            bodyBuilder.AppendLine("--" + boundary);
                        }
                    }
                }

                //prepare data buffer
                int    bufferSize = 1024 * 1024;
                byte[] buffer     = new byte[bufferSize];
                int    bytesRead  = 0;
                putExtra.ProgressHandler(0, stream.Length);
                MemoryStream dataMS = new MemoryStream();
                while ((bytesRead = stream.Read(buffer, 0, bufferSize)) != 0)
                {
                    dataMS.Write(buffer, 0, bytesRead);
                }

                //write crc32
                uint crc32 = CRC32.CheckSumBytes(dataMS.ToArray());
                //write key when it is not null
                bodyBuilder.AppendLine("Content-Disposition: form-data; name=\"crc32\"");
                bodyBuilder.AppendLine();
                bodyBuilder.AppendLine(crc32.ToString());
                bodyBuilder.AppendLine("--" + boundary);

                //write fname
                bodyBuilder.AppendFormat("Content-Disposition: form-data; name=\"file\"; filename=\"{0}\"", fname);
                bodyBuilder.AppendLine();

                //write mime type
                bodyBuilder.AppendFormat("Content-Type: {0}", putExtra.MimeType);
                bodyBuilder.AppendLine();
                bodyBuilder.AppendLine();

                //write file data
                StringBuilder bodyEnd = new StringBuilder();
                bodyEnd.AppendLine();
                bodyEnd.AppendLine("--" + boundary + "--");

                byte[] partData1 = Encoding.UTF8.GetBytes(bodyBuilder.ToString());
                byte[] partData2 = dataMS.ToArray();
                byte[] partData3 = Encoding.UTF8.GetBytes(bodyEnd.ToString());

                MemoryStream ms = new MemoryStream();
                ms.Write(partData1, 0, partData1.Length);
                ms.Write(partData2, 0, partData2.Length);
                ms.Write(partData3, 0, partData3.Length);

                //get upload host
                string ak     = UpToken.GetAccessKeyFromUpToken(token);
                string bucket = UpToken.GetBucketFromUpToken(token);
                if (ak == null || bucket == null)
                {
                    return(HttpResult.InvalidToken);
                }

                string uploadHost = this.config.UpHost(ak, bucket);
                putExtra.ProgressHandler(stream.Length / 5, stream.Length);
                result = httpManager.PostMultipart(uploadHost, ms.ToArray(), boundary, null);
                putExtra.ProgressHandler(stream.Length, stream.Length);
                if (result.Code == (int)HttpCode.OK)
                {
                    result.RefText += string.Format("[{0}] [FormUpload] Uploaded: #STREAM# ==> \"{1}\"\n",
                                                    DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff"), key);
                }
                else
                {
                    result.RefText += string.Format("[{0}] [FormUpload] Failed: code = {1}, text = {2}\n",
                                                    DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff"), result.Code, result.Text);
                }

                //close memory stream
                ms.Close();
                dataMS.Close();
            }
            catch (Exception ex)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("[{0}] [FormUpload] Error: ", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff"));
                Exception e = ex;
                while (e != null)
                {
                    sb.Append(e.Message + " ");
                    e = e.InnerException;
                }
                sb.AppendLine();

                if (ex is QiniuException)
                {
                    QiniuException qex = (QiniuException)ex;
                    result.Code     = qex.HttpResult.Code;
                    result.RefCode  = qex.HttpResult.Code;
                    result.Text     = qex.HttpResult.Text;
                    result.RefText += sb.ToString();
                }
                else
                {
                    result.RefCode  = (int)HttpCode.USER_UNDEF;
                    result.RefText += sb.ToString();
                }
            }
            finally
            {
                if (stream != null)
                {
                    try
                    {
                        stream.Close();
                        stream.Dispose();
                    }
                    catch (Exception) { }
                }
            }

            return(result);
        }
Example #8
0
        private BlkputRet ResumableBlockPut(Client client, byte[] body, int blkIdex, int blkSize)
        {
            int bodyLength;
            int chunkSize = extra.chunkSize;

            #region Mkblock
            if (extra.Progresses [blkIdex] == null)
            {
                bodyLength = chunkSize < blkSize ? chunkSize : blkSize;
                byte[] firstChunk = new byte[bodyLength];
                Array.Copy(body, 0, firstChunk, 0, bodyLength);
                uint crc32 = CRC32.CheckSumBytes(firstChunk);
                for (int i = 0; i < putSetting.TryTimes; i++)
                {
                    extra.Progresses [blkIdex] = Mkblock(client, firstChunk, body.Length);
                    if (extra.Progresses [blkIdex] == null || crc32 != extra.Progresses [blkIdex].crc32)
                    {
                        if (i == (putSetting.TryTimes - 1))
                        {
                            return(null);
                        }
                        continue;
                    }
                    else
                    {
                        progress();
                        break;
                    }
                }
            }
            #endregion
            #region PutBlock
            while (extra.Progresses[blkIdex].offset < blkSize)
            {
                bodyLength = (chunkSize < (blkSize - extra.Progresses [blkIdex].offset)) ? chunkSize : (int)(blkSize - extra.Progresses [blkIdex].offset);
                byte[] chunk = new byte[bodyLength];
                Array.Copy(body, extra.Progresses [blkIdex].offset, chunk, 0, bodyLength);
                for (int i = 0; i < putSetting.TryTimes; i++)
                {
                    extra.Progresses [blkIdex] = BlockPut(client, extra.Progresses [blkIdex], new MemoryStream(chunk), bodyLength);
                    if (extra.Progresses [blkIdex] == null)
                    {
                        if (i == (putSetting.TryTimes - 1))
                        {
                            return(null);
                        }
                        continue;
                    }
                    else
                    {
                        uploadedChunks++;
                        if (Progress != null)
                        {
                            Progress((float)uploadedChunks / chunks);
                        }
                        break;
                    }
                }
            }
            #endregion
            return(extra.Progresses [blkIdex]);
        }
        private void upload(HttpFormFile fFile, string key, string token,
                            UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            if (uploadOptions == null)
            {
                uploadOptions = UploadOptions.defaultOptions();
            }
            Dictionary <string, string> vPostParams = new Dictionary <string, string>();

            //设置key
            if (!string.IsNullOrEmpty(key))
            {
                vPostParams.Add("key", key);
            }
            //设置token
            vPostParams.Add("token", token);
            //设置crc32校验
            if (uploadOptions.CheckCrc32)
            {
                switch (fFile.BodyType)
                {
                case HttpFileType.DATA_SLICE:
                    vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumSlice(fFile.BodyBytes, fFile.Offset, fFile.Count)));
                    break;

                case HttpFileType.DATA_BYTES:
                    vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumBytes(fFile.BodyBytes)));
                    break;

                case HttpFileType.FILE_STREAM:
                    long   streamLength = fFile.BodyStream.Length;
                    byte[] buffer       = new byte[streamLength];
                    int    cnt          = fFile.BodyStream.Read(buffer, 0, (int)streamLength);
                    vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumSlice(buffer, 0, cnt)));
                    fFile.BodyStream.Seek(0, SeekOrigin.Begin);
                    break;

                case HttpFileType.FILE_PATH:
                    vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumFile(fFile.BodyFile)));
                    break;
                }
            }

            //设置MimeType
            // FIX: (添加了下一行代码)
            // 修正上传文件MIME总为octect-stream(原因:未初始化FormFile.ContentType)的问题
            // @fengyh 2016-08-17 14:50
            fFile.ContentType = uploadOptions.MimeType;
            //设置扩展参数
            foreach (KeyValuePair <string, string> kvp in uploadOptions.ExtraParams)
            {
                vPostParams.Add(kvp.Key, kvp.Value);
            }
            //设置进度处理和取消信号
            ProgressHandler fUpProgressHandler = new ProgressHandler(delegate(long bytesWritten, long totalBytes)
            {
                double percent = (double)bytesWritten / totalBytes;
                //这样做是为了等待回复
                if (percent > 0.95)
                {
                    percent = 0.95;
                }
                uploadOptions.ProgressHandler(key, percent);
            });

            CancellationSignal fCancelSignal = new CancellationSignal(delegate()
            {
                return(uploadOptions.CancellationSignal());
            });


            //第一次失败后使用备用域名重试一次
            CompletionHandler fUpCompletionHandler = new CompletionHandler(delegate(ResponseInfo respInfo, string response)
            {
                Console.WriteLine("form upload result, {0}", respInfo.StatusCode);
                if (respInfo.needRetry())
                {
                    if (fFile.BodyStream != null)
                    {
                        fFile.BodyStream.Seek(0, SeekOrigin.Begin);
                    }

                    CompletionHandler retried = new CompletionHandler(delegate(ResponseInfo retryRespInfo, string retryResponse)
                    {
                        Console.WriteLine("form upload retry result, {0}", retryRespInfo.StatusCode);
                        if (respInfo.isOk())
                        {
                            uploadOptions.ProgressHandler(key, 1.0);
                        }

                        if (fFile.BodyStream != null)
                        {
                            fFile.BodyStream.Close();
                        }

                        if (upCompletionHandler != null)
                        {
                            try
                            {
                                upCompletionHandler(key, retryRespInfo, retryResponse);
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("form upload retry completion error, {0}", ex.Message);
                            }
                        }
                    });


                    this.mHttpManager.postMultipartDataForm(Config.ZONE.UploadHost, null, vPostParams, fFile, fUpProgressHandler, retried);
                }
                else
                {
                    if (respInfo.isOk())
                    {
                        uploadOptions.ProgressHandler(key, 1.0);
                    }

                    if (fFile.BodyStream != null)
                    {
                        fFile.BodyStream.Close();
                    }

                    if (upCompletionHandler != null)
                    {
                        try
                        {
                            upCompletionHandler(key, respInfo, response);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("form upload completion error, {0}", ex.Message);
                        }
                    }
                }
            });

            this.mHttpManager.postMultipartDataForm(Config.ZONE.UploadHost, null, vPostParams, fFile, fUpProgressHandler, fUpCompletionHandler);
        }
Example #10
0
        private void upload(HttpManager httpManager, PostArgs postArgs, string key, string token,
                            UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            if (uploadOptions == null)
            {
                uploadOptions = UploadOptions.defaultOptions();
            }
            postArgs.Params = new Dictionary <string, string>();
            //设置key
            if (!string.IsNullOrEmpty(key))
            {
                postArgs.Params.Add("key", key);
            }
            //设置token
            postArgs.Params.Add("token", token);
            //设置crc32校验
            if (uploadOptions.CheckCrc32)
            {
                switch (httpManager.FileContentType)
                {
                case PostContentType.BYTES:
                    postArgs.Params.Add("crc32", string.Format("{0}", CRC32.CheckSumBytes(postArgs.Data, postArgs.Data.Length)));
                    break;

                case PostContentType.STREAM:
                    long   streamLength = postArgs.Stream.Length;
                    byte[] buffer       = new byte[streamLength];
                    int    cnt          = postArgs.Stream.Read(buffer, 0, (int)streamLength);
                    postArgs.Params.Add("crc32", string.Format("{0}", CRC32.CheckSumBytes(buffer, cnt)));
                    postArgs.Stream.Seek(0, SeekOrigin.Begin);
                    break;

                case PostContentType.FILE:
                    postArgs.Params.Add("crc32", string.Format("{0}", CRC32.CheckSumFile(postArgs.File)));
                    break;
                }
            }

            //设置MimeType
            postArgs.MimeType = uploadOptions.MimeType;
            //设置扩展参数
            foreach (KeyValuePair <string, string> kvp in uploadOptions.ExtraParams)
            {
                postArgs.Params.Add(kvp.Key, kvp.Value);
            }
            //设置进度处理和取消信号
            httpManager.ProgressHandler = new ProgressHandler(delegate(int bytesWritten, int totalBytes)
            {
                double percent = (double)bytesWritten / totalBytes;
                //这样做是为了等待回复
                if (percent > 0.95)
                {
                    percent = 0.95;
                }
                uploadOptions.ProgressHandler(key, percent);
            });

            httpManager.CancellationSignal = new CancellationSignal(delegate()
            {
                return(uploadOptions.CancellationSignal());
            });
            httpManager.PostArgs = postArgs;
            //第一次失败后使用备用域名重试一次
            httpManager.CompletionHandler = new CompletionHandler(delegate(ResponseInfo respInfo, string response)
            {
                if (respInfo.needRetry())
                {
                    if (httpManager.PostArgs.Stream != null)
                    {
                        httpManager.PostArgs.Stream.Seek(0, SeekOrigin.Begin);
                    }
                    CompletionHandler retried = new CompletionHandler(delegate(ResponseInfo retryRespInfo, string retryResponse)
                    {
                        uploadOptions.ProgressHandler(key, 1.0);

                        if (httpManager.PostArgs.Stream != null)
                        {
                            httpManager.PostArgs.Stream.Close();
                        }

                        if (upCompletionHandler != null)
                        {
                            upCompletionHandler(key, retryRespInfo, retryResponse);
                        }
                    });
                    httpManager.CompletionHandler = retried;
                    httpManager.multipartPost(Config.UP_HOST);
                }
                else
                {
                    uploadOptions.ProgressHandler(key, 1.0);

                    if (httpManager.PostArgs.Stream != null)
                    {
                        httpManager.PostArgs.Stream.Close();
                    }

                    if (upCompletionHandler != null)
                    {
                        upCompletionHandler(key, respInfo, response);
                    }
                }
            });
            httpManager.multipartPost(Config.UPLOAD_HOST);
        }
        private upInfo readyUp(string upToken, Stream stream, string saveKey, string mimeType = null, bool crc32 = false, NameValueCollection xVars = null, bool isFile = true)
        {
            if (uping)
            {
                throw new Exception("upload busying");
            }
            if (string.IsNullOrEmpty(upToken))
            {
                throw new Exception("bad upToken");
            }


            StringBuilder urlBuilder = new StringBuilder();

            urlBuilder.Append(QiniuHosts.UP_HOST);

            //Fsize

            urlBuilder.AppendFormat("/put/{0}", stream.Length);

            //key
            if (string.IsNullOrEmpty(saveKey))
            {
                urlBuilder.AppendFormat("/key/{0}", saveKey.ToBase64URLSafe());
            }

            //mimetype
            if (!string.IsNullOrEmpty(mimeType))
            {
                urlBuilder.AppendFormat("/mimeType/{0}", mimeType);
            }

            byte[] buffer = new byte[stream.Length];
            int    n      = stream.Read(buffer, 0, (int)stream.Length);

            //crc32
            if (crc32)
            {
                urlBuilder.AppendFormat("/crc32/{0}", CRC32.CheckSumBytes(buffer));
            }

            //x-Vars
            if (xVars != null && xVars.Count > 0)
            {
                foreach (string key in xVars.AllKeys)
                {
                    if (!key.StartsWith("x:"))
                    {
                        throw new Exception("user var name must begin with \"x:\"");
                    }
                    urlBuilder.Append(string.Format("/{0}/{1}", key, xVars [key].ToBase64URLSafe()));
                }
            }


            string url = urlBuilder.ToString();

            return(new upInfo {
                upUrl = url, data = buffer
            });
        }