Skip to content

Commit

Permalink
Fix TUS upload with zero length files
Browse files Browse the repository at this point in the history
  • Loading branch information
vangheem committed Mar 20, 2018
1 parent 0433daa commit 2498012
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.rst
@@ -1,7 +1,8 @@
2.5.5 (unreleased)
------------------

- Nothing changed yet.
- Fix TUS upload with zero length files
[vangheem]


2.5.4 (2018-03-19)
Expand Down
2 changes: 1 addition & 1 deletion guillotina/files/manager.py
Expand Up @@ -133,7 +133,7 @@ async def tus_patch(self, *args, **kwargs):
'Tus-Upload-Finished'])
}

if self.dm.get('size') and self.dm.get_offset() >= self.dm.get('size'):
if self.dm.get('size') is not None and self.dm.get_offset() >= self.dm.get('size'):
await self.file_storage_manager.finish(self.dm)
await self.dm.finish()
headers['Tus-Upload-Finished'] = '1'
Expand Down
62 changes: 62 additions & 0 deletions guillotina/tests/test_attachment.py
Expand Up @@ -310,3 +310,65 @@ async def test_tus_unfinished_error(container_requester):
)
# override it
assert status == 201


async def test_tus_with_empty_file(container_requester):
async with container_requester as requester:
response, status = await requester(
'POST',
'/db/guillotina/',
data=json.dumps({
'@type': 'Item',
'@behaviors': ['guillotina.behaviors.attachment.IAttachment'],
'id': 'foobar'
})
)
assert status == 201

response, status = await requester(
'OPTIONS',
'/db/guillotina/foobar/@tusupload/file')
assert status == 200

response, status = await requester(
'POST',
'/db/guillotina/foobar/@tusupload/file',
headers={
'UPLOAD-LENGTH': '0',
'TUS-RESUMABLE': '1.0.0'
}
)
assert status == 201

response, status = await requester(
'HEAD',
'/db/guillotina/foobar/@tusupload/file')
assert status == 200

response, status = await requester(
'PATCH',
'/db/guillotina/foobar/@tusupload/file',
headers={
'CONTENT-LENGTH': '0',
'TUS-RESUMABLE': '1.0.0',
'upload-offset': '0'
},
data=b''
)
assert status == 200

response, status = await requester(
'GET',
'/db/guillotina/foobar/@download/file'
)
assert status == 200
assert len(response) == 0

request = utils.get_mocked_request(requester.db)
root = await utils.get_root(request)
async with managed_transaction(request=request, abort_when_done=True):
container = await root.async_get('guillotina')
obj = await container.async_get('foobar')
behavior = IAttachment(obj)
await behavior.load()
assert behavior.file._blob.chunks == 0

0 comments on commit 2498012

Please sign in to comment.