예제 #1
0
def get_role_of_user(user_id, file_id):
    from file_management.repositories.files import es
    cur_id = file_id
    if user_id:
        user_id = str(user_id)
    viewable = False
    editable = False
    is_owner = False
    while es.exists(index=FILES_INDEX, id=cur_id):
        cur_file = es.get_source(index=FILES_INDEX, id=cur_id)
        if (cur_file['owner'] == user_id):
            is_owner = True
        if cur_file['share_mode'] == 1 and user_id in cur_file['users_shared']:
            viewable = True
            if cur_file['editable'] == True:
                editable = True
        if cur_file['share_mode'] == 2:
            viewable = True
        if is_owner: break

        cur_id = cur_file['parent_id']
    if is_owner:
        viewable = True
        editable = True
    return {'is_owner': is_owner, 'viewable': viewable, 'editable': editable}
예제 #2
0
def get_descendants(file_id):
    descendants = []
    from file_management.repositories.files import es
    children = es.get_source(index=FILES_INDEX, id=file_id)['children_id']
    descendants += children
    for child in children:
        descendants += get_descendants(child)
    return descendants
예제 #3
0
def get_ancestors(file_id):
    from file_management.repositories.files import es
    ancestors = []
    cur_id = file_id
    while es.exists(index=FILES_INDEX, id=cur_id):
        ancestors.append(cur_id)
        cur_id = es.get_source(index=FILES_INDEX, id=cur_id)['parent_id']
    ancestors.append(cur_id)
    ancestors.append(pathconst.FAKE_HDD)
    return ancestors[::-1]
예제 #4
0
def update(file_id, **kwargs):
    from datetime import datetime
    update_body = {}
    fields = [
        'file_title', 'star', 'parent_id', 'share_mode', 'editable',
        'users_shared', 'children_id', 'description', 'file_tag', 'trashed'
    ]
    from file_management.repositories.files import es
    if not es.exists(index=FILES_INDEX, id=file_id):
        return True
    file = es.get_source(index=FILES_INDEX, id=file_id)
    for field in fields:
        if field in kwargs:
            new_value = kwargs.get(field)
            old_value = file[field]

            if isinstance(new_value, list):
                new_value.sort()
                old_value.sort()

            if new_value != old_value:
                if field == 'parent_id':
                    """
                    If file is moved (i.e: changed parend_id), update size of old ancestors and new ancestors
                    """
                    old_ancestors = set(get_ancestors(old_value))
                    new_ancestors = set(get_ancestors(new_value))
                    negotiate_size = list(old_ancestors.difference(new_value))
                    advance_size = list(new_ancestors.difference(old_value))

                    update_size(negotiate_size,
                                -1 * file['size'])  # Update old ancestors
                    update_size(advance_size,
                                file['size'])  # Update new ancestors
                if field == 'trashed':
                    """
                    This file is moved/restored to/from trash, update size for it's ancestors
                    """
                    ancestors = get_ancestors(file['parent_id'])
                    sign = -1 if new_value else 1
                    update_body[field] = datetime.now()
                    update_size(ancestors, sign * file['size'])
                update_body[field] = new_value
                update_body['updated_at'] = datetime.now().strftime(
                    "%d/%m/%Y %I:%M:%S %p")
    es.indices.refresh(index=FILES_INDEX)
    res = es.update(index=FILES_INDEX, id=file_id, body={"doc": update_body})
    return res
예제 #5
0
def get_parse_url(folder_id, user_id):
    from file_management.repositories.files import es
    pending_urls = []
    parse_urls = []
    cur_id = folder_id
    if user_id:
        user_id = str(user_id)

    while es.exists(index=FILES_INDEX, id=cur_id):
        cur_file = es.get_source(index=FILES_INDEX, id=cur_id)
        pending_urls.append({
            'id': cur_id,
            'title': cur_file.get('file_title')
        })
        viewable = False
        viewable |= cur_file['owner'] == user_id
        viewable |= cur_file['share_mode'] == 1 and user_id in cur_file[
            'children_id']
        viewable |= cur_file['share_mode'] == 2
        if viewable:
            parse_urls += pending_urls
            pending_urls.clear()
        cur_id = cur_file['parent_id']
    return parse_urls[::-1]
예제 #6
0
def get_file(file_id):
    from file_management.repositories.files import es
    if es.exists(index=FILES_INDEX, id=file_id):
        return es.get_source(index=FILES_INDEX, id=file_id)
    else:
        return None
예제 #7
0
def insert(file_id,
           file_title,
           file_size,
           parent_id,
           user_id,
           mime_type,
           tags,
           thumbnail_url,
           starred=False,
           children_id=[],
           created_at=None,
           updated_at=None):
    current = datetime.datetime.now().strftime("%d/%m/%Y %I:%M:%S %p")
    created_at = current if not created_at else created_at
    updated_at = current if not updated_at else updated_at
    document = {
        "file_id": file_id,
        "file_title": file_title,
        "size": file_size,
        "file_type": mime_type,
        "owner": str(user_id),
        "star": starred,
        "parent_id": parent_id,
        "thumbnail_url": thumbnail_url,
        "children_id": children_id,
        "share_mode": 0,
        "editable": False,
        "users_shared": [],
        "created_at": created_at,
        "updated_at": updated_at,
        "file_tag": tags,
        "description": "",
        "trashed": False
    }

    from file_management.repositories.files import es
    res = es.index(index=FILES_INDEX, body=document, id=file_id)
    ancestors = get_ancestors(parent_id)

    if es.exists(index=FILES_INDEX, id=parent_id):
        parent = es.get_source(index=FILES_INDEX, id=parent_id)
        update(file_id=parent_id,
               children_id=parent['children_id'] + [file_id])

    es.indices.refresh(index=FILES_INDEX)
    es.update_by_query(index=FILES_INDEX,
                       body={
                           "query": {
                               "terms": {
                                   "file_id": ancestors[1:]
                               }
                           },
                           "script": {
                               "lang": "painless",
                               "source": "ctx._source.size += params.capacity",
                               "params": {
                                   "capacity": file_size
                               }
                           }
                       })
    return res