def marked_as_readed(ids): from file_management.helpers.check_role import get_email_in_jwt email = get_email_in_jwt() user = find_one_by_email(email) user_id = user.id notifications = get_notifications_by_ids(ids, user_id) return repositories.notification.marked_as_readed(notifications)
def create_folder(args): parent_id = args.get('parent_id') file_title = args.get('file_title') try: email = get_jwt_identity() user_id = find_one_by_email(email).id except Exception as e: _logger.error(e) raise UserNotFoundException() if not is_this_file_exists(parent_id): raise ParentFolderNotExistException() if check_duplicate_when_upload_or_create(parent_id, file_title): raise FolderNameUsed() file_id = helpers.generate_file_id(user_id) return insert.insert(file_id, file_title, 0, parent_id, user_id, "folder", "", "", starred=False)
def search(args): email = get_email_in_jwt() if email: args['user_id'] = find_one_by_email(email).id if args.get('user_id'): args['user_id'] = str(args['user_id']) file_id = args.get('file_id') if file_id: if isinstance(file_id, list): for id in file_id: viewable_check( id, error_message="You are not allowed to view this file/folder" ) else: viewable_check( file_id, error_message="You are not allowed to view this file/folder") file_es = FileElasticRepo() response = file_es.search(args) response = extract_file_data_from_response(response) add_user_name_to_files(response['result']['files']) return response
def move2trash(file_ids=None): """ Move files to trash """ email = get_email_in_jwt() user_id = str(find_one_by_email(email).id) if not isinstance(file_ids, list): return "Only accept `list` datatype" if len(file_ids) == 0: return "Nothing to move!" parent_of_first_file = files.utils.get_file(file_ids[0]) parent_of_first_file = parent_of_first_file['parent_id'] deleting_file = [files.utils.get_file(file_id) for file_id in file_ids] for file in deleting_file: file_id = file.get("file_id") if file.get('parent_id') != parent_of_first_file: raise DiffParentException( "Can't move files which have different parents") user_permission = get_role_of_user(user_id=user_id, file_id=file_id) if not user_permission.get('is_owner'): raise PermissionException("You can't delete file of another user!") if file_id == user_id: raise PermissionException("You can't delete your home folder") for file_id in file_ids: move_one_file_to_trash(file_id) return True
def share(args): try: email = get_jwt_identity() args['user_id'] = str(find_one_by_email(email).id) except Exception as e: _logger.error(e) raise UserNotFoundException() file_id = args['file_id'] file = get_file(file_id) if not file: raise FileNotExistException() if (file['owner'] != args['user_id']): raise PermissionException("You are not the owner of this file/folder") if args.get('private'): share_mode = 0 return update.update(file_id, share_mode=share_mode, users_shared=[]).get('result') # private elif args.get('emails'): share_mode = 1 users_shared = [] for email in args['emails']: user_shared = find_one_by_email(email) if not user_shared: raise UserNotFoundException("User with email " + email + " not exist!!!") users_shared.append(user_shared.id) for user_id in users_shared: from file_management.services.notification import create_notification create_notification(owner=int(args.get('user_id')), viewed=False, user_id=user_id, file_id=file.get('file_id')) users_shared = [str(id) for id in users_shared] return update.update(file_id, share_mode=share_mode, users_shared=users_shared).get('result') # custom elif args.get('share_by_link'): share_mode = 2 return update.update(file_id, share_mode=share_mode, users_shared=[]).get('result') # public
def delete_all_file_in_trash(): email = get_email_in_jwt() id = find_one_by_email(email).id response = services.file.search({ "trash": True, 'user_id': str(id), 'basic_info': True, '_page': 1, '_limit': 10000 }) ids = [file.get('file_id') for file in response['result']['files']] drop_out(ids)
def viewable_check(file_id, error_message='You are not allowed to view this file!'): email = get_email_in_jwt() user_id = None if email: user_id = find_one_by_email(email).id user_id = str(user_id) if user_id else user_id permission = get_role_of_user_not_trashed(user_id, file_id) if permission.get('trashed'): raise FileDeletedException() if not permission['viewable']: raise PermissionException(error_message) return permission, {'user_id': user_id, 'email': email}
def move_files(file_ids, new_parent): try: email = get_jwt_identity() user_id = find_one_by_email(email).id except Exception as e: _logger.error(e) raise UserNotFoundException() check_insert_privilege(parent_id=new_parent, user_id=user_id) parent_of_first_file = files.utils.get_file(file_ids[0]) parent_of_first_file = parent_of_first_file['parent_id'] move_files = [files.utils.get_file(file_id) for file_id in file_ids] for file in move_files: if file.get('parent_id') != parent_of_first_file: raise DiffParentException( "Can't move files which have different parents") for file_id in file_ids: move_one_file(file_id, new_parent)
def drop_out(file_ids): """ Drop away files from ES """ email = get_jwt_identity() user_id = find_one_by_email(email).id user_id = str(user_id) for file_id in file_ids: permission = get_role_of_user(user_id, file_id) if not permission.get('is_owner'): raise PermissionException( "You can't delete this files since you are not their owner") if file_id == user_id: raise PermissionException("You can't delete your home folder") file_ids = get_descendants_of_list(file_ids) for file_id in file_ids: files.delete.delete(file_id)