Files
hsa/code-server-like/app/views/file.py
2025-09-18 11:47:24 +00:00

150 lines
5.3 KiB
Python

from flask import Blueprint, request, jsonify, session
import os
import shutil
from flask import current_app
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}'
filepath = f'{base_path}/{parent}/{filepath}'
os.makedirs(filepath, exist_ok=True)
return jsonify({"message": "Folder created successfully"})
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}'
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():
session['path'] = 'algotest/8080/lesson'
session['user_id'] = 'test'
user_root = f'/tmp/{session.get("user_id")}/{session.get("path")}'
path = request.json.get('path', '')
new_name = request.json.get('newName', '')
filepath = f'{user_root}/{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():
session['path'] = 'algotest/8080/lesson'
session['user_id'] = 'test'
user_root = f'/tmp/{session.get("user_id")}/{session.get("path")}'
path = request.json.get('path', '')
new_name = request.json.get('newName', '')
folderpath = f'{user_root}/{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():
session['path'] = 'algotest/8080/lesson'
session['user_id'] = 'test'
old_path = request.json.get('oldPath', '')
new_path = request.json.get('newPath', '')
old_name = os.path.basename(old_path)
final_new_path = os.path.join(new_path, old_name)
counter = 1
while os.path.exists(final_new_path):
final_new_path = os.path.join(new_path, f"{old_name}_{counter}")
counter += 1
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}'
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", "")}'
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():
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", "")}'
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"})
@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([])