diff --git a/filesys/__pycache__/login.cpython-39.pyc b/filesys/__pycache__/login.cpython-39.pyc index 5131ca0..3482a5b 100644 Binary files a/filesys/__pycache__/login.cpython-39.pyc and b/filesys/__pycache__/login.cpython-39.pyc differ diff --git a/filesys/__pycache__/register.cpython-39.pyc b/filesys/__pycache__/register.cpython-39.pyc index ed263d7..ef465f9 100644 Binary files a/filesys/__pycache__/register.cpython-39.pyc and b/filesys/__pycache__/register.cpython-39.pyc differ diff --git a/filesys/__pycache__/sql.cpython-39.pyc b/filesys/__pycache__/sql.cpython-39.pyc index 9e6c890..b380171 100644 Binary files a/filesys/__pycache__/sql.cpython-39.pyc and b/filesys/__pycache__/sql.cpython-39.pyc differ diff --git a/filesys/__pycache__/test.cpython-39.pyc b/filesys/__pycache__/test.cpython-39.pyc new file mode 100644 index 0000000..e62fc74 Binary files /dev/null and b/filesys/__pycache__/test.cpython-39.pyc differ diff --git a/filesys/__pycache__/upload.cpython-39.pyc b/filesys/__pycache__/upload.cpython-39.pyc new file mode 100644 index 0000000..d489eff Binary files /dev/null and b/filesys/__pycache__/upload.cpython-39.pyc differ diff --git a/filesys/login.py b/filesys/login.py index 7dc524e..3933ca9 100644 --- a/filesys/login.py +++ b/filesys/login.py @@ -1,5 +1,7 @@ import tkinter as tk -from tkinter import filedialog +from tkinter import messagebox +from upload import * +from test import * def login_1(): new_window = tk.Tk() @@ -10,43 +12,26 @@ def login_1(): new_label = tk.Label(new_window, text="登陆成功!") new_label.pack() - button_frame = tk.Frame(new_window) - button_frame.pack() - - upload_button = tk.Button(button_frame, text="上传文件", command=upload_file) - download_button = tk.Button(button_frame, text="下载文件", command=download_file) - delete_button = tk.Button(button_frame, text="删除文件", command=delete_file) - - # 使用grid()布局管理器在button_frame内 - upload_button.grid(row=4, column=0,padx=10, pady=10) - download_button.grid(row=4, column=3,padx=10, pady=10) - delete_button.grid(row=4, column=6,padx=10, pady=10) - - # 退出按钮 - exit_button = tk.Button(new_window, text="返回", command=new_window.destroy) + root = tk.Frame(new_window) + root.pack() + + # 创建按钮 + create_table_button = tk.Button(root, text="创建表", command=create_table) + upload_image_button = tk.Button(root, text="上传图片", command=upload_image) + download_button = tk.Button(root, text="下载图片", command=download_image) + delete_button = tk.Button(root, text="删除图片", command=delete_image) + exit_button = tk.Button(root, text="返回", command=new_window.destroy) + + # 布局 + create_table_button.pack() + upload_image_button.pack() + download_button.pack() + delete_button.pack() exit_button.pack() new_window.mainloop() - -def upload_file(): - - selected_file_path = filedialog.askopenfilename() - new_window = tk.Tk() - new_window.title("欢迎来到上传界面") - new_window.geometry("600x200") - # 更新界面,显示选定的文件路径 - selected_file_label = tk.Label(new_window, text=f"选定文件: {selected_file_path}") - selected_file_label.pack() - - new_window.mainloop() - - -def download_file(): - return None - - -def delete_file(): - return None +if __name__ == "__main__": + login_1() diff --git a/filesys/main.py b/filesys/main.py index f6fdfab..75a693d 100644 --- a/filesys/main.py +++ b/filesys/main.py @@ -42,5 +42,5 @@ def main_0(): erro_label = tk.Label(root, text="账号或密码错误") erro_label.pack() - -main() +if __name__ == "__main__": + main() diff --git a/filesys/register.py b/filesys/register.py index 2bdc97c..50c38c5 100644 --- a/filesys/register.py +++ b/filesys/register.py @@ -10,13 +10,13 @@ def register(): username_label = tk.Label(register_view, text="用户名:") username_label.pack() - username_entry_1 = tk.Entry(register_view) - username_entry_1.pack() + username_entry = tk.Entry(register_view) + username_entry.pack() password_label = tk.Label(register_view, text="密码:") password_label.pack() - password_entry_1 = tk.Entry(register_view) - password_entry_1.pack() + password_entry = tk.Entry(register_view) + password_entry.pack() register_button = tk.Button(register_view, text="注册", command=register_0) register_button.pack() @@ -36,3 +36,6 @@ def register_0(): add_user(entered_username, entered_password) ok_label = tk.Label(register_view, text="注册成功") ok_label.pack() + +if __name__ == "__main__": + register() \ No newline at end of file diff --git a/filesys/sql.py b/filesys/sql.py index cb81446..cf49018 100644 --- a/filesys/sql.py +++ b/filesys/sql.py @@ -1,6 +1,14 @@ from flask import * from flask_sqlalchemy import SQLAlchemy import pymysql +import os +from tkinter import filedialog +import pymysql.cursors + +DB_HOST = 'localhost' # 数据库主机 +DB_USER = 'root' # 数据库用户名 +DB_PASSWORD = '123456' # 数据库密码 +DB_NAME = 'file' # 数据库名 app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:123456@localhost:3306/file' @@ -22,6 +30,44 @@ def add_user(username, password): db.session.add(user) db.session.commit() +def create_table(): + connection = pymysql.connect(host=DB_HOST, user=DB_USER, password=DB_PASSWORD, db=DB_NAME) + try: + with connection.cursor() as cursor: + cursor.execute(CREATE_TABLE_SQL) + connection.commit() + print("Table 'images' created successfully.") + finally: + connection.close() + +def path_to_bytes(): + return filedialog.askopenfilename(filetypes=(("Image Files", "*.jpg;*.png;*.gif"), ("All Files", "*.*"))) +def upload_image(): + selected_image_path = path_to_bytes() + if selected_image_path: + with open(selected_image_path, 'rb') as image_file: + image_content = image_file.read() + + connection = pymysql.connect(host=DB_HOST, user=DB_USER, password=DB_PASSWORD, db=DB_NAME) + try: + with connection.cursor() as cursor: + sql = "INSERT INTO images (filename, content) VALUES (%s, %s)" + cursor.execute(sql, (os.path.basename(selected_image_path), pymysql.Binary(image_content))) + connection.commit() + print(f"Image '{selected_image_path}' uploaded successfully.") + finally: + connection.close() + +CREATE_TABLE_SQL = """ +CREATE TABLE IF NOT EXISTS images ( + id INT AUTO_INCREMENT PRIMARY KEY, + filename VARCHAR(255), + content LONGBLOB, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); +""" + + try: with app.app_context(): # 尝试连接数据库 diff --git a/filesys/test.py b/filesys/test.py index bac8506..996ec1e 100644 --- a/filesys/test.py +++ b/filesys/test.py @@ -1,69 +1,114 @@ -import pymysql +import pymysql.cursors +import tkinter as tk +from tkinter import filedialog import os +# 数据库连接参数 +DB_HOST = 'localhost' # 数据库主机 +DB_USER = 'root' # 数据库用户名 +DB_PASSWORD = '123456' # 数据库密码 +DB_NAME = 'file' # 数据库名 -def create_connection(host, user, password, db): - connection = None - try: - connection = pymysql.connect( - host=host, - user=user, - password=password, - db=db, - charset='utf8mb4', - cursorclass=pymysql.cursors.DictCursor - ) - print("Connection to MySQL DB successful") - except pymysql.MySQLError as e: - print(f"The error '{e}' occurred") - - return connection - - -def upload_file_to_db(connection, file_path): - if not os.path.exists(file_path): - print("File does not exist.") - return +# 创建表的SQL语句 +CREATE_TABLE_SQL = """ +CREATE TABLE IF NOT EXISTS images ( + id INT AUTO_INCREMENT PRIMARY KEY, + filename VARCHAR(255), + content LONGBLOB, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); +""" +def create_table(): + connection = pymysql.connect(host=DB_HOST, user=DB_USER, password=DB_PASSWORD, db=DB_NAME) try: - with open(file_path, 'rb') as file: - # 读取文件二进制内容 - file_content = file.read() - - cursor = connection.cursor() - sql_query = """ - INSERT INTO file_storage (file_name, file_content) - VALUES (%s, %s) - """ - # 获取文件名 - file_name = os.path.basename(file_path) - # 执行SQL语句 - cursor.execute(sql_query, (file_name, pymysql.Binary(file_content))) - connection.commit() - print(f"File {file_name} has been uploaded successfully.") - except pymysql.MySQLError as e: - print(f"Error uploading file: {e}") + with connection.cursor() as cursor: + cursor.execute(CREATE_TABLE_SQL) + connection.commit() + print("Table 'images' created successfully.") + finally: + connection.close() +def path_to_bytes(): + return filedialog.askopenfilename(filetypes=(("Image Files", "*.jpg;*.png;*.gif"), ("All Files", "*.*"))) +def upload_image(): + selected_image_path = path_to_bytes() + if selected_image_path: + with open(selected_image_path, 'rb') as image_file: + image_content = image_file.read() + connection = pymysql.connect(host=DB_HOST, user=DB_USER, password=DB_PASSWORD, db=DB_NAME) + try: + with connection.cursor() as cursor: + sql = "INSERT INTO images (filename, content) VALUES (%s, %s)" + cursor.execute(sql, (os.path.basename(selected_image_path), pymysql.Binary(image_content))) + connection.commit() + print(f"Image '{selected_image_path}' uploaded successfully.") + finally: + connection.close() -# 数据库连接配置 -host = 'localhost' -database = 'file' -user = 'root' -password = '123456' +def delete_image(): + # 获取用户输入的图像ID + image_id = tk.simpledialog.askinteger("删除图片", "请输入需要删除的图像ID:") + if image_id is not None: + connection = pymysql.connect(host=DB_HOST, user=DB_USER, password=DB_PASSWORD, db=DB_NAME) + try: + with connection.cursor() as cursor: + # 查询图像信息 + cursor.execute("SELECT * FROM images WHERE id = %s", (image_id,)) + image_data = cursor.fetchone() + if image_data: + # 输出图像信息 + print(f"Image Details:") + for index, value in enumerate(image_data): + column_name = cursor.description[index][0] + print(f"{column_name}: {value}") + # 提示用户确认删除 + confirmation = tk.messagebox.askyesno("Confirm Delete", f"是否删除图片ID为 {image_id}的图片?") + if confirmation: + # 执行删除操作 + sql = "DELETE FROM images WHERE id = %s" + cursor.execute(sql, (image_id,)) + connection.commit() + print(f"成功删除图片ID为 {image_id} 的图片.") + else: + print(f"没有发现图片ID为 {image_id}的图片.") + except Exception as e: + print(f"Error deleting image: {e}") + finally: + connection.close() -# 文件路径 -file_path = 'file_paths.txt' +def download_image(): + # 获取用户输入的图像ID + image_id = tk.simpledialog.askinteger("下载图片", "请输入需要下载的图像ID:") + if image_id is not None: + connection = pymysql.connect(host=DB_HOST, user=DB_USER, password=DB_PASSWORD, db=DB_NAME) + try: + with connection.cursor() as cursor: + # 查询图像内容和文件名 + cursor.execute("SELECT content, filename FROM images WHERE id = %s", (image_id,)) + image_data = cursor.fetchone() + if image_data: + content, original_filename = image_data + # 构建默认的保存文件名 + default_filename = os.path.splitext(original_filename)[0] + ".jpg" + # 使用默认扩展名并允许用户选择保存位置 + save_path = filedialog.asksaveasfilename(initialfile=default_filename, defaultextension=".jpg", filetypes=[("JPEG Images", "*.jpg"), ("All Files", "*.*")]) + if save_path: + try: + with open(save_path, 'wb') as image_file: + image_file.write(content) + print(f"Image downloaded successfully to '{save_path}'.") + except IOError as e: + print(f"Error writing to file: {e}") + else: + print("Download cancelled.") + else: + print(f"No image found with ID {image_id}.") + except Exception as e: + print(f"Error downloading image: {e}") + finally: + connection.close() -# 创建数据库连接 -conn = create_connection(host, user, password, database) -if conn is not None: - # 上传文件到数据库 - upload_file_to_db(conn, file_path) -else: - print("Failed to connect to the database.") -# 关闭连接(如果打开) -if conn: - conn.close() diff --git a/filesys/upload.py b/filesys/upload.py new file mode 100644 index 0000000..c48a8a7 --- /dev/null +++ b/filesys/upload.py @@ -0,0 +1,18 @@ +from tkinter import filedialog +import tkinter as tk +from sql import * +def upload_file_login(): + + new_window = tk.Tk() + new_window.title("欢迎来到上传界面") + new_window.geometry("600x200") + + upload_file_button = tk.Button(new_window, text="上传图片", command=upload_image) + upload_file_button.pack() + + back_button = tk.Button(new_window, text="返回", command=new_window.destroy) + back_button.pack() + + new_window.mainloop() + + diff --git a/filesys/图片/0f754affcc469c034048cec299bdc149.jpg b/filesys/图片/0f754affcc469c034048cec299bdc149.jpg new file mode 100644 index 0000000..d6b7fb1 Binary files /dev/null and b/filesys/图片/0f754affcc469c034048cec299bdc149.jpg differ diff --git a/filesys/图片/null22d2cf8d1fc4906f.jpg b/filesys/图片/null22d2cf8d1fc4906f.jpg new file mode 100644 index 0000000..3faef02 Binary files /dev/null and b/filesys/图片/null22d2cf8d1fc4906f.jpg differ