| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- # -*- coding: utf-8 -*-
- from flask import Flask, jsonify, request, render_template
- from db_utils.data import *
- from db_utils.files import *
- from file_uploader import upload_file # 导入文件上传的函数
- # 初始化 Flask 应用
- app = Flask(__name__)
- # 初始化数据库和表
- #initialize_database()
- #initialize_table()
- # 初始化数据库和表
- #initialize_files_database()
- #initialize_files_table("filePath")
- @app.route('/')
- def home():
- return render_template('index.html')
- # 获取类别数据
- @app.route('/categories')
- def get_categories():
- rows = fetch_data('category')
- # 将查询结果格式化为字典列表
- category_list = [{"id": row[0], "categoryName": row[1]} for row in rows]
- print(category_list)
- # 返回 JSON 格式的数据
- return jsonify(category_list)
- @app.route('/category-data', methods=['GET'])
- def get_table():
- # 获取查询参数中的表名 (categoryName)
- category_name = request.args.get('categoryName')
- if not category_name:
- return jsonify({"error": "No categoryName provided"}), 400 # 如果没有提供表名,返回错误
- # 根据表名查询数据
- rows = fetch_data(category_name)
- if not rows:
- return jsonify({"error": f"No data found for table {category_name}"}), 404 # 如果没有找到数据,返回错误
- # 将查询结果格式化为字典列表
- category_list = [{"id": row[0], "categoryName": row[1], "url":row[2]} for row in rows]
- print(category_list)
- # 返回 JSON 格式的数据
- return jsonify(category_list)
- @app.route('/delete_files', methods=['DELETE'])
- def delete_files():
- # 获取 URL 查询参数
- file_table = request.args.get('tableName')
- row_id = request.args.get('rowId')
- # 检查是否提供了表名和记录 ID
- if not file_table:
- return jsonify({"error": "Missing table"}), 400
- if not row_id:
- return jsonify({"error": "Missing rowId"}), 400
- try:
- # 调用删除数据的函数
- delete_file(file_table, row_id)
- return jsonify({"message": f"{row_id} deleted successfully from table {file_table}!"}), 200
- except Exception as e:
- return jsonify({"error": str(e)}), 500
- # 上传文件接口
- @app.route('/upload', methods=['POST'])
- def upload():
- return upload_file() # 调用封装好的文件上传函数
- # 下载文件接口
- from flask import send_from_directory
- @app.route('/download/<filename>', methods=['GET'])
- def download_file(filename):
- # 强制下载文件
- return send_from_directory('../uploads', filename, as_attachment=True)
- # 运行 Flask 应用
- if __name__ == '__main__':
- app.run(host='0.0.0.0', port=5000, debug=True)
|