fix history save bug
This commit is contained in:
@@ -1,36 +1,34 @@
|
|||||||
from flask import Blueprint, request, jsonify, session
|
from hmac import new
|
||||||
import os
|
|
||||||
import shutil
|
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
|
from ..services.file_service import get_file_tree
|
||||||
|
|
||||||
file_bp = Blueprint('file', __name__, url_prefix='/vsc-like')
|
file_bp = Blueprint('file', __name__, url_prefix='/vsc-like')
|
||||||
|
|
||||||
# ===== 永久调试:写死 session 值 =====
|
|
||||||
@file_bp.route('/create-folder', methods=['POST'])
|
@file_bp.route('/create-folder', methods=['POST'])
|
||||||
def create_folder():
|
def create_folder():
|
||||||
session['path'] = 'algotest/8080/lesson'
|
|
||||||
session['user_id'] = 'test'
|
|
||||||
try:
|
try:
|
||||||
filepath = request.json.get('path', '')
|
filepath = request.json.get('path', '')
|
||||||
path = session.get('path')
|
path = session.get('path')
|
||||||
parent = request.json.get('parent', '')
|
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}'
|
filepath = f'{base_path}/{parent}/{filepath}'
|
||||||
os.makedirs(filepath, exist_ok=True)
|
try:
|
||||||
return jsonify({"message": "Folder created successfully"})
|
os.makedirs(filepath)
|
||||||
|
return jsonify({"message": "Folder created successfully"})
|
||||||
|
except Exception as e:
|
||||||
|
return jsonify({"error": str(e)}), 500
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return jsonify({"error": str(e)}), 500
|
return jsonify({"error": str(e)}), 500
|
||||||
|
|
||||||
|
|
||||||
@file_bp.route('/create-file', methods=['POST'])
|
@file_bp.route('/create-file', methods=['POST'])
|
||||||
def create_file():
|
def create_file():
|
||||||
session['path'] = 'algotest/8080/lesson'
|
|
||||||
session['user_id'] = 'test'
|
|
||||||
filepath = request.json.get('path', '')
|
filepath = request.json.get('path', '')
|
||||||
path = session.get('path')
|
path = session.get('path')
|
||||||
parent = request.json.get('parent', '')
|
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}'
|
filepath = f'{base_path}/{parent}/{filepath}'
|
||||||
try:
|
try:
|
||||||
open(filepath, 'w').close()
|
open(filepath, 'w').close()
|
||||||
@@ -38,15 +36,14 @@ def create_file():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
return jsonify({"error": str(e)}), 500
|
return jsonify({"error": str(e)}), 500
|
||||||
|
|
||||||
|
|
||||||
@file_bp.route('/rename-file', methods=['POST'])
|
@file_bp.route('/rename-file', methods=['POST'])
|
||||||
def rename_file():
|
def rename_file():
|
||||||
session['path'] = 'algotest/8080/lesson'
|
user_root = session.get('path')
|
||||||
session['user_id'] = 'test'
|
|
||||||
user_root = f'/tmp/{session.get("user_id")}/{session.get("path")}'
|
|
||||||
path = request.json.get('path', '')
|
path = request.json.get('path', '')
|
||||||
new_name = request.json.get('newName', '')
|
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:
|
try:
|
||||||
path_list = filepath.split('/')
|
path_list = filepath.split('/')
|
||||||
path_list[-1] = new_name
|
path_list[-1] = new_name
|
||||||
@@ -56,15 +53,14 @@ def rename_file():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
return jsonify({"error": str(e)}), 500
|
return jsonify({"error": str(e)}), 500
|
||||||
|
|
||||||
|
|
||||||
@file_bp.route('/rename-folder', methods=['POST'])
|
@file_bp.route('/rename-folder', methods=['POST'])
|
||||||
def rename_folder():
|
def rename_folder():
|
||||||
session['path'] = 'algotest/8080/lesson'
|
user_root = session.get('path')
|
||||||
session['user_id'] = 'test'
|
|
||||||
user_root = f'/tmp/{session.get("user_id")}/{session.get("path")}'
|
|
||||||
path = request.json.get('path', '')
|
path = request.json.get('path', '')
|
||||||
new_name = request.json.get('newName', '')
|
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:
|
try:
|
||||||
path_list = folderpath.split('/')
|
path_list = folderpath.split('/')
|
||||||
path_list[-1] = new_name
|
path_list[-1] = new_name
|
||||||
@@ -74,46 +70,59 @@ def rename_folder():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
return jsonify({"error": str(e)}), 500
|
return jsonify({"error": str(e)}), 500
|
||||||
|
|
||||||
|
|
||||||
@file_bp.route('/copy-files', methods=['POST'])
|
@file_bp.route('/copy-files', methods=['POST'])
|
||||||
def copy_files():
|
def copy_files():
|
||||||
session['path'] = 'algotest/8080/lesson'
|
|
||||||
session['user_id'] = 'test'
|
|
||||||
old_path = request.json.get('oldPath', '')
|
old_path = request.json.get('oldPath', '')
|
||||||
new_path = request.json.get('newPath', '')
|
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)
|
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
|
counter = 1
|
||||||
while os.path.exists(final_new_path):
|
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
|
counter += 1
|
||||||
|
|
||||||
|
# 判断 old_path 是文件夹还是文件,并进行相应的复制操作
|
||||||
if os.path.isdir(old_path):
|
if os.path.isdir(old_path):
|
||||||
shutil.copytree(old_path, final_new_path, dirs_exist_ok=True)
|
shutil.copytree(old_path, final_new_path, dirs_exist_ok=True)
|
||||||
else:
|
else:
|
||||||
shutil.copy(old_path, final_new_path)
|
shutil.copy(old_path, final_new_path)
|
||||||
|
|
||||||
return jsonify({"message": "Files copied successfully"})
|
return jsonify({"message": "Files copied successfully"})
|
||||||
|
|
||||||
|
|
||||||
@file_bp.route('/delete-file', methods=['POST'])
|
@file_bp.route('/delete-file', methods=['POST'])
|
||||||
def delete_file():
|
def delete_file():
|
||||||
session['path'] = 'algotest/8080/lesson'
|
|
||||||
session['user_id'] = 'test'
|
|
||||||
path = request.json.get('path', '')
|
path = request.json.get('path', '')
|
||||||
user_root = f'/tmp/{session.get("user_id")}/{session.get("path")}'
|
root_workspace_path = current_app.config['ROOT_WORKSPACE_PATH']
|
||||||
filepath = f'{user_root}/{path}'
|
user_root = session.get('path')
|
||||||
|
base_path = f'{root_workspace_path}/{user_root}'
|
||||||
|
filepath = f'{base_path}/{path}'
|
||||||
if os.path.isdir(filepath):
|
if os.path.isdir(filepath):
|
||||||
shutil.rmtree(filepath)
|
shutil.rmtree(filepath)
|
||||||
else:
|
else:
|
||||||
os.remove(filepath)
|
os.remove(filepath)
|
||||||
return jsonify({"message": "File deleted successfully"})
|
return jsonify({"message": "File deleted successfully"})
|
||||||
|
|
||||||
|
|
||||||
@file_bp.route('/file-content', methods=['POST'])
|
@file_bp.route('/file-content', methods=['POST'])
|
||||||
def file_content():
|
def file_content():
|
||||||
session['path'] = 'algotest/8080/lesson'
|
path = session.get('path')
|
||||||
session['user_id'] = 'test'
|
root_workspace_path = current_app.config['ROOT_WORKSPACE_PATH']
|
||||||
user_root = f'/tmp/{session.get("user_id")}/{session.get("path")}'
|
base_path = f'{root_workspace_path}/{path}'
|
||||||
filepath = f'{user_root}/{request.json.get("path", "")}'
|
filepath = request.json.get('path', '')
|
||||||
|
filepath = f'{base_path}/{filepath}'
|
||||||
if not os.path.exists(filepath):
|
if not os.path.exists(filepath):
|
||||||
return jsonify({"error": "File not found"}), 404
|
return jsonify({"error": "File not found"}), 404
|
||||||
try:
|
try:
|
||||||
@@ -123,27 +132,34 @@ def file_content():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
return jsonify({"error": str(e)}), 500
|
return jsonify({"error": str(e)}), 500
|
||||||
|
|
||||||
|
|
||||||
@file_bp.route('/save-file', methods=['POST'])
|
@file_bp.route('/save-file', methods=['POST'])
|
||||||
def save_file():
|
def save_file():
|
||||||
session['path'] = 'algotest/8080/lesson'
|
path = session.get('path')
|
||||||
session['user_id'] = 'test'
|
root_workspace_path = current_app.config['ROOT_WORKSPACE_PATH']
|
||||||
user_root = f'/tmp/{session.get("user_id")}/{session.get("path")}'
|
base_path = f'{root_workspace_path}/{path}'
|
||||||
filepath = f'{user_root}/{request.json.get("path", "")}'
|
filepath = request.json.get('path', '')
|
||||||
|
filepath = f'{base_path}/{filepath}'
|
||||||
content = request.json.get('content', '')
|
content = request.json.get('content', '')
|
||||||
# 文件不存在就创建
|
if not os.path.exists(filepath):
|
||||||
os.makedirs(os.path.dirname(filepath), exist_ok=True)
|
return jsonify({"error": "File not found"}), 404
|
||||||
with open(filepath, 'w', encoding='utf-8') as file:
|
try:
|
||||||
file.write(content)
|
with open(filepath, 'w') as file:
|
||||||
return jsonify({"message": "File saved successfully"})
|
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'])
|
@file_bp.route('/file-tree', methods=['GET'])
|
||||||
def file_tree():
|
def file_tree():
|
||||||
session['path'] = 'algotest/8080/lesson'
|
path = session.get('path')
|
||||||
session['user_id'] = 'test'
|
user_id = session.get('user_id')
|
||||||
user_root = f'/tmp/{session.get("user_id")}/{session.get("path")}'
|
if not path:
|
||||||
# 确保目录存在
|
return jsonify({"error": "Missing path parameter"}), 400
|
||||||
os.makedirs(user_root, exist_ok=True)
|
root_workspace_path = current_app.config['ROOT_WORKSPACE_PATH']
|
||||||
# 返回空列表,前端不再报错
|
base_path = os.path.join(root_workspace_path, user_id, path)
|
||||||
return jsonify([])
|
|
||||||
|
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)
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ TERM_INIT_CONFIG = {
|
|||||||
# instead of local server runnning this web terminal service
|
# instead of local server runnning this web terminal service
|
||||||
# "domain" is the target that you want to access through local server (with this web terminal)
|
# "domain" is the target that you want to access through local server (with this web terminal)
|
||||||
# and before doing so - make sure you have username and port (on the "domain") to implement remote access
|
# and before doing so - make sure you have username and port (on the "domain") to implement remote access
|
||||||
'domain': '15.168.14.27', # or ip address like 192.168.10.11
|
'domain': '127.0.0.1', # or ip address like 192.168.10.11
|
||||||
'client_path': {
|
'client_path': {
|
||||||
'telnet': '/usr/bin/telnet', # confirmed location of your client binary (with cmd like 'which telnet')
|
'telnet': '/usr/bin/telnet', # confirmed location of your client binary (with cmd like 'which telnet')
|
||||||
'ssh': '/usr/bin/ssh'
|
'ssh': '/usr/bin/ssh'
|
||||||
|
|||||||
Reference in New Issue
Block a user