Skip to content

Commit

Permalink
Added check for free space upload quota.
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel diniz gisoldo committed Oct 6, 2017
1 parent 6457f95 commit 6206f4d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
15 changes: 14 additions & 1 deletion vimeo/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,21 @@ def __init__(self, message):
super(ObjectLoadFailure, self).__init__(message)


class UploadQuotaExceeded(Exception):
"""Exception for upload quota execeeded."""

def _get_free_space(self, num):
"""Transform bytes in giga bytes."""
return 'Free space quota: %sGb' % (round((num / 1073741824.0), 1))

def __init__(self, free_quota, message):
"""Init method for this subclass of BaseVimeoException."""
message = message + self._get_free_space(num=free_quota)
super(UploadQuotaExceeded, self).__init__(message)


class UploadTicketCreationFailure(BaseVimeoException):
"""Exception for upload tickt creation failure."""
"""Exception for upload ticket creation failure."""

def __init__(self, response, message):
"""Init method for this subclass of BaseVimeoException."""
Expand Down
10 changes: 9 additions & 1 deletion vimeo/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def upload(self, filename, upgrade_to_1080=False):
self.UPLOAD_ENDPOINT,
data={'type': 'streaming',
'upgrade_to_1080': 'true' if upgrade_to_1080 else 'false'},
params={'fields': 'upload_link,complete_uri'})
params={'fields': 'upload_link,complete_uri,user.upload_quota.' +
'space.free'})

return self._perform_upload(filename, ticket)

Expand All @@ -52,11 +53,15 @@ def _perform_upload(self, filename, ticket):

# Perform the actual upload.
target = ticket['upload_link']
free_quota = ticket['user']['upload_quota']['space']['free']
last_byte = 0
# Try to get size of obj by path. If provided obj is not a file path
# find the size of file-like object.
try:
size = os.path.getsize(filename)
if size > free_quota:
raise exceptions.UploadQuotaExceeded(
free_quota, 'Upload quota was exceeded.')
with io.open(filename, 'rb') as f:
while last_byte < size:
try:
Expand All @@ -68,6 +73,9 @@ def _perform_upload(self, filename, ticket):
last_byte = self._get_progress(target, size)
except TypeError:
size = len(filename.read())
if size > free_quota:
raise exceptions.UploadQuotaExceeded(
free_quota, 'Upload quota was exceeded.')
f = filename
while last_byte < size:
try:
Expand Down

0 comments on commit 6206f4d

Please sign in to comment.