Skip to content

Commit

Permalink
Merge pull request #86 from coaku/b_xzk_fix_resumable_upload_bug
Browse files Browse the repository at this point in the history
bug fix
  • Loading branch information
xushiwei committed Dec 23, 2013
2 parents 3506dff + 906956e commit 240ec68
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
4 changes: 3 additions & 1 deletion qiniu/auth/up.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

class Client(rpc.Client):
up_token = None

def __init__(self, up_token, host=None):
if host is None:
host = conf.UP_HOST
if host.startswith("http://"):
host = host[7:]
self.up_token = up_token
super(Client, self).__init__(host)

Expand Down
15 changes: 9 additions & 6 deletions qiniu/resumable_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,14 @@ def put(uptoken, key, f, fsize, extra):
if extra.chunk_size is None:
extra.chunk_size = _chunk_size

client = auth.up.Client(uptoken)
for i in xrange(0, block_cnt):
try_time = extra.try_times
read_length = _block_size
if (i+1)*_block_size > fsize:
read_length = fsize - i*_block_size
data_slice = f.read(read_length)
while True:
err = resumable_block_put(client, data_slice, i, extra)
err = resumable_block_put(data_slice, i, extra, uptoken)
if err is None:
break

Expand All @@ -101,34 +100,38 @@ def put(uptoken, key, f, fsize, extra):
return None, err_put_failed
print err, ".. retry"

return mkfile(client, key, fsize, extra)
mkfile_client = auth.up.Client(uptoken, extra.progresses[-1]["host"])
return mkfile(mkfile_client, key, fsize, extra)

# ----------------------------------------------------------

def resumable_block_put(client, block, index, extra):
def resumable_block_put(block, index, extra, uptoken):
block_size = len(block)

mkblk_client = auth.up.Client(uptoken, conf.UP_HOST)
if extra.progresses[index] is None or "ctx" not in extra.progresses[index]:
end_pos = extra.chunk_size-1
if block_size < extra.chunk_size:
end_pos = block_size-1
chunk = block[: end_pos]
crc32 = gen_crc32(chunk)
chunk = bytearray(chunk)
extra.progresses[index], err = mkblock(client, block_size, chunk)
extra.progresses[index], err = mkblock(mkblk_client, block_size, chunk)
if not extra.progresses[index]["crc32"] == crc32:
return err_unmatched_checksum
if err is not None:
extra.notify_err(index, end_pos + 1, err)
return err
extra.notify(index, end_pos + 1, extra.progresses[index])

bput_client = auth.up.Client(uptoken, extra.progresses[index]["host"])
while extra.progresses[index]["offset"] < block_size:
offset = extra.progresses[index]["offset"]
chunk = block[offset: offset+extra.chunk_size-1]
crc32 = gen_crc32(chunk)
chunk = bytearray(chunk)
extra.progresses[index], err = putblock(client, extra.progresses[index], chunk)

extra.progresses[index], err = putblock(bput_client, extra.progresses[index], chunk)
if not extra.progresses[index]["crc32"] == crc32:
return err_unmatched_checksum
if err is not None:
Expand Down

0 comments on commit 240ec68

Please sign in to comment.