test

· · 个人记录

本程序是面向deepseek编程项目

遵循 zcygod -v lizesheng 开源也闭源协议

后端放在templates文件夹里

main.py


import os
from flask import Flask, request, redirect, render_template, send_from_directory, make_response
from werkzeug.utils  import secure_filename
app = Flask(__name__)
UPLOAD_FOLDER = 'webdisk'
app.config['UPLOAD_FOLDER']  = UPLOAD_FOLDER
os.makedirs(UPLOAD_FOLDER,  exist_ok=True)  # 确保存储目录存在 
def get_file_list():
"""获取当前目录文件列表并排序"""
files = []
for filename in os.listdir(app.config['UPLOAD_FOLDER']): 
path = os.path.join(app.config['UPLOAD_FOLDER'],  filename)
files.append({ 
'name': filename,
'size': os.path.getsize(path), 
'is_dir': os.path.isdir(path), 
'modified': os.path.getmtime(path) 
})
return sorted(files, key=lambda x: (not x['is_dir'], x['name']))
@app.route('/',  methods=['GET'])
def index():
"""主界面:显示文件列表"""
return render_template('index.html',  files=get_file_list())
@app.route('/mkdir',  methods=['POST'])
def create_dir():
"""创建目录接口"""
dirname = secure_filename(request.form['dirname'].strip()) 
if dirname:
new_dir = os.path.join(app.config['UPLOAD_FOLDER'],  dirname)
os.makedirs(new_dir,  exist_ok=True)
return redirect('/')
@app.route('/upload',  methods=['POST'])
def upload_file():
"""文件上传接口"""
if 'file' not in request.files: 
return redirect('/')
file = request.files['file'] 
if file.filename  == '':
return redirect('/')
if file:
filename = secure_filename(file.filename) 
file.save(os.path.join(app.config['UPLOAD_FOLDER'],  filename))
return redirect('/')
@app.route('/download/<path:filename>') 
def download_file(filename):
"""文件下载接口"""
safe_filename = secure_filename(filename)
return send_from_directory(
app.config['UPLOAD_FOLDER'], 
safe_filename,
as_attachment=True
)
@app.route('/view/<path:filename>') 
def view_file(filename):
"""文件预览接口(仅文本)"""
safe_filename = secure_filename(filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'],  safe_filename)
# 仅支持预览文本文件 
if os.path.isfile(filepath)  and filepath.endswith(('.txt',  '.py', '.html', '.js', '.css')):
    with open(filepath, 'r', encoding='utf-8', errors='ignore') as f:
        content = f.read() 
    return render_template('viewer.html',  filename=safe_filename, content=content)
return "预览格式不支持", 400

if name == 'main':

监听所有地址的80端口

app.run(host='0.0.0.0',  port=80)
## index.html
```html
<!DOCTYPE html>
<html>
<head>
    <title>内网网盘</title>
    <style>
        body { font-family: sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
        table { width: 100%; border-collapse: collapse; margin: 20px 0; }
        th, td { padding: 10px; text-align: left; border-bottom: 1px solid #ddd; }
        th { background-color: #f2f2f2; }
        .btn { padding: 8px 15px; cursor: pointer; }
        .dir-icon::before { content: "1 "; }
    </style>
</head>
<body>
    <h1>内网共享网盘</h1>

    <!-- 文件上传表单 -->
    <form method="post" enctype="multipart/form-data" action="/upload">
        <input type="file" name="file" required>
        <button type="submit" class="btn">上传文件</button>
    </form>

    <!-- 创建目录表单 -->
    <form method="post" action="/mkdir" style="margin-top: 15px;">
        <input type="text" name="dirname" placeholder="新目录名" required>
        <button type="submit" class="btn">创建目录</button>
    </form>

    <!-- 文件列表 -->
    <h2>文件列表</h2>
    <table>
        <thead>
            <tr>
                <th>文件名</th>
                <th>大小</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            {% for file in files %}
            <tr>
                <td>
                    {% if file.is_dir  %}
                    <span class="dir-icon">{{ file.name  }}</span>
                    {% else %}
                    {{ file.name  }}
                    {% endif %}
                </td>
                <td>
                    {% if file.is_dir  %} - {% else %}
                    {{ (file.size/1024) |round(2) }} KB
                    {% endif %}
                </td>
                <td>
                    {% if not file.is_dir  %}
                    <a href="/download/{{ file.name  }}" download>下载</a> | 
                    <a href="/view/{{ file.name  }}">预览</a>
                    {% endif %}
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</body>
</html>

viewer

<!DOCTYPE html>
<html>
<head>
    <title>预览 - {{ filename }}</title>
    <style>
        pre { 
            background: #f4f4f4; 
            padding: 15px; 
            border-radius: 5px;
            max-height: 70vh;
            overflow: auto;
        }
    </style>
</head>
<body>
    <h1>{{ filename }}</h1>
    <pre>{{ content }}</pre>
    <button onclick="history.back()"> 返回</button>
</body>
</html>