33from contextlib import contextmanager
44from ..objects .uri import quote , unquote
55from ..common import Object , DateTimeUtils
6- from ..core .enum import ProtectionLevel , CollaboratorType , SearchType , PortalAccountType , FileAccessMode , FileAccessError
6+ from ..core .enum import ProtectionLevel , CollaboratorType , SearchType , PortalAccountType , FileAccessMode , FileAccessError , \
7+ UploadError
78from ..core .types import PortalAccount , UserAccount , GroupAccount
8- from ..exceptions .io import ResourceExistsError , PathValidationError , NameSyntaxError , ReservedNameError , RestrictedRoot
9+ from ..exceptions .io import ResourceExistsError , PathValidationError , NameSyntaxError , \
10+ ReservedNameError , RestrictedRoot , InsufficientPermission
11+ from ..exceptions .io import UploadException , OutOfQuota , RejectedByPolicy , NoStorageBucket , WindowsACLError
912from ..lib .iterator import DefaultResponse
1013from . import common
1114
@@ -181,7 +184,7 @@ def build(self):
181184class FetchResourcesResponse (DefaultResponse ):
182185
183186 def __init__ (self , response ):
184- accept_response (response .errorType )
187+ accept_error (response .errorType )
185188 super ().__init__ (response )
186189
187190 @property
@@ -291,7 +294,7 @@ def handle(path):
291294
292295
293296def destination_prerequisite_conditions (destination , name ):
294- if not destination .reference .root :
297+ if not len ( destination .reference .parts ) > 0 :
295298 raise RestrictedRoot ()
296299 if any (c in name for c in ['\\ ' , '/' , ':' , '?' , '&' , '<' , '>' , '"' , '|' ]):
297300 raise NameSyntaxError ()
@@ -311,6 +314,26 @@ def upload(core, name, destination, size, fd):
311314 yield param
312315
313316
317+ def validate_transfer_success (response , path ):
318+ if response .rc :
319+ logger .error ('Upload of file: "%s" failed.' , path )
320+ if response .msg == UploadError .UserQuotaViolation :
321+ raise OutOfQuota ('User' , path )
322+ if response .msg == UploadError .PortalQuotaViolation :
323+ raise OutOfQuota ('Team Portal' , path )
324+ if response .msg == UploadError .FolderQuotaViolation :
325+ raise OutOfQuota ('Cloud drive folder' , path )
326+ if response .msg == UploadError .RejectedByPolicy :
327+ raise RejectedByPolicy (path )
328+ if response .msg == UploadError .WindowsACL :
329+ raise WindowsACLError (path )
330+ if response .msg .startswith (UploadError .NoStorageBucket ):
331+ raise NoStorageBucket (path )
332+ raise UploadException (f'Upload failed. Reason: { response .msg } ' , path )
333+ if not response .rc and response .msg == 'OK' :
334+ logger .info ('Upload successful. Saved to: %s' , path )
335+
336+
314337@contextmanager
315338def handle_many (directory , objects ):
316339 param = Object ()
@@ -508,15 +531,16 @@ def obtain_current_accounts(param):
508531 return current_accounts
509532
510533
511- def accept_response (error_type ):
534+ def accept_error (error_type ):
512535 """
513536 Check if response contains an error.
514537 """
515538 error = {
516539 FileAccessError .FileWithTheSameNameExist : ResourceExistsError (),
517540 FileAccessError .DestinationNotExists : PathValidationError (),
518541 FileAccessError .InvalidName : NameSyntaxError (),
519- FileAccessError .ReservedName : ReservedNameError ()
542+ FileAccessError .ReservedName : ReservedNameError (),
543+ FileAccessError .PermissionDenied : InsufficientPermission ()
520544 }.get (error_type , None )
521545 try :
522546 if error :
0 commit comments