code-server-like
This commit is contained in:
164
code-server-like/app/views/file.py
Normal file
164
code-server-like/app/views/file.py
Normal file
@@ -0,0 +1,164 @@
|
||||
from hmac import new
|
||||
import shutil
|
||||
from flask import Blueprint, request, jsonify, current_app, render_template, session
|
||||
import os
|
||||
from ..services.file_service import get_file_tree
|
||||
|
||||
file_bp = Blueprint('file', __name__, url_prefix='/vsc-like')
|
||||
|
||||
@file_bp.route('/create-folder', methods=['POST'])
|
||||
def create_folder():
|
||||
try:
|
||||
filepath = request.json.get('path', '')
|
||||
path = session.get('path')
|
||||
parent = request.json.get('parent', '')
|
||||
root_workspace_path = current_app.config['ROOT_WORKSPACE_PATH']
|
||||
base_path = f'{root_workspace_path}/{path}'
|
||||
filepath = f'{base_path}/{parent}/{filepath}'
|
||||
try:
|
||||
os.makedirs(filepath)
|
||||
return jsonify({"message": "Folder created successfully"})
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
@file_bp.route('/create-file', methods=['POST'])
|
||||
def create_file():
|
||||
filepath = request.json.get('path', '')
|
||||
path = session.get('path')
|
||||
parent = request.json.get('parent', '')
|
||||
root_workspace_path = current_app.config['ROOT_WORKSPACE_PATH']
|
||||
base_path = f'{root_workspace_path}/{path}'
|
||||
filepath = f'{base_path}/{parent}/{filepath}'
|
||||
try:
|
||||
open(filepath, 'w').close()
|
||||
return jsonify({"message": "File created successfully"})
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
@file_bp.route('/rename-file', methods=['POST'])
|
||||
def rename_file():
|
||||
user_root = session.get('path')
|
||||
path = request.json.get('path', '')
|
||||
new_name = request.json.get('newName', '')
|
||||
root_workspace_path = current_app.config['ROOT_WORKSPACE_PATH']
|
||||
base_path = f'{root_workspace_path}/{user_root}'
|
||||
filepath = f'{base_path}/{path}'
|
||||
try:
|
||||
path_list = filepath.split('/')
|
||||
path_list[-1] = new_name
|
||||
new_filepath = '/'.join(path_list)
|
||||
os.rename(filepath, new_filepath)
|
||||
return jsonify({"message": "File renamed successfully"})
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
@file_bp.route('/rename-folder', methods=['POST'])
|
||||
def rename_folder():
|
||||
user_root = session.get('path')
|
||||
path = request.json.get('path', '')
|
||||
new_name = request.json.get('newName', '')
|
||||
root_workspace_path = current_app.config['ROOT_WORKSPACE_PATH']
|
||||
base_path = f'{root_workspace_path}/{user_root}'
|
||||
folderpath = f'{base_path}/{path}'
|
||||
try:
|
||||
path_list = folderpath.split('/')
|
||||
path_list[-1] = new_name
|
||||
new_filepath = '/'.join(path_list)
|
||||
os.rename(folderpath, new_filepath)
|
||||
return jsonify({"message": "Folder renamed successfully"})
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
@file_bp.route('/copy-files', methods=['POST'])
|
||||
def copy_files():
|
||||
old_path = request.json.get('oldPath', '')
|
||||
new_path = request.json.get('newPath', '')
|
||||
root_workspace_path = current_app.config['ROOT_WORKSPACE_PATH']
|
||||
user_root = session.get('path')
|
||||
base_path = f'{root_workspace_path}/{user_root}'
|
||||
old_path = f'{base_path}/{old_path}'
|
||||
new_path = f'{base_path}/{new_path}'
|
||||
# oldpath的父目录就是newpath的话,对oldpath进行重命名
|
||||
|
||||
# 获取 old_path 的文件/文件夹名
|
||||
old_name = os.path.basename(old_path)
|
||||
|
||||
# 确保目标路径是父目录(即 new_path 是一个目录)
|
||||
new_path_parent = os.path.abspath(new_path)
|
||||
|
||||
# 如果目标路径下已经存在同名文件/文件夹,进行重命名
|
||||
final_new_path = os.path.join(new_path_parent, old_name)
|
||||
counter = 1
|
||||
while os.path.exists(final_new_path):
|
||||
# 如果文件夹或文件已经存在,则为新的文件名添加计数后缀
|
||||
final_new_path = os.path.join(new_path_parent, f"{old_name}_{counter}")
|
||||
counter += 1
|
||||
|
||||
# 判断 old_path 是文件夹还是文件,并进行相应的复制操作
|
||||
if os.path.isdir(old_path):
|
||||
shutil.copytree(old_path, final_new_path, dirs_exist_ok=True)
|
||||
else:
|
||||
shutil.copy(old_path, final_new_path)
|
||||
|
||||
return jsonify({"message": "Files copied successfully"})
|
||||
|
||||
@file_bp.route('/delete-file', methods=['POST'])
|
||||
def delete_file():
|
||||
path = request.json.get('path', '')
|
||||
root_workspace_path = current_app.config['ROOT_WORKSPACE_PATH']
|
||||
user_root = session.get('path')
|
||||
base_path = f'{root_workspace_path}/{user_root}'
|
||||
filepath = f'{base_path}/{path}'
|
||||
if os.path.isdir(filepath):
|
||||
shutil.rmtree(filepath)
|
||||
else:
|
||||
os.remove(filepath)
|
||||
return jsonify({"message": "File deleted successfully"})
|
||||
|
||||
@file_bp.route('/file-content', methods=['POST'])
|
||||
def file_content():
|
||||
path = session.get('path')
|
||||
root_workspace_path = current_app.config['ROOT_WORKSPACE_PATH']
|
||||
base_path = f'{root_workspace_path}/{path}'
|
||||
filepath = request.json.get('path', '')
|
||||
filepath = f'{base_path}/{filepath}'
|
||||
if not os.path.exists(filepath):
|
||||
return jsonify({"error": "File not found"}), 404
|
||||
try:
|
||||
with open(filepath, 'r') as file:
|
||||
content = file.read()
|
||||
return jsonify({"content": content})
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
@file_bp.route('/save-file', methods=['POST'])
|
||||
def save_file():
|
||||
path = session.get('path')
|
||||
root_workspace_path = current_app.config['ROOT_WORKSPACE_PATH']
|
||||
base_path = f'{root_workspace_path}/{path}'
|
||||
filepath = request.json.get('path', '')
|
||||
filepath = f'{base_path}/{filepath}'
|
||||
content = request.json.get('content', '')
|
||||
if not os.path.exists(filepath):
|
||||
return jsonify({"error": "File not found"}), 404
|
||||
try:
|
||||
with open(filepath, 'w') as file:
|
||||
file.write(content)
|
||||
return jsonify({"message": "File saved successfully"})
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
@file_bp.route('/file-tree', methods=['GET'])
|
||||
def file_tree():
|
||||
path = session.get('path')
|
||||
if not path:
|
||||
return jsonify({"error": "Missing path parameter"}), 400
|
||||
root_workspace_path = current_app.config['ROOT_WORKSPACE_PATH']
|
||||
base_path = f'{root_workspace_path}/{path}'
|
||||
if not os.path.exists(base_path):
|
||||
return jsonify({"error": "Workspace not found"}), 404
|
||||
tree = get_file_tree(base_path)
|
||||
return jsonify(tree)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user