diff --git a/app/run.py b/app/run.py index 596bbd0..2f637b2 100644 --- a/app/run.py +++ b/app/run.py @@ -650,6 +650,55 @@ def delete_file(): account = Quark(config_data["cookie"][0], 0) if fid := request.json.get("fid"): response = account.delete([fid]) + + # 处理delete_records参数 + if request.json.get("delete_records") and response.get("code") == 0: + try: + # 初始化数据库 + db = RecordDB() + + # 获取save_path参数 + save_path = request.json.get("save_path", "") + + # 如果没有提供save_path,则不删除任何记录 + if not save_path: + response["deleted_records"] = 0 + # logging.info(f">>> 删除文件 {fid} 但未提供save_path,不删除任何记录") + return jsonify(response) + + # 查询与该文件ID和save_path相关的所有记录 + cursor = db.conn.cursor() + + # 使用file_id和save_path进行精确匹配 + cursor.execute("SELECT id FROM transfer_records WHERE file_id = ? AND save_path = ?", (fid, save_path)) + record_ids = [row[0] for row in cursor.fetchall()] + + # 如果没有找到匹配的file_id记录,尝试通过文件名查找 + if not record_ids: + # 获取文件名(如果有的话) + file_name = request.json.get("file_name", "") + if file_name: + # 使用文件名和save_path进行精确匹配 + cursor.execute(""" + SELECT id FROM transfer_records + WHERE (original_name = ? OR renamed_to = ?) + AND save_path = ? + """, (file_name, file_name, save_path)) + + record_ids = [row[0] for row in cursor.fetchall()] + + # 删除找到的所有记录 + deleted_count = 0 + for record_id in record_ids: + deleted_count += db.delete_record(record_id) + + # 添加删除记录的信息到响应中 + response["deleted_records"] = deleted_count + # logging.info(f">>> 删除文件 {fid} 同时删除了 {deleted_count} 条相关记录") + + except Exception as e: + logging.error(f">>> 删除记录时出错: {str(e)}") + # 不影响主流程,即使删除记录失败也返回文件删除成功 else: response = {"success": False, "message": "缺失必要字段: fid"} return jsonify(response) diff --git a/app/sdk/db.py b/app/sdk/db.py index 302d8c5..316de4e 100644 --- a/app/sdk/db.py +++ b/app/sdk/db.py @@ -31,9 +31,17 @@ class RecordDB: resolution TEXT, modify_date INTEGER NOT NULL, file_id TEXT, - file_type TEXT + file_type TEXT, + save_path TEXT ) ''') + + # 检查save_path字段是否存在,如果不存在则添加 + cursor.execute("PRAGMA table_info(transfer_records)") + columns = [column[1] for column in cursor.fetchall()] + if 'save_path' not in columns: + cursor.execute('ALTER TABLE transfer_records ADD COLUMN save_path TEXT') + self.conn.commit() def close(self): @@ -41,19 +49,19 @@ class RecordDB: self.conn.close() def add_record(self, task_name, original_name, renamed_to, file_size, modify_date, - duration="", resolution="", file_id="", file_type=""): + duration="", resolution="", file_id="", file_type="", save_path=""): """添加一条转存记录""" cursor = self.conn.cursor() cursor.execute( "INSERT INTO transfer_records (transfer_time, task_name, original_name, renamed_to, file_size, " - "duration, resolution, modify_date, file_id, file_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", + "duration, resolution, modify_date, file_id, file_type, save_path) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (int(time.time()), task_name, original_name, renamed_to, file_size, - duration, resolution, modify_date, file_id, file_type) + duration, resolution, modify_date, file_id, file_type, save_path) ) self.conn.commit() return cursor.lastrowid - def update_renamed_to(self, file_id, original_name, renamed_to, task_name=""): + def update_renamed_to(self, file_id, original_name, renamed_to, task_name="", save_path=""): """更新最近一条记录的renamed_to字段 Args: @@ -61,6 +69,7 @@ class RecordDB: original_name: 原文件名 renamed_to: 重命名后的文件名 task_name: 任务名称,可选项,如提供则作为附加筛选条件 + save_path: 保存路径,可选项,如提供则同时更新保存路径 Returns: 更新的记录数量 @@ -97,11 +106,17 @@ class RecordDB: if result: record_id = result[0] - # 更新记录 - cursor.execute( - "UPDATE transfer_records SET renamed_to = ? WHERE id = ?", - (renamed_to, record_id) - ) + # 根据是否提供save_path决定更新哪些字段 + if save_path: + cursor.execute( + "UPDATE transfer_records SET renamed_to = ?, save_path = ? WHERE id = ?", + (renamed_to, save_path, record_id) + ) + else: + cursor.execute( + "UPDATE transfer_records SET renamed_to = ? WHERE id = ?", + (renamed_to, record_id) + ) self.conn.commit() return cursor.rowcount @@ -124,7 +139,7 @@ class RecordDB: # 构建SQL查询 valid_columns = ["transfer_time", "task_name", "original_name", "renamed_to", - "file_size", "duration", "resolution", "modify_date"] + "file_size", "duration", "resolution", "modify_date", "save_path"] if sort_by not in valid_columns: sort_by = "transfer_time" @@ -140,7 +155,8 @@ class RecordDB: params.append(task_name_filter) if keyword_filter: - where_clauses.append("task_name LIKE ?") + where_clauses.append("(task_name LIKE ? OR original_name LIKE ?)") + params.append(f"%{keyword_filter}%") params.append(f"%{keyword_filter}%") where_clause = " AND ".join(where_clauses) @@ -192,4 +208,33 @@ class RecordDB: cursor = self.conn.cursor() cursor.execute("DELETE FROM transfer_records WHERE id = ?", (record_id,)) self.conn.commit() - return cursor.rowcount \ No newline at end of file + return cursor.rowcount + + def get_records_by_save_path(self, save_path, include_subpaths=False): + """根据保存路径查询记录 + + Args: + save_path: 要查询的保存路径 + include_subpaths: 是否包含子路径下的文件 + + Returns: + 匹配的记录列表 + """ + cursor = self.conn.cursor() + + if include_subpaths: + # 如果包含子路径,使用LIKE查询 + query = "SELECT * FROM transfer_records WHERE save_path LIKE ? ORDER BY transfer_time DESC" + cursor.execute(query, [f"{save_path}%"]) + else: + # 精确匹配路径 + query = "SELECT * FROM transfer_records WHERE save_path = ? ORDER BY transfer_time DESC" + cursor.execute(query, [save_path]) + + records = cursor.fetchall() + + # 将结果转换为字典列表 + if records: + columns = [col[0] for col in cursor.description] + return [dict(zip(columns, row)) for row in records] + return [] \ No newline at end of file diff --git a/app/static/css/main.css b/app/static/css/main.css index 89c394a..9ae9200 100644 --- a/app/static/css/main.css +++ b/app/static/css/main.css @@ -1633,10 +1633,11 @@ button.close:focus, /* 操作列 - 固定宽度 */ #fileSelectModal .table .col-action { - width: 53px; - min-width: 53px; - max-width: 53px; - text-align: center; + width: 188px; + min-width: 188px; + max-width: 188px; + text-align: left; + padding-left: 12px !important; } /* 确保单元格内容溢出时正确显示 */ @@ -3576,11 +3577,11 @@ input::-moz-list-button { /* 针对选择保存到的文件夹模式 - 带操作列的表格 */ #fileSelectModal[data-modal-type="target"] .breadcrumb { - min-width: 513px; /* 4列表格总宽度: 230px + 90px + 140px + 53px */ + min-width: 648px; /* 4列表格总宽度: 230px + 90px + 140px + 188px */ } #fileSelectModal[data-modal-type="target"] .table { - width: 513px; + width: 648px; } /* 针对命名预览模式 - 2列表格 */ diff --git a/app/templates/index.html b/app/templates/index.html index 7c54034..936284e 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -953,7 +953,8 @@