Beispiel #1
0
def get_tile_info(toyz_settings, tid, params):
    """
    Get new tiles that need to be loaded
    """
    import toyz.web.viewer as viewer
    
    core.check4keys(params, ['img_info', 'file_info'])
    if tid['user_id']!='admin':
        permissions = file_access.get_parent_permissions(
            toyz_settings.db, params['file_info']['filepath'], user_id=tid['user_id'])
        if 'r' not in permissions:
            raise ToyzJobError(
                'You do not have permission to view the requested file.'
                'Please contact your network administrator if you believe this is an error.')
    
    all_tiles, new_tiles = viewer.get_tile_info(params['file_info'], params['img_info'])
    
    #print('all tile:', all_tiles)
    
    response = {
        'id': 'tile info',
        'all_tiles': all_tiles,
        'new_tiles': new_tiles
    }
    return response
Beispiel #2
0
def get_img_tile(toyz_settings, tid, params):
    """
    Load a tile from a larger image and notify the client it has been created
    """
    import toyz.web.viewer as viewer
    
    core.check4keys(params, ['img_info', 'file_info', 'tile_info'])
    if tid['user_id']!='admin':
        permissions = file_access.get_parent_permissions(
            toyz_settings.db, params['file_info']['filepath'], user_id=tid['user_id'])
        if 'r' not in permissions:
            raise ToyzJobError(
                'You do not have permission to view the requested file.'
                'Please contact your network administrator if you believe this is an error.')
    
    created, tile_info = viewer.create_tile(
        params['file_info'], params['img_info'], params['tile_info'])
    
    response = {
        'id': 'tile created',
        'success': created,
        'tile_info': tile_info
    }
    
    return response
Beispiel #3
0
def get_file_info(toyz_settings, tid, params):
    """
    Get information about an image file
    """
    import toyz.web.viewer as viewer
    core.check4keys(params, ['file_info', 'img_info'])
    if tid['user_id']!='admin':
        permissions = file_access.get_parent_permissions(
            toyz_settings.db, params['file_info']['filepath'], user_id=tid['user_id'])
        if permissions is None or 'r' not in permissions:
            raise ToyzJobError(
                'You do not have permission to view the requested file.'
                'Please contact your network administrator if you believe this is an error.')
    file_info = viewer.get_file_info(params['file_info'])
    img_info = file_info['images'][file_info['frame']]
    img_info.update(params['img_info'])
    # Get the tile map for the first image
    result = get_img_info(toyz_settings, tid, {
        'file_info': file_info,
        'img_info': img_info
    })
    
    file_info['images'][file_info['frame']] = result['img_info']
    response = {
        'id': 'file info',
        'file_info': file_info,
        'new_tiles': result['new_tiles']
    }
    return response
Beispiel #4
0
def get_img_info(toyz_settings, tid, params):
    """
    Map a large image into a set of tiles that make up the larger image
    """
    import toyz.web.viewer as viewer
    print('************************************************')
    core.check4keys(params, ['img_info', 'file_info'])
    if tid['user_id']!='admin':
        permissions = file_access.get_parent_permissions(
            toyz_settings.db, params['file_info']['filepath'], user_id=tid['user_id'])
        if 'r' not in permissions:
            raise ToyzJobError(
                'You do not have permission to view the requested file.'
                'Please contact your network administrator if you believe this is an error.')
    shortcuts = db_utils.get_param(toyz_settings.db, 'shortcuts', user_id=tid['user_id'])
    save_path = os.path.join(shortcuts['temp'], tid['session_id'], 'images')
    params['img_info']['save_path'] = save_path
    img_info = viewer.get_img_info(params['file_info'], params['img_info'])
    
    result = get_tile_info(toyz_settings, tid, {
        'file_info': params['file_info'],
        'img_info': img_info
    })
    img_info['tiles'] = result['new_tiles']
    response = {
        'id': 'img info',
        'img_info': img_info,
        'new_tiles': result['new_tiles']
    }
    print('************************************************')
    return response
Beispiel #5
0
def get_img_tile(toyz_settings, tid, params):
    """
    Load a tile from a larger image and notify the client it has been created
    """
    import toyz.web.viewer as viewer

    core.check4keys(params, ['img_info', 'file_info', 'tile_info'])
    if tid['user_id'] != 'admin':
        permissions = file_access.get_parent_permissions(
            toyz_settings.db,
            params['file_info']['filepath'],
            user_id=tid['user_id'])
        if 'r' not in permissions:
            raise ToyzJobError(
                'You do not have permission to view the requested file.'
                'Please contact your network administrator if you believe this is an error.'
            )

    created, tile_info = viewer.create_tile(params['file_info'],
                                            params['img_info'],
                                            params['tile_info'])

    response = {
        'id': 'tile created',
        'success': created,
        'tile_info': tile_info
    }

    return response
Beispiel #6
0
def create_paths(toyz_settings, tid, params):
    """
    Creates a new path on the server (if it does not already exist).
    
    Parameters
        - toyz_settings ( :py:class:`toyz.utils.core.ToyzSettings`): Settings for the toyz 
          application
        - tid (*string* ): Task ID of the client user running the task
        - params (*dict* ): Any parameters sent by the client (see *params* below)
    
    Params
        - path (*string* ): path to create on the server
    
    Response
        id: 'create_folder',
        status (*string* ): 'success',
        path (*string* ): path created on the server
    """
    core.check4keys(params, ['path', 'new_folder'])
    path = os.path.join(params['path'], params['new_folder'])
    permissions = file_access.get_parent_permissions(toyz_settings.db,
        path, user_id=tid['user_id'])
    if 'w' not in permissions and 'x' not in permissions:
        raise ToyzJobError("You do not have permission to create path {0}".format(path))
    core.create_paths(path)
    response = {
        'id': 'notification',
        'msg': 'Created path'.format(path),
        'func': 'create_dir'
    }
    return response
Beispiel #7
0
def get_tile_info(toyz_settings, tid, params):
    """
    Get new tiles that need to be loaded
    """
    import toyz.web.viewer as viewer

    core.check4keys(params, ['img_info', 'file_info'])
    if tid['user_id'] != 'admin':
        permissions = file_access.get_parent_permissions(
            toyz_settings.db,
            params['file_info']['filepath'],
            user_id=tid['user_id'])
        if 'r' not in permissions:
            raise ToyzJobError(
                'You do not have permission to view the requested file.'
                'Please contact your network administrator if you believe this is an error.'
            )

    all_tiles, new_tiles = viewer.get_tile_info(params['file_info'],
                                                params['img_info'])

    #print('all tile:', all_tiles)

    response = {
        'id': 'tile info',
        'all_tiles': all_tiles,
        'new_tiles': new_tiles
    }
    return response
Beispiel #8
0
def get_file_info(toyz_settings, tid, params):
    """
    Get information about an image file
    """
    import toyz.web.viewer as viewer
    core.check4keys(params, ['file_info', 'img_info'])
    if tid['user_id'] != 'admin':
        permissions = file_access.get_parent_permissions(
            toyz_settings.db,
            params['file_info']['filepath'],
            user_id=tid['user_id'])
        if permissions is None or 'r' not in permissions:
            raise ToyzJobError(
                'You do not have permission to view the requested file.'
                'Please contact your network administrator if you believe this is an error.'
            )
    file_info = viewer.get_file_info(params['file_info'])
    img_info = file_info['images'][file_info['frame']]
    img_info.update(params['img_info'])
    # Get the tile map for the first image
    result = get_img_info(toyz_settings, tid, {
        'file_info': file_info,
        'img_info': img_info
    })

    file_info['images'][file_info['frame']] = result['img_info']
    response = {
        'id': 'file info',
        'file_info': file_info,
        'new_tiles': result['new_tiles']
    }
    return response
Beispiel #9
0
def create_paths(toyz_settings, tid, params):
    """
    Creates a new path on the server (if it does not already exist).
    
    Parameters
        - toyz_settings ( :py:class:`toyz.utils.core.ToyzSettings`): Settings for the toyz 
          application
        - tid (*string* ): Task ID of the client user running the task
        - params (*dict* ): Any parameters sent by the client (see *params* below)
    
    Params
        - path (*string* ): path to create on the server
    
    Response
        id: 'create_folder',
        status (*string* ): 'success',
        path (*string* ): path created on the server
    """
    core.check4keys(params, ['path', 'new_folder'])
    path = os.path.join(params['path'], params['new_folder'])
    permissions = file_access.get_parent_permissions(toyz_settings.db,
                                                     path,
                                                     user_id=tid['user_id'])
    if 'w' not in permissions and 'x' not in permissions:
        raise ToyzJobError(
            "You do not have permission to create path {0}".format(path))
    core.create_paths(path)
    response = {
        'id': 'notification',
        'msg': 'Created path'.format(path),
        'func': 'create_dir'
    }
    return response
Beispiel #10
0
 def validate_absolute_path(self, root, full_path):
     """
     Check that the user has permission to view the file
     """
     #print('root:{0}, full_path:{1}\n\n'.format(root, full_path))
     permissions = file_access.get_parent_permissions(
         self.application.toyz_settings.db, full_path, 
         user_id=self.get_current_user().strip('"'))
     if 'r' in permissions or 'x' in permissions:
         absolute_path = tornado.web.StaticFileHandler.validate_absolute_path(
                 self, root, full_path)
     else:
         absolute_path = None
     #print('Absoulte path:', absolute_path)
     return absolute_path
Beispiel #11
0
 def validate_absolute_path(self, root, full_path):
     """
     Check that the user has permission to view the file
     """
     #print('root:{0}, full_path:{1}\n\n'.format(root, full_path))
     permissions = file_access.get_parent_permissions(
         self.application.toyz_settings.db,
         full_path,
         user_id=self.get_current_user().strip('"'))
     if 'r' in permissions or 'x' in permissions:
         absolute_path = tornado.web.StaticFileHandler.validate_absolute_path(
             self, root, full_path)
     else:
         absolute_path = None
     #print('Absoulte path:', absolute_path)
     return absolute_path
Beispiel #12
0
def get_img_info(toyz_settings, tid, params):
    """
    Map a large image into a set of tiles that make up the larger image
    """
    import toyz.web.viewer as viewer
    print('************************************************')
    core.check4keys(params, ['img_info', 'file_info'])
    if tid['user_id'] != 'admin':
        permissions = file_access.get_parent_permissions(
            toyz_settings.db,
            params['file_info']['filepath'],
            user_id=tid['user_id'])
        if 'r' not in permissions:
            raise ToyzJobError(
                'You do not have permission to view the requested file.'
                'Please contact your network administrator if you believe this is an error.'
            )
    shortcuts = db_utils.get_param(toyz_settings.db,
                                   'shortcuts',
                                   user_id=tid['user_id'])
    save_path = os.path.join(shortcuts['temp'], tid['session_id'], 'images')
    params['img_info']['save_path'] = save_path
    img_info = viewer.get_img_info(params['file_info'], params['img_info'])

    result = get_tile_info(toyz_settings, tid, {
        'file_info': params['file_info'],
        'img_info': img_info
    })
    img_info['tiles'] = result['new_tiles']
    response = {
        'id': 'img info',
        'img_info': img_info,
        'new_tiles': result['new_tiles']
    }
    print('************************************************')
    return response
Beispiel #13
0
def load_directory(toyz_settings, tid, params):
    """
    Used by the file browser to load the folders and files in a given path.
    
    Parameters
        - toyz_settings ( :py:class:`toyz.utils.core.ToyzSettings`): Settings for the toyz 
          application
        - tid (*string* ): Task ID of the client user running the task
        - params (*dict* ): Any parameters sent by the client (see *params* below)
    
    Params
        - path (*string* ): Path to search
    
    Response
        - id: 'directory'
        - path (*string* ): path passed to the function
        - shortcuts (*dict* ): Dictionary of ``shortcut_name: shortcut_path`` 's for the user
        - folders (*list* of strings): folders contained in the path
        - files (*list* of strings): files contained in the path
        - parent (*string* ): parent directory of current path
    """
    core.check4keys(params,['path'])
    show_hidden=False
    # If the path is contained in a set of dollar signs (for example `$images$`) then 
    # search in the users shortcuts for the given path
    shortcuts = db_utils.get_param(toyz_settings.db, 'shortcuts', user_id=tid['user_id'])
    if params['path'][0]=='$' and params['path'][-1]=='$':
        shortcut = params['path'][1:-1]
        if shortcut not in shortcuts:
            raise ToyzJobError("Shortcut '{0}' not found for user {1}".format(shortcut, 
                tid['user_id']))
        params['path'] = shortcuts[shortcut]
    
    if not os.path.isdir(params['path']):
        parent = os.path.dirname(params['path'])
        if not os.path.isdir(parent):
            raise ToyzJobError("Path '{0}' not found".format(params['path']))
        params['path'] = parent
    if 'show_hidden' in params and params['show_hidden']:
        show_hidden=True
    
    # Keep separate lists of the files and directories for the current path.
    # Only include the files and directories the user has permissions to view
    files = []
    folders = []
    groups = db_utils.get_param(toyz_settings.db, 'groups', user_id=tid['user_id'])
    if 'admin' in groups or tid['user_id'] == 'admin':
        admin=True
    else:
        admin=False
    for f in os.listdir(params['path']):
        if(f[0]!='.' or show_hidden):
            f_path =os.path.join(params['path'],f)
            if admin:
                permission = True
            else:
                permissions = file_access.get_parent_permissions(toyz_settings.db,
                    f_path, user_id=tid['user_id'])
                if permissions is None:
                    permissions = ''
                permission = 'f' in permissions
            if permission:
                if os.path.isfile(f_path):
                    files.append(str(f))
                elif os.path.isdir(f_path):
                    folders.append(str(f))
            else:
                print("no access to", f)
    files.sort(key=lambda v: v.lower())
    folders.sort(key=lambda v: v.lower())
    response={
        'id': 'directory',
        'path': os.path.join(params['path'],''),
        'shortcuts': shortcuts.keys(),
        'folders': folders,
        'files': files,
        'parent': os.path.abspath(os.path.join(params['path'],os.pardir))
    }
    
    #print('path info:', response)
    return response
Beispiel #14
0
def load_directory(toyz_settings, tid, params):
    """
    Used by the file browser to load the folders and files in a given path.
    
    Parameters
        - toyz_settings ( :py:class:`toyz.utils.core.ToyzSettings`): Settings for the toyz 
          application
        - tid (*string* ): Task ID of the client user running the task
        - params (*dict* ): Any parameters sent by the client (see *params* below)
    
    Params
        - path (*string* ): Path to search
    
    Response
        - id: 'directory'
        - path (*string* ): path passed to the function
        - shortcuts (*dict* ): Dictionary of ``shortcut_name: shortcut_path`` 's for the user
        - folders (*list* of strings): folders contained in the path
        - files (*list* of strings): files contained in the path
        - parent (*string* ): parent directory of current path
    """
    core.check4keys(params, ['path'])
    show_hidden = False
    # If the path is contained in a set of dollar signs (for example `$images$`) then
    # search in the users shortcuts for the given path
    shortcuts = db_utils.get_param(toyz_settings.db,
                                   'shortcuts',
                                   user_id=tid['user_id'])
    if params['path'][0] == '$' and params['path'][-1] == '$':
        shortcut = params['path'][1:-1]
        if shortcut not in shortcuts:
            raise ToyzJobError("Shortcut '{0}' not found for user {1}".format(
                shortcut, tid['user_id']))
        params['path'] = shortcuts[shortcut]

    if not os.path.isdir(params['path']):
        parent = os.path.dirname(params['path'])
        if not os.path.isdir(parent):
            raise ToyzJobError("Path '{0}' not found".format(params['path']))
        params['path'] = parent
    if 'show_hidden' in params and params['show_hidden']:
        show_hidden = True

    # Keep separate lists of the files and directories for the current path.
    # Only include the files and directories the user has permissions to view
    files = []
    folders = []
    groups = db_utils.get_param(toyz_settings.db,
                                'groups',
                                user_id=tid['user_id'])
    if 'admin' in groups or tid['user_id'] == 'admin':
        admin = True
    else:
        admin = False
    for f in os.listdir(params['path']):
        if (f[0] != '.' or show_hidden):
            f_path = os.path.join(params['path'], f)
            if admin:
                permission = True
            else:
                permissions = file_access.get_parent_permissions(
                    toyz_settings.db, f_path, user_id=tid['user_id'])
                if permissions is None:
                    permissions = ''
                permission = 'f' in permissions
            if permission:
                if os.path.isfile(f_path):
                    files.append(str(f))
                elif os.path.isdir(f_path):
                    folders.append(str(f))
            else:
                print("no access to", f)
    files.sort(key=lambda v: v.lower())
    folders.sort(key=lambda v: v.lower())
    response = {
        'id': 'directory',
        'path': os.path.join(params['path'], ''),
        'shortcuts': shortcuts.keys(),
        'folders': folders,
        'files': files,
        'parent': os.path.abspath(os.path.join(params['path'], os.pardir))
    }

    #print('path info:', response)
    return response