Exemplo n.º 1
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];
 }
Exemplo n.º 2
0
 private BlkputRet Mkblock(Client client, byte[] firstChunk, long blkSize)
 {
     string url = string.Format ("{0}/mkblk/{1}", Config.UP_HOST, blkSize);
     CallRet callRet = client.CallWithBinary (url, "application/octet-stream", new MemoryStream (firstChunk), firstChunk.Length);
     if (callRet.OK) {
         return callRet.Response.ToObject<BlkputRet> ();
     }
     return null;
 }
Exemplo n.º 3
0
 private CallRet Mkfile(Client client, string key, long fsize)
 {
     EntryPath entry = new EntryPath (extra.Bucket, key);
     string url = string.Format ("{0}/rs-mkfile/{1}/fsize/{2}/", Config.UP_HOST, entry.Base64EncodedURI, fsize);
     if (!string.IsNullOrEmpty (extra.MimeType)) {
         url += (string.Format ("/mimeType/{0}", extra.MimeType.ToBase64URLSafe ()));
     }
     if (!string.IsNullOrEmpty (extra.CustomMeta)) {
         url += (string.Format ("/meta/{0}", extra.CustomMeta.ToBase64URLSafe ()));
     }
     if (!string.IsNullOrEmpty (extra.CallbackParams)) {
         url += (string.Format ("/params/{0}", extra.CallbackParams.ToBase64URLSafe ()));
     }
     int proCount = extra.Progresses.Length;
     using (Stream body = new MemoryStream()) {
         for (int i = 0; i < proCount; i++) {
             byte[] bctx = Encoding.ASCII.GetBytes (extra.Progresses [i].ctx);
             body.Write (bctx, 0, bctx.Length);
             if (i != proCount - 1) {
                 body.WriteByte ((byte)',');
             }
         }
         body.Seek (0, SeekOrigin.Begin);
         return client.CallWithBinary (url, "text/plain", body, body.Length);
     }
 }
Exemplo n.º 4
0
 private BlkputRet BlockPut(Client client, BlkputRet ret, Stream body, long length)
 {
     string url = string.Format ("{0}/bput/{1}/{2}", Config.UP_HOST, ret.ctx, ret.offset);
     CallRet callRet = client.CallWithBinary (url, "application/octet-stream", body, length);
     if (callRet.OK) {
         return callRet.Response.ToObject<BlkputRet> ();
     }
     return null;
 }
Exemplo n.º 5
0
		private async System.Threading.Tasks.Task<CallRet> Mkfile(Client client, string key, long fsize)
		{
			StringBuilder urlBuilder = new StringBuilder();
			urlBuilder.AppendFormat("{0}/mkfile/{1}", Config.UP_HOST, fsize);
			if (key != null)
			{
				urlBuilder.AppendFormat("/key/{0}", Base64URLSafe.ToBase64URLSafe(key));
			}
			if (!string.IsNullOrEmpty(extra.MimeType))
			{
				urlBuilder.AppendFormat("/mimeType/{0}", Base64URLSafe.ToBase64URLSafe(extra.MimeType));
			}
			if (!string.IsNullOrEmpty(extra.CustomMeta))
			{
				urlBuilder.AppendFormat("/meta/{0}", Base64URLSafe.ToBase64URLSafe(extra.CustomMeta));
			}
			if (extra.CallbackParams != null && extra.CallbackParams.Count > 0)
			{
				StringBuilder sb = new StringBuilder();
				foreach (string _key in extra.CallbackParams.Keys)
				{
					sb.AppendFormat("/{0}/{1}", _key, Base64URLSafe.ToBase64URLSafe(extra.CallbackParams[_key]));
				}
				urlBuilder.Append(sb.ToString());
			}

			int proCount = extra.Progresses.Length;
			using (Stream body = new MemoryStream())
			{
				for (int i = 0; i < proCount; i++)
				{
					byte[] bctx = Encoding.ASCII.GetBytes(extra.Progresses[i].ctx);
					body.Write(bctx, 0, bctx.Length);
					if (i != proCount - 1)
					{
						body.WriteByte((byte)',');
					}
				}
				body.Seek(0, SeekOrigin.Begin);
				return await client.CallWithBinary(urlBuilder.ToString(), "text/plain", body, body.Length);
			}
		}
Exemplo n.º 6
0
		private async System.Threading.Tasks.Task<BlkputRet> Mkblock(Client client, byte[] firstChunk, int blkSize)
		{
			string url = string.Format("{0}/mkblk/{1}", Config.UP_HOST, blkSize);

			CallRet callRet = await client.CallWithBinary(url, "application/octet-stream", new MemoryStream(firstChunk, 0, blkSize), blkSize);
			if (callRet.OK)
			{
				return QiniuJsonHelper.ToObject<BlkputRet>(callRet.Response);
			}
			return null;
		}
Exemplo n.º 7
0
		private async System.Threading.Tasks.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 ee;
					}
					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);
					continue;
				}
				else
				{
					break;
				}
			}
			#endregion

			return extra.Progresses[blkIdex];
		}
Exemplo n.º 8
0
 private async Task<BlkputRet> Mkblock(Client client, byte[] firstChunk, int blkSize)
 {
     string url = string.Format("{0}/mkblk/{1}", Config.UP_HOST, blkSize);
     
     CallRet callRet =await client.CallWithBinary(url,new Windows.Web.Http.Headers.HttpMediaTypeHeaderValue("application/octet-stream"),new MemoryStream(firstChunk, 0, blkSize),blkSize);
     if (callRet.OK)
     {
         return QiniuJsonHelper.ToObject<BlkputRet>(callRet.Response);
     }
     return null;
 }