From 779d6ec887d28d70292bde826f0d11aa06d5cb13 Mon Sep 17 00:00:00 2001 From: qing <1776177662@qq.com> Date: Wed, 22 Jul 2026 20:18:45 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E8=80=A6=E8=A7=86=E5=9B=BE=E6=A8=A1?= =?UTF-8?q?=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- static/fsys.py | 404 +++++--------------------------------- static/public/__view.html | 331 +++++++++++++++++++++++++++++++ 2 files changed, 378 insertions(+), 357 deletions(-) create mode 100644 static/public/__view.html diff --git a/static/fsys.py b/static/fsys.py index d3f52a0..81ad4fb 100644 --- a/static/fsys.py +++ b/static/fsys.py @@ -60,6 +60,9 @@ SSL_ENABLED = file_config['ssl'].get('enabled', 'false').lower() == 'true' SSL_CERT_FILE = file_config['ssl'].get('cert_file', 'cert.pem') SSL_KEY_FILE = file_config['ssl'].get('key_file', 'key.pem') +# 视图文件名:根路径与目录访问时固定返回此静态文件,由前端 JS 调用 /api/filelist 拉取数据 +VIEW_FILE = '__view.html' + # ==================== 视频文件支持 ==================== VIDEO_MIME_TYPES = { '.mp4': 'video/mp4', '.webm': 'video/webm', '.ogg': 'video/ogg', @@ -106,29 +109,29 @@ def get_file_list(): try: if not FILESYSTEM_ENABLED: return jsonify({'error': '文件系统未启用'}), 403 - + requested_path = request.args.get('path', '') full_path = safe_path_join(HTML_ROOT_DIR, requested_path) - + if full_path is None: return jsonify({'error': '无效的路径'}), 403 - + if not os.path.exists(full_path) or not os.path.isdir(full_path): return jsonify({'error': '目录不存在'}), 404 - + items = [] for item in os.listdir(full_path): if item == '__pycache__': continue - + item_path = os.path.join(full_path, item) item_stat = os.stat(item_path) is_dir = os.path.isdir(item_path) - + item_rel_path = os.path.join(requested_path, item) if requested_path else item - + file_type = 'dir' if is_dir else ('video' if is_video_file(item) else ('audio' if is_audio_file(item) else 'file')) - + items.append({ 'name': item, 'type': file_type, @@ -137,9 +140,9 @@ def get_file_list(): 'size_formatted': format_file_size(item_stat.st_size) if not is_dir else '', 'modified': datetime.datetime.fromtimestamp(item_stat.st_mtime).strftime('%Y-%m-%d %H:%M:%S') }) - + items.sort(key=lambda x: (x['type'] != 'dir', x['name'].lower())) - + return jsonify({ 'success': True, 'path': requested_path or '/', @@ -155,33 +158,33 @@ def stream_video(file_path): try: if not FILESYSTEM_ENABLED: return jsonify({'error': '文件系统未启用'}), 403 - + full_path = safe_path_join(HTML_ROOT_DIR, file_path) if full_path is None or not os.path.exists(full_path) or not os.path.isfile(full_path): return jsonify({'error': '文件不存在'}), 404 - + if not is_video_file(full_path) and not is_audio_file(full_path): return jsonify({'error': '不支持的文件类型'}), 400 - + file_size = os.path.getsize(full_path) mime_type = get_media_mime_type(full_path) - + range_header = request.headers.get('Range', None) - + if range_header: byte_range = range_header.replace('bytes=', '').split('-') start = int(byte_range[0]) if byte_range[0] else 0 end = int(byte_range[1]) if len(byte_range) > 1 and byte_range[1] else file_size - 1 - + if start >= file_size: return jsonify({'error': '请求范围超出文件大小'}), 416 - + length = end - start + 1 - + with open(full_path, 'rb') as f: f.seek(start) data = f.read(length) - + response = Response(data, 206, mimetype=mime_type, direct_passthrough=True) response.headers.add('Content-Range', f'bytes {start}-{end}/{file_size}') response.headers.add('Accept-Ranges', 'bytes') @@ -194,342 +197,38 @@ def stream_video(file_path): logger.error(f"Video streaming error: {str(e)}") return jsonify({'error': str(e)}), 500 -# ==================== 极简版HTML页面(使用字符串拼接,避免花括号问题) ==================== -def get_html_template(path, breadcrumb_html): - """生成HTML页面""" - return f''' - -
- - -