Ejemplo n.º 1
0
    def filemanager_rename():
        old_path = request.args.get("old_path")
        new_path = request.args.get("new_path")
        old_root = request.args.get("old_root")
        new_root = request.args.get("new_root")
        project_uuid = request.args.get("project_uuid")

        try:
            old_root_path, _ = process_request(root=old_root,
                                               path=new_path,
                                               project_uuid=project_uuid)
            new_root_path, _ = process_request(root=new_root,
                                               path=new_path,
                                               project_uuid=project_uuid)
        except Exception as e:
            return jsonify({"message": str(e)}), 400

        abs_old_path = safe_join(old_root_path, old_path[1:])
        abs_new_path = safe_join(new_root_path, new_path[1:])

        try:
            os.rename(abs_old_path, abs_new_path)
            return jsonify({"message": "Success"})
        except Exception:
            return jsonify({"message": "Failed to rename"}), 500
Ejemplo n.º 2
0
    def filemanager_create():
        """
        Create an empty file with the given path within `/project-dir`
        or `/data`.
        """
        root = request.args.get("root")
        path = request.args.get("path")
        project_uuid = request.args.get("project_uuid")

        try:
            root_dir_path, _ = process_request(root=root,
                                               path=path,
                                               project_uuid=project_uuid)
        except Exception as e:
            return jsonify({"message": str(e)}), 400

        file_path = safe_join(root_dir_path, path[1:])

        if not file_path.split(".")[-1] in _config.ALLOWED_FILE_EXTENSIONS:
            return jsonify({"message":
                            "Given file type is not supported."}), 409

        directory, _ = os.path.split(file_path)

        if directory:
            os.makedirs(directory, exist_ok=True)

        if os.path.isfile(file_path):
            return jsonify({"message": "File already exists."}), 409
        try:
            create_empty_file(file_path)
            return jsonify({"message": "File created."})
        except IOError as e:
            app.logger.error(
                f"Could not create file at {file_path}. Error: {e}")
Ejemplo n.º 3
0
    def filemanager_extension_search():
        root = request.args.get("root")
        path = request.args.get("path")
        project_uuid = request.args.get("project_uuid")
        extensions = request.args.get("extensions")

        try:
            root_dir_path, _ = process_request(root=root,
                                               path=path,
                                               project_uuid=project_uuid)
        except Exception as e:
            return jsonify({"message": str(e)}), 400

        if extensions is None:
            return jsonify({"message": "extensions is required."}), 400

        path_filter = path

        extensions = extensions.split(",")

        # Make absolute path relative
        path_filter = path_filter[1:]
        app.logger.info(f"Path filter {path_filter}")

        matches = []

        for extension in extensions:
            matches += list(
                pathlib.Path(safe_join(root_dir_path, path_filter)).glob(
                    "**/*.{}".format(extension)))

        return jsonify({
            "files":
            [os.path.relpath(str(match), root_dir_path) for match in matches]
        })
Ejemplo n.º 4
0
    def filemanager_download():
        root = request.args.get("root")
        path = request.args.get("path")
        project_uuid = request.args.get("project_uuid")

        try:
            root_dir_path, _ = process_request(root=root,
                                               path=path,
                                               project_uuid=project_uuid)
        except Exception as e:
            return jsonify({"message": str(e)}), 400

        target_path = safe_join(root_dir_path, path[1:])

        if os.path.isfile(target_path):
            return send_file(target_path, as_attachment=True)
        else:
            memory_file = io.BytesIO()
            with zipfile.ZipFile(memory_file, "w", zipfile.ZIP_STORED) as zf:
                zipdir(target_path, zf)
            memory_file.seek(0)
            return send_file(
                memory_file,
                mimetype="application/zip",
                as_attachment=True,
                attachment_filename=os.path.basename(target_path[:-1]) +
                ".zip",
            )
Ejemplo n.º 5
0
    def filemanager_upload():
        root = request.args.get("root")
        path = request.args.get("path")
        project_uuid = request.args.get("project_uuid")

        try:
            root_dir_path, _ = process_request(root=root,
                                               path=path,
                                               project_uuid=project_uuid)
        except Exception as e:
            return jsonify({"message": str(e)}), 400

        app.logger.debug(path)

        # check if the post request has the file part
        if "file" not in request.files or request.files["file"].filename == "":
            return jsonify({"message": "No file found"}), 500

        file = request.files["file"]
        if file and allowed_file(file.filename):
            filename = file.filename.split(os.sep)[-1]
            # Trim path for joining (up until this point paths always
            # start and end with a "/")
            path = path[1:]
            dir_path = safe_join(root_dir_path, path)
            # Create directory if it doesn't exist
            if not os.path.isdir(dir_path):
                os.makedirs(dir_path, exist_ok=True)
            file_path = safe_join(dir_path, filename)
            file.save(file_path)

        return jsonify({"file_path": file_path})
Ejemplo n.º 6
0
    def filemanager_duplicate():
        root = request.args.get("root")
        path = request.args.get("path")
        project_uuid = request.args.get("project_uuid")

        try:
            root_dir_path, _ = process_request(root=root,
                                               path=path,
                                               project_uuid=project_uuid)
        except Exception as e:
            return jsonify({"message": str(e)}), 400

        # Make absolute path relative
        target_path = safe_join(root_dir_path, path[1:])

        if os.path.isfile(target_path) or os.path.isdir(target_path):
            new_path = find_unique_duplicate_filepath(target_path)
            try:
                if os.path.isfile(target_path):
                    copytree(target_path, new_path)
                else:
                    copytree(target_path, new_path, use_gitignore=False)
            except Exception as e:
                app.logger.error(e)
                return jsonify({"message":
                                "Copy of file/directory failed"}), 500
        else:
            return jsonify(
                {"message": "No file or directory at path %s" % path}), 500

        return jsonify({"message": "Success"})
Ejemplo n.º 7
0
    def filemanager_delete():
        root = request.args.get("root")
        path = request.args.get("path")
        project_uuid = request.args.get("project_uuid")

        try:
            root_dir_path, _ = process_request(root=root,
                                               path=path,
                                               project_uuid=project_uuid)
        except Exception as e:
            return jsonify({"message": str(e)}), 400

        # Make absolute path relative
        target_path = safe_join(root_dir_path, path[1:])

        if target_path == root_dir_path:
            return (
                jsonify({
                    "message": ("It is not allowed to delete roots "
                                "through the file-manager.")
                }),
                403,
            )

        if os.path.exists(target_path):
            try:
                rmtree(target_path)
            except Exception:
                return jsonify({"message": "Deletion failed."}), 500
        else:
            return jsonify(
                {"message": "No file or directory at path %s" % path}), 500

        return jsonify({"message": "Success"})
Ejemplo n.º 8
0
    def browse_files():
        root = request.args.get("root")
        path = request.args.get("path")
        depth_as_string = request.args.get("depth")
        project_uuid = request.args.get("project_uuid")

        try:
            root_dir_path, depth = process_request(
                root=root,
                path=path,
                project_uuid=project_uuid,
                depth=depth_as_string,
                is_path_required=False,
            )
        except Exception as e:
            return jsonify({"message": str(e)}), 400

        # Path
        path_filter = path if path else "/"

        app.logger.info(f"Path filter {path_filter}")

        return jsonify(
            generate_tree(root_dir_path, path_filter=path_filter, depth=depth))
Ejemplo n.º 9
0
    def filemanager_create_dir():
        root = request.args.get("root")
        path = request.args.get("path")
        project_uuid = request.args.get("project_uuid")

        try:
            root_dir_path, _ = process_request(root=root,
                                               path=path,
                                               project_uuid=project_uuid)
        except Exception as e:
            return jsonify({"message": str(e)}), 400

        # Make absolute path relative
        path = "/".join(path.split("/")[1:])

        full_path = safe_join(root_dir_path, path)

        if os.path.isdir(full_path) or os.path.isfile(full_path):
            return jsonify({"message": "Path already exists"}), 500

        # even if name ends like an extension, e.g. "my-folder.txt"
        # it will be seen as a folder name
        os.makedirs(full_path, exist_ok=True)
        return jsonify({"message": "Success"})