コード例 #1
0
def execute_actions(actions: List[SyncAction]) -> None:
    client_logger_sync().info(f"Executing actions: {actions}")
    user = net_interface.get_user()
    devices = db.Device.get_by_user_id(user.user_id)
    device_ids = [device.device_id for device in devices]
    device_ids.remove(user.device_id)

    for action in actions:
        if action["rel_file_path"] in ["", "."]:
            dest_path = action["local_folder_path"]
        else:
            dest_path = server_paths.normalize_path(action["local_folder_path"], action["rel_file_path"])
        if action["action_type"] == gen_json.ACTION_DELETE[0]:
            # Because directory deletions are also handled as files, there is only one function, that tries what
            # function fits
            file_exchanges.remove_file_dir(dest_path)
        elif action["action_type"] == gen_json.ACTION_MOVE[0]:
            src_path = server_paths.normalize_path(action["local_folder_path"], action["rel_old_file_path"])
            file_exchanges.move(src_path, dest_path)
        elif action["action_type"] == gen_json.ACTION_PULL[0]:
            src_path = action["remote_abs_path"]
            if action["is_directory"]:
                file_exchanges.pull_dir(src_path, dest_path)
            else:
                file_exchanges.pull_file(src_path, dest_path)
        elif action["action_type"] == gen_json.ACTION_MKDIR[0]:
            file_exchanges.make_dirs(dest_path)
        else:
            raise KeyError(f"Unknown action type: {action['action_type']} in {action}")

        server_json.distribute_action(action, device_ids)

    if len(actions) > 0:
        notify_other_devices(user)
コード例 #2
0
ファイル: explorer.py プロジェクト: JulianSobott/OpenDrive
def build_content(mongo: PyMongo, path: str):
    abs_path = server_paths.normalize_path(server_paths.FOLDERS_ROOT, path)
    logger_web.debug(abs_path)
    files = os.listdir(abs_path)
    logger_web.debug(files)
    files = [
        File(name=f, path=server_paths.normalize_path(path, f)) for f in files
    ]
    File.objects.insert(files)
    Content = namedtuple("Content", "files ")
    content = Content(files)
    return content
コード例 #3
0
    def test_add_change_entry(self):
        path = server_paths.NormalizedPath("folder_1")
        server_json.add_folder(path)
        rel_file_path = server_paths.normalize_path("test.txt")

        server_json.add_change_entry(path, rel_file_path, gen_json.ACTION_PULL)
        folder_entry = gen_json.get_folder_entry(path)
        changes = folder_entry["changes"]
        self.assertEqual(1, len(changes))
コード例 #4
0
ファイル: __init__.py プロジェクト: JulianSobott/OpenDrive
 def _get_files(_abs_root: str, _rel_path: str,
                remaining_depth: int) -> List[Union[File, Folder]]:
     files = []
     _abs_path = server_paths.normalize_path(_abs_root, _rel_path)
     with os.scandir(_abs_path) as it:
         for f in it:
             file = File("No_UID_yet", f.name, _rel_path,
                         "No_parent_UID_yet", user, [], {})
             if f.is_file():
                 files.append(file)
             if f.is_dir():
                 if remaining_depth > 0:
                     rel = server_paths.normalize_path(_rel_path, f.name)
                     folder = Folder(
                         file,
                         _get_files(_abs_root, rel, remaining_depth - 1))
                 else:
                     folder = Folder(file, [], files_not_listed=True)
                 files.append(folder)
     return files
コード例 #5
0
ファイル: __init__.py プロジェクト: JulianSobott/OpenDrive
def get_files_by_user(user: User, rel_path: str, depth: int = 0, max_files: int = 999) \
        -> Status[List[Union[File, Folder]]]:
    """Get a list of files from a user.

    The query starts at rel_path.
    The bigger the depth the less often this function needs to be called, when the filesystem is traversed, but
    the data can become very huge. Default depth is 0, meaning that no sub-folders are opened.

    :param user: The user for which the files are requested
    :param rel_path: The path where the query should start. Root folder is /
    :param depth: How deep the sub-folders are queried.
    :param max_files:
    :return: A list of files and folders
    """
    def _get_files(_abs_root: str, _rel_path: str,
                   remaining_depth: int) -> List[Union[File, Folder]]:
        files = []
        _abs_path = server_paths.normalize_path(_abs_root, _rel_path)
        with os.scandir(_abs_path) as it:
            for f in it:
                file = File("No_UID_yet", f.name, _rel_path,
                            "No_parent_UID_yet", user, [], {})
                if f.is_file():
                    files.append(file)
                if f.is_dir():
                    if remaining_depth > 0:
                        rel = server_paths.normalize_path(_rel_path, f.name)
                        folder = Folder(
                            file,
                            _get_files(_abs_root, rel, remaining_depth - 1))
                    else:
                        folder = Folder(file, [], files_not_listed=True)
                    files.append(folder)
        return files

    rel_path = "." if rel_path == "/" else rel_path  # needed when joining
    abs_root = server_paths.get_users_root_folder(user)
    abs_path = server_paths.normalize_path(abs_root, rel_path)
    if not os.path.exists(abs_path):
        return Status.fail(ERR_FILE_NOT_EXISTS)
    if not os.path.isdir(abs_path):
        return Status.fail(ERR_NOT_A_FOLDER)
    final_files = _get_files(abs_root, rel_path, remaining_depth=depth)
    return Status.success(final_files)
コード例 #6
0
def _get_file_path(user_id: int, device_id: int) -> str:
    user_path = path_utils.get_users_root_folder(user_id)
    file_path = server_paths.normalize_path(user_path,
                                            get_file_name(device_id))
    return file_path
コード例 #7
0
def get_changes(dest_path: str) -> net.File:
    user = net_interface.get_user()
    user_path = path_utils.get_users_root_folder(user.user_id)
    changes_path = server_paths.normalize_path(user_path, f"changes_{user.device_id}.json")
    return net.File(changes_path, dest_path)
コード例 #8
0
 def test_remove_folder_not_existing(self):
     path = server_paths.normalize_path("p1")
     self.assertRaises(KeyError,
                       server_json.remove_folder,
                       path,
                       non_exists_ok=False)
コード例 #9
0
def get_users_root_folder(user_id: int) -> str:
    return paths.normalize_path(paths.FOLDERS_ROOT, f"user_{user_id}/")
コード例 #10
0
def rel_user_path_to_abs(rel_path: str, user_id: int = -1):
    if user_id == -1:
        user_id = net_interface.get_user_id()
    return paths.normalize_path(get_users_root_folder(user_id), rel_path)
コード例 #11
0
ファイル: folders.py プロジェクト: JulianSobott/OpenDrive
def _create_physical_folder(user_id: int, folder_name: str):
    """Creates a new folder at the harddrive."""
    users_root = path_utils.get_users_root_folder(user_id)
    new_folder_path = paths.normalize_path(users_root, folder_name)
    os.mkdir(new_folder_path)