def dropbox_hgrid_data_contents(node_addon, auth, **kwargs): """Return the Rubeus/HGrid-formatted response for a folder's contents. Takes optional query parameters `foldersOnly` (only return folders) and `includeRoot` (include the root folder). """ # No folder, just return an empty list of data if node_addon.folder is None and not request.args.get('foldersOnly'): return {'data': []} node = node_addon.owner path = kwargs.get('path', '') # Verify that path is a subdirectory of the node's shared folder if not is_authorizer(auth, node_addon): abort_if_not_subdir(path, node_addon.folder) permissions = { 'edit': node.can_edit(auth) and not node.is_registration, 'view': node.can_view(auth) } client = get_node_client(node) file_not_found = HTTPError(http.NOT_FOUND, data=dict(message_short='File not found', message_long='The Dropbox file ' 'you requested could not be found.')) max_retry_error = HTTPError(http.REQUEST_TIMEOUT, data=dict(message_short='Request Timeout', message_long='Dropbox could not be reached ' 'at this time.')) try: metadata = client.metadata(path) except ErrorResponse: raise file_not_found except MaxRetryError: raise max_retry_error # Raise error if folder was deleted if metadata.get('is_deleted'): raise file_not_found contents = metadata['contents'] if request.args.get('foldersOnly'): contents = [metadata_to_hgrid(file_dict, node, permissions) for file_dict in contents if file_dict['is_dir']] else: contents = [metadata_to_hgrid(file_dict, node, permissions) for file_dict in contents] if request.args.get('includeRoot'): root = {'kind': rubeus.FOLDER, 'path': '/', 'name': '/ (Full Dropbox)'} contents.insert(0, root) return contents
def dropbox_upload(node_addon, auth, **kwargs): """View for uploading a file from the filebrowser interface. Must return the Rubeus/HGrid representation of the newly added file. """ node = node_addon.owner # Route may or may not have a path path = kwargs.get('path', node_addon.folder) client = get_node_addon_client(node_addon) file_obj = request.files.get('file', None) node = node_addon.owner if path and file_obj and client: filepath = os.path.join(path, file_obj.filename) # Check that user has access to the folder being uploaded to if not is_authorizer(auth, node_addon): abort_if_not_subdir(path, node_addon.folder) metadata = client.put_file(filepath, file_obj) permissions = { 'edit': node.can_edit(auth), 'view': node.can_view(auth) } # Log the event nodelogger = DropboxNodeLogger(node=node, auth=auth, path=filepath) nodelogger.log(NodeLog.FILE_ADDED, save=True) # Return the HGrid-formatted JSON response return metadata_to_hgrid(metadata, node=node, permissions=permissions), http.CREATED raise HTTPError(http.BAD_REQUEST)
def test_metadata_to_hgrid(self): metadata = { u'bytes': 123, u'icon': u'file', u'is_dir': False, u'modified': u'Sat, 22 Mar 2014 05:40:29 +0000', u'path': u'/foo/bar/baz.mp3', u'rev': u'3fed51f002c12fc', u'revision': 67032351, u'root': u'dropbox', u'size': u'0 bytes', u'thumb_exists': False, u'mime_type': u'audio/mpeg', } node = ProjectFactory() permissions = {'view': True, 'edit': False} result = utils.metadata_to_hgrid(metadata, node, permissions) assert_equal(result['addon'], 'dropbox') assert_equal(result['permissions'], permissions) filename = utils.get_file_name(metadata['path']) assert_equal(result['name'], filename) assert_equal(result['path'], metadata['path']) assert_equal(result['ext'], os.path.splitext(filename)[1])