Example #1
0
def files(path):
    try:
        mode_download = get_url_param(request.args, "mode") == "download"

        path = "/" + path  # Add the pre fix / to the path, cuz all the rel path have a / in the beginning

        if os.name == "nt":
            path = path.replace("/", "\\")

        file = CommonQuery.query_file_by_relative_path(path)

        if not file:
            return render_template("error/404.html")

        if mode_download:
            return send_file(file.abs_path,
                             attachment_filename=file.name,
                             as_attachment=True)
        else:
            return send_file(file.abs_path,
                             mimetype=file.mimetype,
                             conditional=True)

    except (PermissionError, FileNotFoundError):
        return render_template("error/404.html")
Example #2
0
def delete_file_or_folder():
    content = request.json

    targets = []

    for path in content["folder"]:
        if os.name == "nt": path = path.replace("/", "\\")
        folder = CommonQuery.query_dir_by_relative_path(path)
        if not folder:
            return utils.make_status_resp(
                103, f"Folder with path: {path} does not exist",
                STATUS_TO_HTTP_CODE[103])
        targets.append(folder)

    for path in content["file"]:
        if os.name == "nt": path = path.replace("/", "\\")
        file = CommonQuery.query_file_by_relative_path(path)
        if not file:
            return utils.make_status_resp(
                103, f"File with path: {path} does not exist",
                STATUS_TO_HTTP_CODE[103])
        targets.append(file)

    failed_to_delete = [
    ]  # A list to store list of relative path if os.removed failed to remove them
    failed_to_delete_reason = None

    for target in targets:
        if app.config[
                "DELETE_MODE"] == 1:  # Remove the target both from the file system and the database
            try:
                api_utils.delete_file_or_directory_from_filesystem(target)
                api_utils.delete_file_or_directory_from_db(target)
            except PermissionError:
                failed_to_delete.append(target.rel_path)
                failed_to_delete_reason = 101
                continue

        elif app.config[
                "DELETE_MODE"] == 2:  # Only Remove the target from the database not the filesystem
            api_utils.delete_file_or_directory_from_db(target)

    db.session.commit()

    if failed_to_delete:
        return utils.make_status_resp(
            0,
            f"Errors [{STATUS_TO_MESSAGE[failed_to_delete_reason]}] has prevented some file from being deleted. A total of {len(failed_to_delete)} items out of {len(targets)} failed to be deleted",
            STATUS_TO_HTTP_CODE[0])

    return utils.make_status_resp_ex(STATUS_ENUM.SUCCESS)
Example #3
0
def db_list_directory_basic(record: Directory) -> dict:
    content = {"dirs": [], "files": []}
    if record.content_dir:
        for folder in record.content_dir.split("\0"):
            dir_rel_path = os.path.join(record.rel_path, folder)
            dir_info = CommonQuery.query_dir_by_relative_path(dir_rel_path)

            content["dirs"].append({
                "name": dir_info.name,
                "path": dir_info.rel_path.replace("\\", "/"),
                "size": dir_info.size,
                "last_mod": dir_info.last_mod,
                "dir_count": dir_info.dir_count,
                "file_count": dir_info.file_count
            })

    if record.content_file:
        for file in record.content_file.split("\0"):
            file_rel_path = os.path.join(record.rel_path, file)
            file_info = CommonQuery.query_file_by_relative_path(file_rel_path)

            content["files"].append({
                "name":
                file_info.name,
                "path":
                file_info.rel_path.replace("\\", "/"),
                "size":
                file_info.size,
                "last_mod":
                file_info.last_mod,
                "mimetype":
                file_info.mimetype
            })

    parent_path = record.rel_path.replace("\\", "/")
    return {parent_path: content}