commit 25d01bb331d145e62bebd2a7b7496e8a65176d8b Author: gz Date: Fri May 29 09:40:07 2026 +0800 Initial commit diff --git a/nohup.out b/nohup.out new file mode 100644 index 0000000..ddff7b5 --- /dev/null +++ b/nohup.out @@ -0,0 +1,140 @@ + * Serving Flask app 'upload_service' + * Debug mode: off +WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. + * Running on all addresses (0.0.0.0) + * Running on http://127.0.0.1:8066 + * Running on http://172.16.0.2:8066 +Press CTRL+C to quit +127.0.0.1 - - [10/Jan/2025 16:15:49] "GET /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 16:19:34] "GET /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 16:23:23] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 16:23:23] "GET /upload HTTP/1.0" 200 - + * Serving Flask app 'upload_service' + * Debug mode: off +WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. + * Running on all addresses (0.0.0.0) + * Running on http://127.0.0.1:8066 + * Running on http://172.16.0.2:8066 +Press CTRL+C to quit +127.0.0.1 - - [10/Jan/2025 16:50:49] "GET /upload HTTP/1.0" 200 - + * Serving Flask app 'upload_service' + * Debug mode: off +WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. + * Running on all addresses (0.0.0.0) + * Running on http://127.0.0.1:8066 + * Running on http://172.16.0.2:8066 +Press CTRL+C to quit +127.0.0.1 - - [10/Jan/2025 16:52:39] "GET /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 16:53:30] "GET /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 16:56:53] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 16:57:20] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 16:57:35] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 17:32:30] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 17:32:35] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 17:32:37] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 17:32:41] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 17:32:44] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 17:32:44] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 17:32:44] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 17:32:45] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 17:32:45] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 17:32:45] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 17:32:45] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 17:33:18] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 17:34:43] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 17:35:36] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 17:36:31] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 17:36:54] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 17:37:07] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 17:37:38] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 17:37:55] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 17:38:09] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 17:38:25] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 17:38:41] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 17:38:58] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 17:38:59] "GET /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 17:39:17] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 17:39:20] "GET /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 17:39:25] "GET /upload HTTP/1.0" 200 - + * Serving Flask app 'upload_service' + * Debug mode: off +WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. + * Running on all addresses (0.0.0.0) + * Running on http://127.0.0.1:8066 + * Running on http://172.16.0.2:8066 +Press CTRL+C to quit +127.0.0.1 - - [10/Jan/2025 17:48:57] "GET /upload HTTP/1.0" 200 - + * Serving Flask app 'upload_service' + * Debug mode: off +WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. + * Running on all addresses (0.0.0.0) + * Running on http://127.0.0.1:8066 + * Running on http://172.16.0.2:8066 +Press CTRL+C to quit +127.0.0.1 - - [10/Jan/2025 17:49:55] "GET /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 18:00:45] "POST /upload HTTP/1.0" 200 - +Traceback (most recent call last): + File "/opt/service/python_prj/fileUpload/upload_service.py", line 1, in + from flask import Flask, request, jsonify +ModuleNotFoundError: No module named 'flask' +Traceback (most recent call last): + File "/opt/service/python_prj/fileUpload/upload_service.py", line 1, in + from flask import Flask, request, jsonify +ModuleNotFoundError: No module named 'flask' +Traceback (most recent call last): + File "/opt/service/python_prj/fileUpload/upload_service.py", line 1, in + from flask import Flask, request, jsonify +ModuleNotFoundError: No module named 'flask' + * Serving Flask app 'upload_service' + * Debug mode: off +WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. + * Running on all addresses (0.0.0.0) + * Running on http://127.0.0.1:8066 + * Running on http://172.16.0.2:8066 +Press CTRL+C to quit +127.0.0.1 - - [10/Jan/2025 21:33:27] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:33:57] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:34:43] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:35:06] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:35:19] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:36:08] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:36:23] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:36:41] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:36:54] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:37:26] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:37:42] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:38:06] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:38:24] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:38:51] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:39:09] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:39:26] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:40:14] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:41:31] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:42:02] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:42:19] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:42:47] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:43:08] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:43:26] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:43:45] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:45:37] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:46:23] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:46:47] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:47:06] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:48:14] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:49:38] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:50:01] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [10/Jan/2025 21:50:16] "POST /upload HTTP/1.0" 200 - +71.6.147.254 - - [10/Jan/2025 22:47:31] "GET / HTTP/1.1" 404 - +71.6.147.254 - - [10/Jan/2025 22:47:31] "GET /favicon.ico HTTP/1.1" 404 - +35.203.210.172 - - [11/Jan/2025 03:53:45] code 400, message Bad request version ('À\x13À') +35.203.210.172 - - [11/Jan/2025 03:53:45] "\x16\x03\x01\x00î\x01\x00\x00ê\x03\x03a.Ó\x84ß\x96ä\x1c\x81v²®:Páï&r^¹¦\x16|»þµoð²aöw Z\x0c\x8eÜöa\x9bëNn\x7fÖíÎ8zÅõ9Tø\x9b\x08¯¤\x81$\x9b\x1fî\x06ë\x00&À+À/À,À0̨̩À\x09À\x13À" HTTPStatus.BAD_REQUEST - +35.203.210.172 - - [11/Jan/2025 03:53:45] code 400, message Bad HTTP/0.9 request type ("\x16\x03\x01\x00Ê\x01\x00\x00Æ\x03\x03̪H\x96\\ÝH[±¥åpaNò\x90wS¯\x02ßomª\x7fX\x0fª¤~19\x00\x00hÌ\x14Ì\x13À/À+À0À,À\x11À\x07À'À#À\x13À") +35.203.210.172 - - [11/Jan/2025 03:53:45] "\x16\x03\x01\x00Ê\x01\x00\x00Æ\x03\x03̪H\x96\\ÝH[±¥åpaNò\x90wS¯\x02ßomª\x7fX\x0fª¤~19\x00\x00hÌ\x14Ì\x13À/À+À0À,À\x11À\x07À'À#À\x13À\x09À(À$À\x14À" HTTPStatus.BAD_REQUEST - +127.0.0.1 - - [11/Jan/2025 07:33:18] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 07:34:15] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 07:35:26] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 07:35:49] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 07:38:30] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 07:39:28] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 07:40:53] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 08:09:33] "GET /upload HTTP/1.0" 200 - diff --git a/upload/upload.zip b/upload/upload.zip new file mode 100644 index 0000000..e69de29 diff --git a/upload/ver1/upload_service.py b/upload/ver1/upload_service.py new file mode 100644 index 0000000..1026063 --- /dev/null +++ b/upload/ver1/upload_service.py @@ -0,0 +1,206 @@ +from flask import Flask, request, jsonify +import os + +# 默认配置 +DEFAULT_UPLOAD_FOLDER = "/Users/zgz/Documents/images/" + +ALLOWED_EXTENSIONS = { + 'png', 'jpg', 'jpeg', 'gif', 'bmp', 'tiff', 'webp', # 图片类型 + 'mp4', 'mov', 'wmv', 'avi', 'm4v', 'mpg', 'mpeg', 'flv', 'mkv', '3gp', 'webm' # 视频类型 +} +MAX_FILE_SIZE = 88 * 1024 * 1024 # 88MB + +app = Flask(__name__) +app.config['UPLOAD_FOLDER'] = DEFAULT_UPLOAD_FOLDER +app.config['MAX_CONTENT_LENGTH'] = MAX_FILE_SIZE + + +def allowed_file(filename): + return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS + + +def list_files(directory): + """列出指定目录下的所有文件并按时间排序""" + if not os.path.exists(directory): + return [] + files = [] + for root, _, filenames in os.walk(directory): + for filename in filenames: + files.append(os.path.relpath(os.path.join(root, filename), directory)) + files.sort(key=lambda x: os.path.getmtime(os.path.join(directory, x)), reverse=True) # 按时间降序排序 + return files + + +@app.route('/upload', methods=['GET', 'POST']) +def upload_file(): + if request.method == 'POST': + # 检查是否上传了文件 + if 'file' not in request.files: + return jsonify({'status': 'error', 'message': 'No file part'}), 400 + + file = request.files['file'] + if file.filename == '': + return jsonify({'status': 'error', 'message': 'No selected file'}), 400 + + # 获取自定义路径并检查合法性 + custom_path = request.form.get('customPath', DEFAULT_UPLOAD_FOLDER).strip() + if custom_path: + # 验证路径合法性 + if not os.path.isabs(custom_path): # 必须是绝对路径 + return jsonify({'status': 'error', 'message': 'Path must be an absolute path'}), 400 + if ".." in custom_path: # 禁止路径包含 ".." 防止目录跳跃 + return jsonify({'status': 'error', 'message': 'Path not allowed'}), 400 + else: + custom_path = DEFAULT_UPLOAD_FOLDER # 默认路径 + + # 创建完整保存路径 + save_dir = os.path.abspath(custom_path) + if not os.path.exists(save_dir): + os.makedirs(save_dir) + + # 检查文件类型并保存 + if file and allowed_file(file.filename): + filepath = os.path.join(save_dir, file.filename) + file.save(filepath) + uploaded_files = list_files(save_dir) # 列举上传路径中的文件 + return jsonify({'status': 'success', 'message': f'File uploaded to {save_dir}/{file.filename}', + 'files': uploaded_files}), 200 + + return jsonify({'status': 'error', 'message': 'File type not allowed'}), 400 + + # GET 请求返回 HTML 页面 + files = list_files(DEFAULT_UPLOAD_FOLDER) + # 显示最新10个文件,超出部分隐藏 + files_to_show = files[:10] + files_hidden = files[10:] + + files_list_html = ''.join( + f'
  • {file}
  • ' + for file in files_to_show + ) + hidden_files_html = ''.join( + f'
  • {file}
  • ' + for file in files_hidden + ) + + files_all_json = jsonify(files=files) # 传回所有文件列表,后续可以通过AJAX加载所有文件 + + html_template = f""" + + + + + + File Upload + + + + +

    Upload a file

    +
    + + + + + +
    +
    +

    Uploaded files (Latest files displayed):

    +
      + {files_list_html} + {hidden_files_html} +
    + + + + """ + return html_template + + +if __name__ == '__main__': + if not os.path.exists(DEFAULT_UPLOAD_FOLDER): + os.makedirs(DEFAULT_UPLOAD_FOLDER) + app.run(host='0.0.0.0', port=8066) \ No newline at end of file diff --git a/upload/ver2/app.py b/upload/ver2/app.py new file mode 100644 index 0000000..6f79783 --- /dev/null +++ b/upload/ver2/app.py @@ -0,0 +1,115 @@ +from flask import Flask, request, jsonify, render_template, send_from_directory +import os + +# 默认配置 +DEFAULT_UPLOAD_FOLDER = "/Users/zgz/Documents/images" +#DEFAULT_UPLOAD_FOLDER = "/mnt/sese" + +ALLOWED_EXTENSIONS = { + 'png', 'jpg', 'jpeg', 'gif', 'bmp', 'tiff', 'webp','heic', # 图片类型 + 'mp4', 'mov', 'wmv', 'avi', 'm4v', 'mpg', 'mpeg', 'flv', 'mkv', '3gp', 'webm' # 视频类型 +} +MAX_FILE_SIZE = 88 * 1024 * 1024 # 88MB +app = Flask(__name__, static_folder=None) +#app = Flask(__name__, static_folder=DEFAULT_UPLOAD_FOLDER, static_url_path="/files") +app.config['UPLOAD_FOLDER'] = DEFAULT_UPLOAD_FOLDER +app.config['MAX_CONTENT_LENGTH'] = MAX_FILE_SIZE + + +def allowed_file(filename): + return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS + + +def list_files(directory): + """列出指定目录下的所有文件并按时间排序""" + if not os.path.exists(directory): + return [] + files = [] + for root, _, filenames in os.walk(directory): + for filename in filenames: + files.append(os.path.relpath(os.path.join(root, filename), directory)) + files.sort(key=lambda x: os.path.getmtime(os.path.join(directory, x)), reverse=True) # 按时间降序排序 + return files + +@app.route('/delete', methods=['DELETE']) +def delete_file(): + # 获取当前选择的路径,默认路径为 DEFAULT_UPLOAD_FOLDER + upload_path = request.args.get('path', DEFAULT_UPLOAD_FOLDER) + + file_name = request.args.get('file') + if not file_name: + return jsonify({'status': 'error', 'message': 'File name is required'}), 400 + + file_path = os.path.join(upload_path, file_name) + if not os.path.exists(file_path): + return jsonify({'status': 'error', 'message': 'File not found'}), 404 + + try: + os.remove(file_path) + return jsonify({'status': 'success', 'message': f'File {file_name} deleted successfully'}) + except Exception as e: + return jsonify({'status': 'error', 'message': f'Failed to delete file: {str(e)}'}), 500 + +@app.route('/upload', methods=['GET', 'POST']) +def upload_file(): + if request.method == 'POST': + # 检查是否上传了文件 + if 'file' not in request.files: + return jsonify({'status': 'error', 'message': 'No file part'}), 400 + + file = request.files['file'] + if file.filename == '': + return jsonify({'status': 'error', 'message': 'No selected file'}), 400 + + # 获取用户选择的上传路径,默认为 DEFAULT_UPLOAD_FOLDER + upload_path = request.form.get('uploadPath', DEFAULT_UPLOAD_FOLDER) + + # 检查路径是否合法 + allowed_paths = ['/mnt/sese', '/mnt/self', '/mnt/temp', '/Users/zgz/Documents/images', '/Users/zgz/Documents/images/share'] + if upload_path not in allowed_paths: + return jsonify({'status': 'error', 'message': 'Invalid upload path'}), 400 + + # 检查文件类型并保存 + if file and allowed_file(file.filename): + save_dir = os.path.abspath(upload_path) + if not os.path.exists(save_dir): + os.makedirs(save_dir) + + filepath = os.path.join(save_dir, file.filename) + file.save(filepath) + + uploaded_files = list_files(save_dir) # 列举上传路径中的文件 + return jsonify({'status': 'success', 'message': f'File uploaded to {filepath}', + 'files': uploaded_files}), 200 + + return jsonify({'status': 'error', 'message': 'File type not allowed'}), 400 + + # GET 请求返回 HTML 页面 + files = list_files(DEFAULT_UPLOAD_FOLDER) + return render_template('index.html', files=files[:10], total_files=len(files)) + + +@app.route('/files', methods=['GET']) +def get_files(): + # 获取当前选择的路径,默认路径为 DEFAULT_UPLOAD_FOLDER + upload_path = request.args.get('path', DEFAULT_UPLOAD_FOLDER) + + # 获取该路径下的文件列表 + files = list_files(upload_path) + return jsonify(files=files) + +@app.route('/static/') +def serve_dynamic_static(filename): + directory = request.args.get('path', '/mnt/sese') + full_path = os.path.join(directory, filename) + print(f"Serving file from directory: {directory}, file: {filename}") + print(f"Full path: {full_path}") + try: + return send_from_directory(directory, filename) + except FileNotFoundError: + return jsonify({"error": "File not found", "directory": directory, "filename": filename}), 404 + +if __name__ == '__main__': + if not os.path.exists(DEFAULT_UPLOAD_FOLDER): + os.makedirs(DEFAULT_UPLOAD_FOLDER) + app.run(host='0.0.0.0', port=8066) \ No newline at end of file diff --git a/upload/ver2/nohup.out b/upload/ver2/nohup.out new file mode 100644 index 0000000..e6f2dfa --- /dev/null +++ b/upload/ver2/nohup.out @@ -0,0 +1,138 @@ + * Serving Flask app 'app' + * Debug mode: off +WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. + * Running on all addresses (0.0.0.0) + * Running on http://127.0.0.1:8066 + * Running on http://172.16.0.2:8066 +Press CTRL+C to quit +127.0.0.1 - - [11/Jan/2025 10:35:15] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 10:41:29] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 10:51:01] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 10:51:21] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 10:53:08] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 10:54:58] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 10:56:14] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 10:56:44] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 10:57:32] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 10:58:00] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 10:58:39] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 10:59:37] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 10:59:52] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 11:00:05] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 11:00:19] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 11:01:21] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 11:02:27] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 11:03:21] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 11:05:07] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 11:07:24] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 11:08:20] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 11:10:13] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 11:14:49] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 11:17:07] "GET /files/IMG_3172.gif HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 11:17:10] "GET /files/IMG_6922.gif HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 11:17:14] "GET /files/IMG_4709.gif HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 11:20:03] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 11:26:11] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 11:29:10] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 11:31:46] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 11:31:48] "GET /files/她的派里渴望满满的奶油.jpeg HTTP/1.0" 200 - + * Serving Flask app 'app' + * Debug mode: off +WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. + * Running on all addresses (0.0.0.0) + * Running on http://127.0.0.1:8066 + * Running on http://172.16.0.2:8066 +Press CTRL+C to quit +127.0.0.1 - - [11/Jan/2025 18:49:00] "GET /files/IMG_3865.jpeg HTTP/1.0" 404 - +127.0.0.1 - - [11/Jan/2025 18:49:02] "GET /files/0InspNN87tbR8Vki.mp4 HTTP/1.0" 404 - +127.0.0.1 - - [11/Jan/2025 18:49:05] "GET /files/IMG_3172.gif HTTP/1.0" 404 - +127.0.0.1 - - [11/Jan/2025 18:49:08] "GET /files/0InspNN87tbR8Vki.mp4 HTTP/1.0" 404 - +127.0.0.1 - - [11/Jan/2025 18:49:09] "GET /files/IMG_3865.jpeg HTTP/1.0" 404 - +127.0.0.1 - - [11/Jan/2025 18:49:12] "GET /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 18:49:12] "GET /files?path=/mnt/sese HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 18:49:24] "GET /static/IMG_3865.jpeg?path=/mnt/sese HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 18:49:33] "GET /static/IMG_4709.gif?path=/mnt/sese HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 18:49:36] "GET /static/IMG_3172.gif?path=/mnt/sese HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 18:50:58] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 18:57:08] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 18:57:45] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 18:59:49] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 19:06:17] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 19:07:30] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 19:10:42] "POST /upload HTTP/1.0" 200 - +34.77.181.91 - - [11/Jan/2025 19:52:00] "GET / HTTP/1.1" 404 - +127.0.0.1 - - [11/Jan/2025 21:16:48] "GET /static/她的派里渴望满满的奶油.jpeg?path=/mnt/self HTTP/1.0" 404 - +127.0.0.1 - - [11/Jan/2025 21:16:53] "GET /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 21:16:53] "GET /files?path=/mnt/sese HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 21:16:57] "GET /static/她的派里渴望满满的奶油.jpeg?path=/mnt/sese HTTP/1.0" 304 - +127.0.0.1 - - [11/Jan/2025 21:17:03] "GET /static/bd45d1292k6138be60eca14446395082.gif?path=/mnt/sese HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 21:17:07] "GET /static/IMG_3865.jpeg?path=/mnt/sese HTTP/1.0" 304 - +127.0.0.1 - - [11/Jan/2025 21:17:37] "GET /files?path=/mnt/self HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 21:17:41] "GET /files?path=/mnt/temp HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 21:17:43] "GET /files?path=/mnt/self HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 21:17:46] "GET /files?path=/mnt/sese HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 21:17:50] "GET /files?path=/mnt/temp HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 22:30:42] "GET /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 22:30:42] "GET /files?path=/mnt/sese HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 22:30:49] "GET /files?path=/mnt/temp HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 22:32:24] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 22:43:48] "GET /files?path=/mnt/self HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 22:43:50] "GET /static/d125515bc7e4929f5920cb39ea6595ea.jpeg?path=/mnt/self HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 22:44:24] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 22:45:52] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 22:46:22] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 22:46:59] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 22:47:32] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 22:48:03] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 22:48:42] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 22:49:16] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 22:49:47] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 22:50:15] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 22:51:02] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 22:51:17] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 22:51:47] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 22:52:18] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 22:52:45] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 22:53:42] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 22:53:59] "POST /upload HTTP/1.0" 200 - +127.0.0.1 - - [11/Jan/2025 22:54:05] "GET /static/IMG_2058.jpeg?path=/mnt/self HTTP/1.0" 200 - +47.251.92.46 - - [12/Jan/2025 08:56:23] code 400, message Bad request version ('À\x13À') +47.251.92.46 - - [12/Jan/2025 08:56:23] "\x16\x03\x01\x00î\x01\x00\x00ê\x03\x03I\x06²ù¼\x99iÀYûøºe-\x83Ud]\x0e\x8d·b8´gÆÝ9dZ8f gÁ\x81i³öò˰©\x19NÖ½\x95g|püéÄ\x94xa\x00&À+À/À,À0̨̩À\x09À\x13À" HTTPStatus.BAD_REQUEST - +162.216.149.170 - - [13/Jan/2025 06:07:36] code 400, message Bad request version ('À(À$À\x14À') +162.216.149.170 - - [13/Jan/2025 06:07:36] "\x16\x03\x01\x00Ê\x01\x00\x00Æ\x03\x03 5f²ç|Q³Øhëueöv\x18}{?²1/fà~Ö¥"\x07Qs}\x00\x00hÌ\x14Ì\x13À/À+À0À,À\x11À\x07À'À#À\x13À\x09À(À$À\x14À" HTTPStatus.BAD_REQUEST - +192.155.90.118 - - [13/Jan/2025 06:59:43] code 400, message Bad request version ('À\x13À') +192.155.90.118 - - [13/Jan/2025 06:59:43] "\x16\x03\x01\x00î\x01\x00\x00ê\x03\x03].j×G\x09\x9a-\x8d\x94YWb\\¼\x90¢\x93o³È\x1dEgS>Åg»Û0Ó Èh3ñc·Ðªï7ú±Ð\x94\x83\x8f\x9b\x80FV\x92¨Í\x80ù&\x9a¤\x9e\x8eli\x00&À+À/À,À0̨̩À\x09À\x13À" HTTPStatus.BAD_REQUEST - +127.0.0.1 - - [13/Jan/2025 09:08:02] "GET /files?path=/mnt/self HTTP/1.0" 200 - +127.0.0.1 - - [13/Jan/2025 09:13:49] "GET /static/IMG_1747.jpeg?path=/mnt/self HTTP/1.0" 200 - diff --git a/upload/ver2/templates/index.html b/upload/ver2/templates/index.html new file mode 100644 index 0000000..25b24fe --- /dev/null +++ b/upload/ver2/templates/index.html @@ -0,0 +1,301 @@ + + + + + + File Upload + + + + +

    Upload a file

    +
    + + + + + + +
    +
    +

    Uploaded files:

    +
      + + +
      +
      +
      + + \ No newline at end of file diff --git a/upload/ver3/__pycache__/app.cpython-38.pyc b/upload/ver3/__pycache__/app.cpython-38.pyc new file mode 100644 index 0000000..d650739 Binary files /dev/null and b/upload/ver3/__pycache__/app.cpython-38.pyc differ diff --git a/upload/ver3/app.py b/upload/ver3/app.py new file mode 100644 index 0000000..fd7dd7f --- /dev/null +++ b/upload/ver3/app.py @@ -0,0 +1,129 @@ +from flask import Flask, request, jsonify, render_template, send_from_directory +import os + +# 默认配置 +#DEFAULT_UPLOAD_FOLDER = "/Users/zgz/Documents/images" +DEFAULT_UPLOAD_FOLDER = "/mnt/sese" + +ALLOWED_EXTENSIONS = { + 'png', 'jpg', 'jpeg', 'gif', 'bmp', 'tiff', 'webp','heic', # 图片类型 + 'mp4', 'mov', 'wmv', 'avi', 'm4v', 'mpg', 'mpeg', 'flv', 'mkv', '3gp', 'webm' # 视频类型 +} +MAX_FILE_SIZE = 88 * 1024 * 1024 # 88MB +app = Flask(__name__, static_folder=None) +#app = Flask(__name__, static_folder=DEFAULT_UPLOAD_FOLDER, static_url_path="/files") +app.config['UPLOAD_FOLDER'] = DEFAULT_UPLOAD_FOLDER +app.config['MAX_CONTENT_LENGTH'] = MAX_FILE_SIZE + + +def allowed_file(filename): + return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS + + +def list_files(directory): + """列出指定目录下的所有文件并按时间排序""" + if not os.path.exists(directory): + return [] + files = [] + for root, _, filenames in os.walk(directory): + for filename in filenames: + files.append(os.path.relpath(os.path.join(root, filename), directory)) + files.sort(key=lambda x: os.path.getmtime(os.path.join(directory, x)), reverse=True) # 按时间降序排序 + return files + +@app.route('/delete', methods=['DELETE']) +def delete_file(): + # 获取当前选择的路径,默认路径为 DEFAULT_UPLOAD_FOLDER + upload_path = request.args.get('path', DEFAULT_UPLOAD_FOLDER) + + file_name = request.args.get('file') + if not file_name: + return jsonify({'status': 'error', 'message': 'File name is required'}), 400 + + file_path = os.path.join(upload_path, file_name) + if not os.path.exists(file_path): + return jsonify({'status': 'error', 'message': 'File not found'}), 404 + + try: + os.remove(file_path) + return jsonify({'status': 'success', 'message': f'File {file_name} deleted successfully'}) + except Exception as e: + return jsonify({'status': 'error', 'message': f'Failed to delete file: {str(e)}'}), 500 + +@app.route('/upload', methods=['GET', 'POST']) +def upload_file(): + if request.method == 'POST': + # 检查是否上传了文件 + if 'files' not in request.files: + return jsonify({'status': 'error', 'message': 'No files part'}), 400 + + files = request.files.getlist('files') # 获取多个文件 + if not files or all(file.filename == '' for file in files): + return jsonify({'status': 'error', 'message': 'No selected files'}), 400 + + # 获取用户选择的上传路径,默认为 DEFAULT_UPLOAD_FOLDER + upload_path = request.form.get('uploadPath', DEFAULT_UPLOAD_FOLDER) + + # 检查路径是否合法 + allowed_paths = [ + '/mnt/sese', + '/mnt/self', + '/mnt/ttings', + '/mnt/temp', + '/mnt/car', + '/Users/zgz/Documents/images', + '/Users/zgz/Documents/images/share', + ] + if upload_path not in allowed_paths: + return jsonify({'status': 'error', 'message': 'Invalid upload path'}), 400 + + save_dir = os.path.abspath(upload_path) + if not os.path.exists(save_dir): + os.makedirs(save_dir) + + # 检查文件类型并逐一保存 + uploaded_files = [] + for file in files: + if file and allowed_file(file.filename): + filepath = os.path.join(save_dir, file.filename) + file.save(filepath) + uploaded_files.append(file.filename) + + # 列举上传路径中的文件 + current_files = list_files(save_dir) + if uploaded_files: + return jsonify({ + 'status': 'success', + 'message': f'Uploaded files: {", ".join(uploaded_files)}', + 'files': current_files, + }), 200 + return jsonify({'status': 'error', 'message': 'No valid files to upload'}), 400 + + # GET 请求返回 HTML 页面 + files = list_files(DEFAULT_UPLOAD_FOLDER) + return render_template('index.html', files=files[:10], total_files=len(files)) + +@app.route('/files', methods=['GET']) +def get_files(): + # 获取当前选择的路径,默认路径为 DEFAULT_UPLOAD_FOLDER + upload_path = request.args.get('path', DEFAULT_UPLOAD_FOLDER) + + # 获取该路径下的文件列表 + files = list_files(upload_path) + return jsonify(files=files) + +@app.route('/static/') +def serve_dynamic_static(filename): + directory = request.args.get('path', '/mnt/sese') + full_path = os.path.join(directory, filename) + print(f"Serving file from directory: {directory}, file: {filename}") + print(f"Full path: {full_path}") + try: + return send_from_directory(directory, filename) + except FileNotFoundError: + return jsonify({"error": "File not found", "directory": directory, "filename": filename}), 404 + +if __name__ == '__main__': + if not os.path.exists(DEFAULT_UPLOAD_FOLDER): + os.makedirs(DEFAULT_UPLOAD_FOLDER) + app.run(host='0.0.0.0', port=8066) diff --git a/upload/ver3/nohup.out b/upload/ver3/nohup.out new file mode 100644 index 0000000..0e5e533 --- /dev/null +++ b/upload/ver3/nohup.out @@ -0,0 +1,20 @@ + * Serving Flask app 'app' + * Debug mode: off +WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. + * Running on all addresses (0.0.0.0) + * Running on http://127.0.0.1:8066 + * Running on http://172.16.0.2:8066 +Press CTRL+C to quit + * Serving Flask app 'app' + * Debug mode: off +WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. + * Running on all addresses (0.0.0.0) + * Running on http://127.0.0.1:8066 + * Running on http://172.16.0.2:8066 +Press CTRL+C to quit +127.0.0.1 - - [13/Jan/2025 11:47:04] "GET /upload HTTP/1.0" 200 - +127.0.0.1 - - [13/Jan/2025 11:47:04] "GET /files?path=/mnt/sese HTTP/1.0" 200 - +127.0.0.1 - - [13/Jan/2025 11:47:07] "GET /files?path=/Users/zgz/Documents/images HTTP/1.0" 200 - +127.0.0.1 - - [13/Jan/2025 11:47:12] "GET /files?path=/mnt/temp HTTP/1.0" 200 - +127.0.0.1 - - [13/Jan/2025 11:47:13] "GET /files?path=/mnt/self HTTP/1.0" 200 - +127.0.0.1 - - [13/Jan/2025 11:47:14] "GET /static/IMG_2505.jpeg?path=/mnt/self HTTP/1.0" 304 - diff --git a/upload/ver3/templates/index.html b/upload/ver3/templates/index.html new file mode 100644 index 0000000..4c040ac --- /dev/null +++ b/upload/ver3/templates/index.html @@ -0,0 +1,632 @@ + + + + + + Sese Upload + + + + +
      +

      📁 文件管理器

      +

      私有隐私空间

      +
      + +
      +
      + + +
      + +
      +
      📤
      +

      点击或拖拽文件到此处

      +
      支持图片、视频文件
      +
      + + + +
      +
      + +
      + +
      +

      📂 文件列表

      + 0 个文件 +
      + +
        + + +
        +
        +
        + + \ No newline at end of file diff --git a/upload/ver3/templates/index.html_20250425 b/upload/ver3/templates/index.html_20250425 new file mode 100644 index 0000000..6911696 --- /dev/null +++ b/upload/ver3/templates/index.html_20250425 @@ -0,0 +1,323 @@ + + + + + + File Upload + + + + +

        Upload a file

        +
        + + + + + + + +
        + +
        +

        Uploaded files:

        +
          + + +
          +
          +
          + + diff --git a/upload/ver3/templates/index.html_bak b/upload/ver3/templates/index.html_bak new file mode 100644 index 0000000..dbf3df0 --- /dev/null +++ b/upload/ver3/templates/index.html_bak @@ -0,0 +1,323 @@ + + + + + + File Upload + + + + +

          Upload a file

          +
          + + + + + + + +
          + +
          +

          Uploaded files:

          +
            + + +
            +
            +
            + + diff --git a/upload/ver3/uwsgi.ini b/upload/ver3/uwsgi.ini new file mode 100644 index 0000000..0a5b77d --- /dev/null +++ b/upload/ver3/uwsgi.ini @@ -0,0 +1,30 @@ +[uwsgi] +uid = uwsgi +gid = uwsgi + +# 启动服务监听的地址和端口 +http-socket = 0.0.0.0:8066 + +# 指定虚拟环境路径 +virtualenv = /opt/service/python_prj/pictoHub.env + +# 指定 Flask 应用文件的路径 +wsgi-file = /opt/service/python_prj/fileUpload/upload/ver3/app.py + +# 设置 Flask 的应用实例 +callable = app + +# 设置静态文件目录映射 +static-map = /static=/opt/service/python_prj/fileUpload/upload/ver3/static + +# 日志文件 +logto = /var/log/uwsgi/uwsgi.log + +# 设置进程数 +processes = 4 + +# 启动时的 Python 环境路径 +home = /opt/service/python_prj/pictoHub.env + +# 确保应用正常启动 +touch-reload = /opt/service/python_prj/fileUpload/upload/ver3/app.py diff --git a/upload/ver3/uwsgi.log # 日志文件路径 b/upload/ver3/uwsgi.log # 日志文件路径 new file mode 100644 index 0000000..59dd71e --- /dev/null +++ b/upload/ver3/uwsgi.log # 日志文件路径 @@ -0,0 +1,42 @@ +*** Starting uWSGI 2.0.28 (64bit) on [Mon Jan 13 14:30:26 2025] *** +compiled with version: 9.4.0 on 13 January 2025 03:35:46 +os: Linux-5.15.0-1070-oracle #76~20.04.1-Ubuntu SMP Wed Oct 9 14:35:51 UTC 2024 +nodename: 24-11-18-2234 +machine: aarch64 +clock source: unix +detected number of CPU cores: 2 +current working directory: /opt/service/python_prj/fileUpload/upload/ver3 +detected binary path: /opt/service/python_prj/pictoHub.env/bin/uwsgi +!!! no internal routing support, rebuild with pcre support !!! +uWSGI running as root, you can use --uid/--gid/--chroot options +*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** +chdir() to /opt/service/python_prj/fileUpload/upload/ver3 # 应用的工作目录 +chdir(): No such file or directory [core/uwsgi.c line 2617] +*** Starting uWSGI 2.0.28 (64bit) on [Mon Jan 13 14:37:07 2025] *** +compiled with version: 9.4.0 on 13 January 2025 03:35:46 +os: Linux-5.15.0-1070-oracle #76~20.04.1-Ubuntu SMP Wed Oct 9 14:35:51 UTC 2024 +nodename: 24-11-18-2234 +machine: aarch64 +clock source: unix +detected number of CPU cores: 2 +current working directory: /opt/service/python_prj/fileUpload/upload/ver3 +detected binary path: /opt/service/python_prj/pictoHub.env/bin/uwsgi +!!! no internal routing support, rebuild with pcre support !!! +uWSGI running as root, you can use --uid/--gid/--chroot options +*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** +chdir() to /opt/service/python_prj/fileUpload/upload/ver3 # 应用的工作目录 +chdir(): No such file or directory [core/uwsgi.c line 2617] +*** Starting uWSGI 2.0.28 (64bit) on [Mon Jan 13 14:40:44 2025] *** +compiled with version: 9.4.0 on 13 January 2025 03:35:46 +os: Linux-5.15.0-1070-oracle #76~20.04.1-Ubuntu SMP Wed Oct 9 14:35:51 UTC 2024 +nodename: 24-11-18-2234 +machine: aarch64 +clock source: unix +detected number of CPU cores: 2 +current working directory: /opt/service/python_prj/fileUpload/upload/ver3 +detected binary path: /opt/service/python_prj/pictoHub.env/bin/uwsgi +!!! no internal routing support, rebuild with pcre support !!! +uWSGI running as root, you can use --uid/--gid/--chroot options +*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** +chdir() to /opt/service/python_prj/fileUpload/upload/ver3 # 应用的工作目录 +chdir(): No such file or directory [core/uwsgi.c line 2617] diff --git a/upload_service.py_20250414 b/upload_service.py_20250414 new file mode 100644 index 0000000..d62d679 --- /dev/null +++ b/upload_service.py_20250414 @@ -0,0 +1,209 @@ +from flask import Flask, request, jsonify +import os + +# 默认配置 +DEFAULT_UPLOAD_FOLDER = "/mnt/sese/" + +ALLOWED_EXTENSIONS = { + 'png', 'jpg', 'jpeg', 'gif', 'bmp', 'tiff', 'webp', # 图片类型 + 'mp4', 'mov', 'wmv', 'avi', 'm4v', 'mpg', 'mpeg', 'flv', 'mkv', '3gp', 'webm' # 视频类型 +} +MAX_FILE_SIZE = 88 * 1024 * 1024 # 88MB + +app = Flask(__name__) +app.config['UPLOAD_FOLDER'] = DEFAULT_UPLOAD_FOLDER +app.config['MAX_CONTENT_LENGTH'] = MAX_FILE_SIZE + + +def allowed_file(filename): + return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS + + +def list_files(directory): + """列出指定目录下的所有文件并按时间排序""" + if not os.path.exists(directory): + return [] + files = [] + for root, _, filenames in os.walk(directory): + for filename in filenames: + files.append(os.path.relpath(os.path.join(root, filename), directory)) + files.sort(key=lambda x: os.path.getmtime(os.path.join(directory, x)), reverse=True) # 按时间降序排序 + return files + + +@app.route('/upload', methods=['GET', 'POST']) +def upload_file(): + if request.method == 'POST': + # 检查是否上传了文件 + if 'file' not in request.files: + return jsonify({'status': 'error', 'message': 'No file part'}), 400 + + file = request.files['file'] + if file.filename == '': + return jsonify({'status': 'error', 'message': 'No selected file'}), 400 + + # 获取自定义路径并检查合法性 + custom_path = request.form.get('customPath', DEFAULT_UPLOAD_FOLDER).strip() + if custom_path: + # 验证路径合法性 + if not os.path.isabs(custom_path): # 必须是绝对路径 + return jsonify({'status': 'error', 'message': 'Path must be an absolute path'}), 400 + if ".." in custom_path: # 禁止路径包含 ".." 防止目录跳跃 + return jsonify({'status': 'error', 'message': 'Path not allowed'}), 400 + else: + custom_path = DEFAULT_UPLOAD_FOLDER # 默认路径 + + # 创建完整保存路径 + save_dir = os.path.abspath(custom_path) + if not os.path.exists(save_dir): + os.makedirs(save_dir) + + # 检查文件类型并保存 + if file and allowed_file(file.filename): + filepath = os.path.join(save_dir, file.filename) + file.save(filepath) + uploaded_files = list_files(save_dir) # 列举上传路径中的文件 + return jsonify({'status': 'success', 'message': f'File uploaded to {save_dir}/{file.filename}', + 'files': uploaded_files}), 200 + + return jsonify({'status': 'error', 'message': 'File type not allowed'}), 400 + + # GET 请求返回 HTML 页面 + files = list_files(DEFAULT_UPLOAD_FOLDER) + # 显示最新10个文件,超出部分隐藏 + files_to_show = files[:10] + files_hidden = files[10:] + + files_list_html = ''.join( + f'
          • {file}
          • ' + for file in files_to_show + ) + hidden_files_html = ''.join( + f'
          • {file}
          • ' + for file in files_hidden + ) + + files_all_json = jsonify(files=files) # 传回所有文件列表,后续可以通过AJAX加载所有文件 + + html_template = f""" + + + + + + File Upload + + + + +

            Upload a file

            +
            + + + + + +
            +
            +

            Uploaded files (Latest files displayed):

            +
              + {files_list_html} + {hidden_files_html} +
            + + + + """ + return html_template + + +if __name__ == '__main__': + if not os.path.exists(DEFAULT_UPLOAD_FOLDER): + os.makedirs(DEFAULT_UPLOAD_FOLDER) + app.run(host='0.0.0.0', port=8066) diff --git a/upload_service.py_bak b/upload_service.py_bak new file mode 100644 index 0000000..82907db --- /dev/null +++ b/upload_service.py_bak @@ -0,0 +1,104 @@ +from flask import Flask, request, jsonify +import os + +# 配置上传目录和文件限制 +UPLOAD_FOLDER = '/mnt/sese' +ALLOWED_EXTENSIONS = { + 'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif', 'md', 'doc', 'docx','mp4','mov','wmv','avi','m4v','mpg', + 'xlsx', 'xls', 'rar', 'zip', 'java', 'sql', 'py', 'css','conf','sql','properties','yaml','html','htm','jsp','js','json','yml' +} +MAX_FILE_SIZE = 88 * 1024 * 1024 # 88MB + +app = Flask(__name__) +app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER +app.config['MAX_CONTENT_LENGTH'] = MAX_FILE_SIZE + + +def allowed_file(filename): + return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS + + +@app.route('/upload', methods=['GET', 'POST']) +def upload_file(): + if request.method == 'POST': + if 'file' not in request.files: + return jsonify({'status': 'error', 'message': 'No file part'}), 400 + file = request.files['file'] + if file.filename == '': + return jsonify({'status': 'error', 'message': 'No selected file'}), 400 + if file and allowed_file(file.filename): + filepath = os.path.join(app.config['UPLOAD_FOLDER'], file.filename) + file.save(filepath) + return jsonify({'status': 'success', 'message': 'File uploaded successfully'}), 200 + return jsonify({'status': 'error', 'message': 'File type not allowed'}), 400 + + # GET 请求返回 HTML 页面 + files = os.listdir(app.config['UPLOAD_FOLDER']) + files_list_html = ''.join( + f'
          • {file}
          • ' + for file in files + ) + html_template = f""" + + + + + + File Upload + + + +

            Upload a file

            +
            + + + +
            +
            +

            Uploaded list(注意:每日零晨清空列表):

            +
              + {files_list_html} +
            + + + """ + return html_template + + +if __name__ == '__main__': + if not os.path.exists(UPLOAD_FOLDER): + os.makedirs(UPLOAD_FOLDER) + app.run(host='0.0.0.0', port=8066)