def _send_hook(self, action): source = None destination = None if action in ('move', 'copy'): # if provider can't intra_move or copy, then the celery task will take care of logging if not getattr(self.provider, 'can_intra_' + action)(self.dest_provider, self.path): return source = LogPayload(self.resource, self.provider, path=self.path) destination = LogPayload( self.dest_resource, self.dest_provider, metadata=self.dest_meta, ) elif action in ('create', 'create_folder', 'update'): source = LogPayload(self.resource, self.provider, metadata=self.metadata) elif action in ('delete', 'download_file', 'download_zip'): source = LogPayload(self.resource, self.provider, path=self.path) else: return remote_logging.log_file_action(action, source=source, destination=destination, api_version='v1', request=remote_logging._serialize_request(self.request), bytes_downloaded=self.bytes_downloaded, bytes_uploaded=self.bytes_uploaded,)
def _send_hook(self, action): source = None destination = None if action in ('move', 'copy'): # if provider can't intra_move or copy, then the celery task will take care of logging if not getattr(self.provider, 'can_intra_' + action)( self.dest_provider, self.path): return source = LogPayload(self.resource, self.provider, path=self.path) destination = LogPayload( self.dest_resource, self.dest_provider, metadata=self.dest_meta, ) elif action in ('create', 'create_folder', 'update'): source = LogPayload(self.resource, self.provider, metadata=self.metadata) elif action in ('delete', 'download_file', 'download_zip'): source = LogPayload(self.resource, self.provider, path=self.path) else: return remote_logging.log_file_action( action, source=source, destination=destination, api_version='v1', request=remote_logging._serialize_request(self.request), bytes_downloaded=self.bytes_downloaded, bytes_uploaded=self.bytes_uploaded, )
def _send_hook(self, action, metadata): source = LogPayload(self.json['source']['nid'], self.source_provider, path=self.json['source']['path']) destination = LogPayload(self.json['destination']['nid'], self.destination_provider, metadata=metadata) remote_logging.log_file_action(action, source=source, destination=destination, api_version='v0', request=remote_logging._serialize_request(self.request), bytes_downloaded=self.bytes_downloaded, bytes_uploaded=self.bytes_uploaded)
def _send_hook(self, action, metadata=None, path=None): source = LogPayload(self.arguments["nid"], self.provider, metadata=metadata, path=path) remote_logging.log_file_action( action, source=source, api_version="v0", request=remote_logging._serialize_request(self.request), bytes_downloaded=self.bytes_downloaded, bytes_uploaded=self.bytes_uploaded, )
def _send_hook(self, action, metadata=None, path=None): source = LogPayload(self.arguments['nid'], self.provider, metadata=metadata, path=path) remote_logging.log_file_action( action, source=source, api_version='v0', request=remote_logging._serialize_request(self.request), bytes_downloaded=self.bytes_downloaded, bytes_uploaded=self.bytes_uploaded)
def _send_hook(self, action, metadata): source = LogPayload(self.json["source"]["nid"], self.source_provider, path=self.json["source"]["path"]) destination = LogPayload(self.json["destination"]["nid"], self.destination_provider, metadata=metadata) remote_logging.log_file_action( action, source=source, destination=destination, api_version="v0", request=remote_logging._serialize_request(self.request), bytes_downloaded=self.bytes_downloaded, bytes_uploaded=self.bytes_uploaded, )
def _send_hook(self, action, metadata): source = LogPayload(self.json['source']['nid'], self.source_provider, path=self.json['source']['path']) destination = LogPayload(self.json['destination']['nid'], self.destination_provider, metadata=metadata) remote_logging.log_file_action( action, source=source, destination=destination, api_version='v0', request=remote_logging._serialize_request(self.request), bytes_downloaded=self.bytes_downloaded, bytes_uploaded=self.bytes_uploaded)
async def post(self): if not self.source_provider.can_intra_move( self.destination_provider, self.json['source']['path']): resp = await tasks.move.adelay( { 'nid': self.json['source']['nid'], 'path': self.json['source']['path'], 'provider': self.source_provider.serialized() }, { 'nid': self.json['destination']['nid'], 'path': self.json['destination']['path'], 'provider': self.destination_provider.serialized() }, rename=self.json.get('rename'), conflict=self.json.get('conflict', 'replace'), start_time=time.time(), request=remote_logging._serialize_request(self.request), ) metadata, created = await tasks.wait_on_celery(resp) else: metadata, created = (await tasks.backgrounded( self.source_provider.move, self.destination_provider, self.json['source']['path'], self.json['destination']['path'], rename=self.json.get('rename'), conflict=self.json.get('conflict', 'replace'), )) if created: self.set_status(201) else: self.set_status(200) self.write(metadata.serialized()) if self.source_provider.can_intra_move(self.destination_provider, self.json['source']['path']): self._send_hook('move', metadata)
async def post(self): if not self.source_provider.can_intra_move(self.destination_provider, self.json['source']['path']): resp = await tasks.move.adelay({ 'nid': self.json['source']['nid'], 'path': self.json['source']['path'], 'provider': self.source_provider.serialized() }, { 'nid': self.json['destination']['nid'], 'path': self.json['destination']['path'], 'provider': self.destination_provider.serialized() }, rename=self.json.get('rename'), conflict=self.json.get('conflict', 'replace'), start_time=time.time(), request=remote_logging._serialize_request(self.request), ) metadata, created = await tasks.wait_on_celery(resp) else: metadata, created = ( await tasks.backgrounded( self.source_provider.move, self.destination_provider, self.json['source']['path'], self.json['destination']['path'], rename=self.json.get('rename'), conflict=self.json.get('conflict', 'replace'), ) ) if created: self.set_status(201) else: self.set_status(200) self.write(metadata.serialized()) if self.source_provider.can_intra_move(self.destination_provider, self.json['source']['path']): self._send_hook('move', metadata)
async def move_or_copy(self): """Copy, move, and rename files and folders. **Auth actions**: ``copy``, ``move``, or ``rename`` **Provider actions**: ``copy`` or ``move`` *Auth actions* come from the ``action`` body parameter in the request and are used by the auth handler. *Provider actions* are determined from the *auth action*. A "rename" is a special case of the "move" provider action that implies that the destination resource, provider, and parent path will all be the same as the source. """ # Force the json body to load into memory await self.request.body auth_action = self.json.get('action', 'null') if auth_action not in ('copy', 'move', 'rename'): raise exceptions.InvalidParameters('Auth action must be "copy", "move", or "rename", ' 'not "{}"'.format(auth_action)) # Provider setup is delayed so the provider action can be updated from the auth action. provider = self.path_kwargs.get('provider', '') provider_action = auth_action if auth_action == 'rename': if not self.json.get('rename', ''): raise exceptions.InvalidParameters('"rename" field is required for renaming') provider_action = 'move' self.auth = await auth_handler.get( self.resource, provider, self.request, action=auth_action, auth_type=AuthType.SOURCE ) self.provider = make_provider( provider, self.auth['auth'], self.auth['credentials'], self.auth['settings'] ) self.path = await self.provider.validate_v1_path(self.path, **self.arguments) if auth_action == 'rename': # 'rename' implies the file/folder does not change location self.dest_auth = self.auth self.dest_provider = self.provider self.dest_path = self.path.parent self.dest_resource = self.resource else: path = self.json.get('path', None) if path is None: raise exceptions.InvalidParameters('"path" field is required for moves or copies') if not path.endswith('/'): raise exceptions.InvalidParameters( '"path" field requires a trailing slash to indicate it is a folder' ) # TODO optimize for same provider and resource # for copy action, `auth_action` is the same as `provider_action` if auth_action == 'copy' and self.path.is_root and not self.json.get('rename'): raise exceptions.InvalidParameters('"rename" field is required for copying root') # Note: attached to self so that _send_hook has access to these self.dest_resource = self.json.get('resource', self.resource) self.dest_auth = await auth_handler.get( self.dest_resource, self.json.get('provider', self.provider.NAME), self.request, action=auth_action, auth_type=AuthType.DESTINATION, ) self.dest_provider = make_provider( self.json.get('provider', self.provider.NAME), self.dest_auth['auth'], self.dest_auth['credentials'], self.dest_auth['settings'] ) self.dest_path = await self.dest_provider.validate_path(**self.json) if not getattr(self.provider, 'can_intra_' + provider_action)(self.dest_provider, self.path): # this weird signature syntax courtesy of py3.4 not liking trailing commas on kwargs conflict = self.json.get('conflict', DEFAULT_CONFLICT) result = await getattr(tasks, provider_action).adelay( rename=self.json.get('rename'), conflict=conflict, request=remote_logging._serialize_request(self.request), *self.build_args() ) metadata, created = await tasks.wait_on_celery(result) else: metadata, created = ( await tasks.backgrounded( getattr(self.provider, provider_action), self.dest_provider, self.path, self.dest_path, rename=self.json.get('rename'), conflict=self.json.get('conflict', DEFAULT_CONFLICT), ) ) self.dest_meta = metadata if created: self.set_status(int(HTTPStatus.CREATED)) else: self.set_status(int(HTTPStatus.OK)) self.write({'data': metadata.json_api_serialized(self.dest_resource)})
async def move_or_copy(self): # Force the json body to load into memory await self.request.body if self.json.get('action') not in ('copy', 'move', 'rename'): # Note: null is used as the default to avoid python specific error messages raise exceptions.InvalidParameters( 'Action must be copy, move or rename, ' 'not {}'.format(self.json.get('action', 'null'))) # Setup of the provider was delayed so the json action could be retrieved from the request body. provider = self.path_kwargs['provider'] action = self.json['action'] self.auth = await auth_handler.get(self.resource, provider, self.request, action=action, auth_type=AuthType.SOURCE) self.provider = make_provider(provider, self.auth['auth'], self.auth['credentials'], self.auth['settings']) self.path = await self.provider.validate_v1_path( self.path, **self.arguments) if action == 'rename': if not self.json.get('rename'): raise exceptions.InvalidParameters( 'Rename is required for renaming') action = 'move' self.dest_auth = self.auth self.dest_provider = self.provider self.dest_path = self.path.parent self.dest_resource = self.resource else: if 'path' not in self.json: raise exceptions.InvalidParameters( 'Path is required for moves or copies') if not self.json['path'].endswith('/'): raise exceptions.InvalidParameters( 'Path requires a trailing slash to indicate ' 'it is a folder') # TODO optimize for same provider and resource # Note: attached to self so that _send_hook has access to these self.dest_resource = self.json.get('resource', self.resource) self.dest_auth = await auth_handler.get( self.dest_resource, self.json.get('provider', self.provider.NAME), self.request, action=action, auth_type=AuthType.DESTINATION, ) self.dest_provider = make_provider( self.json.get('provider', self.provider.NAME), self.dest_auth['auth'], self.dest_auth['credentials'], self.dest_auth['settings']) self.dest_path = await self.dest_provider.validate_path(**self.json ) if not getattr(self.provider, 'can_intra_' + action)( self.dest_provider, self.path): # this weird signature syntax courtesy of py3.4 not liking trailing commas on kwargs conflict = self.json.get('conflict', DEFAULT_CONFLICT) result = await getattr(tasks, action).adelay( rename=self.json.get('rename'), conflict=conflict, request=remote_logging._serialize_request(self.request), *self.build_args()) metadata, created = await tasks.wait_on_celery(result) else: metadata, created = (await tasks.backgrounded( getattr(self.provider, action), self.dest_provider, self.path, self.dest_path, rename=self.json.get('rename'), conflict=self.json.get('conflict', DEFAULT_CONFLICT), )) self.dest_meta = metadata if created: self.set_status(201) else: self.set_status(200) self.write({'data': metadata.json_api_serialized(self.dest_resource)})
async def move_or_copy(self): """Copy, move, and rename files and folders. **Auth actions**: ``copy``, ``move``, or ``rename`` **Provider actions**: ``copy`` or ``move`` *Auth actions* come from the ``action`` body parameter in the request and are used by the auth handler. *Provider actions* are determined from the *auth action*. A "rename" is a special case of the "move" provider action that implies that the destination resource, provider, and parent path will all be the same as the source. """ # Force the json body to load into memory await self.request.body auth_action = self.json.get('action', 'null') if auth_action not in ('copy', 'move', 'rename'): raise exceptions.InvalidParameters('Auth action must be "copy", "move", or "rename", ' 'not "{}"'.format(auth_action)) # Provider setup is delayed so the provider action can be updated from the auth action. provider = self.path_kwargs.get('provider', '') provider_action = auth_action if auth_action == 'rename': if not self.json.get('rename', ''): raise exceptions.InvalidParameters('"rename" field is required for renaming') provider_action = 'move' self.auth = await auth_handler.get( self.resource, provider, self.request, action=auth_action, auth_type=AuthType.SOURCE, path=self.path, version=self.requested_version, ) self.provider = make_provider( provider, self.auth['auth'], self.auth['credentials'], self.auth['settings'] ) self.path = await self.provider.validate_v1_path(self.path, **self.arguments) if auth_action == 'rename': # 'rename' implies the file/folder does not change location self.dest_auth = self.auth self.dest_provider = self.provider self.dest_path = self.path.parent self.dest_resource = self.resource else: path = self.json.get('path', None) if path is None: raise exceptions.InvalidParameters('"path" field is required for moves or copies') if not path.endswith('/'): raise exceptions.InvalidParameters( '"path" field requires a trailing slash to indicate it is a folder' ) # TODO optimize for same provider and resource # for copy action, `auth_action` is the same as `provider_action` if auth_action == 'copy' and self.path.is_root and not self.json.get('rename'): raise exceptions.InvalidParameters('"rename" field is required for copying root') # Note: attached to self so that _send_hook has access to these self.dest_resource = self.json.get('resource', self.resource) self.dest_auth = await auth_handler.get( self.dest_resource, self.json.get('provider', self.provider.NAME), self.request, action=auth_action, auth_type=AuthType.DESTINATION, ) self.dest_provider = make_provider( self.json.get('provider', self.provider.NAME), self.dest_auth['auth'], self.dest_auth['credentials'], self.dest_auth['settings'] ) self.dest_path = await self.dest_provider.validate_path(**self.json) if not getattr(self.provider, 'can_intra_' + provider_action)(self.dest_provider, self.path): # this weird signature syntax courtesy of py3.4 not liking trailing commas on kwargs conflict = self.json.get('conflict', DEFAULT_CONFLICT) result = await getattr(tasks, provider_action).adelay( rename=self.json.get('rename'), conflict=conflict, request=remote_logging._serialize_request(self.request), *self.build_args() ) metadata, created = await tasks.wait_on_celery(result) else: metadata, created = ( await tasks.backgrounded( getattr(self.provider, provider_action), self.dest_provider, self.path, self.dest_path, rename=self.json.get('rename'), conflict=self.json.get('conflict', DEFAULT_CONFLICT), ) ) self.dest_meta = metadata if created: self.set_status(int(HTTPStatus.CREATED)) else: self.set_status(int(HTTPStatus.OK)) self.write({'data': metadata.json_api_serialized(self.dest_resource)})
async def move_or_copy(self): # Force the json body to load into memory await self.request.body if self.json.get('action') not in ('copy', 'move', 'rename'): # Note: null is used as the default to avoid python specific error messages raise exceptions.InvalidParameters('Action must be copy, move or rename, not {}'.format(self.json.get('action', 'null'))) if self.json['action'] == 'rename': if not self.json.get('rename'): raise exceptions.InvalidParameters('Rename is required for renaming') action = 'move' self.dest_auth = self.auth self.dest_provider = self.provider self.dest_path = self.path.parent self.dest_resource = self.resource else: if 'path' not in self.json: raise exceptions.InvalidParameters('Path is required for moves or copies') action = self.json['action'] # Note: attached to self so that _send_hook has access to these self.dest_resource = self.json.get('resource', self.resource) # TODO optimize for same provider and resource self.dest_auth = await auth_handler.get( self.dest_resource, self.json.get('provider', self.provider.NAME), self.request ) self.dest_provider = make_provider( self.json.get('provider', self.provider.NAME), self.dest_auth['auth'], self.dest_auth['credentials'], self.dest_auth['settings'] ) self.dest_path = await self.dest_provider.validate_path(**self.json) if not getattr(self.provider, 'can_intra_' + action)(self.dest_provider, self.path): # this weird signature syntax courtesy of py3.4 not liking trailing commas on kwargs conflict = self.json.get('conflict', DEFAULT_CONFLICT) result = await getattr(tasks, action).adelay( rename=self.json.get('rename'), conflict=conflict, request=remote_logging._serialize_request(self.request), *self.build_args() ) metadata, created = await tasks.wait_on_celery(result) else: metadata, created = ( await tasks.backgrounded( getattr(self.provider, action), self.dest_provider, self.path, self.dest_path, rename=self.json.get('rename'), conflict=self.json.get('conflict', DEFAULT_CONFLICT), ) ) self.dest_meta = metadata if created: self.set_status(201) else: self.set_status(200) self.write({'data': metadata.json_api_serialized(self.dest_resource)})