fix history save bug
This commit is contained in:
@@ -1,36 +1,34 @@
|
||||
from flask import Blueprint, request, jsonify, session
|
||||
import os
|
||||
from hmac import new
|
||||
import shutil
|
||||
from flask import current_app
|
||||
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')
|
||||
|
||||
# ===== 永久调试:写死 session 值 =====
|
||||
@file_bp.route('/create-folder', methods=['POST'])
|
||||
def create_folder():
|
||||
session['path'] = 'algotest/8080/lesson'
|
||||
session['user_id'] = 'test'
|
||||
try:
|
||||
filepath = request.json.get('path', '')
|
||||
path = session.get('path')
|
||||
parent = request.json.get('parent', '')
|
||||
base_path = f'/tmp/{session.get("user_id")}/{path}'
|
||||
root_workspace_path = current_app.config['ROOT_WORKSPACE_PATH']
|
||||
base_path = f'{root_workspace_path}/{path}'
|
||||
filepath = f'{base_path}/{parent}/{filepath}'
|
||||
os.makedirs(filepath, exist_ok=True)
|
||||
return jsonify({"message": "Folder created successfully"})
|
||||
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():
|
||||
session['path'] = 'algotest/8080/lesson'
|
||||
session['user_id'] = 'test'
|
||||
filepath = request.json.get('path', '')
|
||||
path = session.get('path')
|
||||
parent = request.json.get('parent', '')
|
||||
base_path = f'/tmp/{session.get("user_id")}/{path}'
|
||||
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()
|
||||
@@ -38,15 +36,14 @@ def create_file():
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
|
||||
@file_bp.route('/rename-file', methods=['POST'])
|
||||
def rename_file():
|
||||
session['path'] = 'algotest/8080/lesson'
|
||||
session['user_id'] = 'test'
|
||||
user_root = f'/tmp/{session.get("user_id")}/{session.get("path")}'
|
||||
user_root = session.get('path')
|
||||
path = request.json.get('path', '')
|
||||
new_name = request.json.get('newName', '')
|
||||
filepath = f'{user_root}/{path}'
|
||||
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
|
||||
@@ -56,15 +53,14 @@ def rename_file():
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
|
||||
@file_bp.route('/rename-folder', methods=['POST'])
|
||||
def rename_folder():
|
||||
session['path'] = 'algotest/8080/lesson'
|
||||
session['user_id'] = 'test'
|
||||
user_root = f'/tmp/{session.get("user_id")}/{session.get("path")}'
|
||||
user_root = session.get('path')
|
||||
path = request.json.get('path', '')
|
||||
new_name = request.json.get('newName', '')
|
||||
folderpath = f'{user_root}/{path}'
|
||||
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
|
||||
@@ -74,46 +70,59 @@ def rename_folder():
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
|
||||
@file_bp.route('/copy-files', methods=['POST'])
|
||||
def copy_files():
|
||||
session['path'] = 'algotest/8080/lesson'
|
||||
session['user_id'] = 'test'
|
||||
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)
|
||||
final_new_path = os.path.join(new_path, old_name)
|
||||
|
||||
# 确保目标路径是父目录(即 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, f"{old_name}_{counter}")
|
||||
# 如果文件夹或文件已经存在,则为新的文件名添加计数后缀
|
||||
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():
|
||||
session['path'] = 'algotest/8080/lesson'
|
||||
session['user_id'] = 'test'
|
||||
path = request.json.get('path', '')
|
||||
user_root = f'/tmp/{session.get("user_id")}/{session.get("path")}'
|
||||
filepath = f'{user_root}/{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():
|
||||
session['path'] = 'algotest/8080/lesson'
|
||||
session['user_id'] = 'test'
|
||||
user_root = f'/tmp/{session.get("user_id")}/{session.get("path")}'
|
||||
filepath = f'{user_root}/{request.json.get("path", "")}'
|
||||
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:
|
||||
@@ -123,27 +132,34 @@ def file_content():
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
|
||||
@file_bp.route('/save-file', methods=['POST'])
|
||||
def save_file():
|
||||
session['path'] = 'algotest/8080/lesson'
|
||||
session['user_id'] = 'test'
|
||||
user_root = f'/tmp/{session.get("user_id")}/{session.get("path")}'
|
||||
filepath = f'{user_root}/{request.json.get("path", "")}'
|
||||
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', '')
|
||||
# 文件不存在就创建
|
||||
os.makedirs(os.path.dirname(filepath), exist_ok=True)
|
||||
with open(filepath, 'w', encoding='utf-8') as file:
|
||||
file.write(content)
|
||||
return jsonify({"message": "File saved successfully"})
|
||||
|
||||
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():
|
||||
session['path'] = 'algotest/8080/lesson'
|
||||
session['user_id'] = 'test'
|
||||
user_root = f'/tmp/{session.get("user_id")}/{session.get("path")}'
|
||||
# 确保目录存在
|
||||
os.makedirs(user_root, exist_ok=True)
|
||||
# 返回空列表,前端不再报错
|
||||
return jsonify([])
|
||||
path = session.get('path')
|
||||
user_id = session.get('user_id')
|
||||
if not path:
|
||||
return jsonify({"error": "Missing path parameter"}), 400
|
||||
root_workspace_path = current_app.config['ROOT_WORKSPACE_PATH']
|
||||
base_path = os.path.join(root_workspace_path, user_id, path)
|
||||
|
||||
print(base_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