增加了删除图片的功能。
This commit is contained in:
@@ -224,6 +224,79 @@ class SQLiteManager:
|
||||
print(f"获取下载统计失败: {e}")
|
||||
return {}
|
||||
|
||||
def get_local_path(self, datasource: str, image_url: str) -> Optional[str]:
|
||||
"""
|
||||
获取图片的本地文件路径
|
||||
|
||||
Args:
|
||||
datasource: 数据源
|
||||
image_url: 图片URL
|
||||
|
||||
Returns:
|
||||
本地文件路径,如果未下载则返回None
|
||||
"""
|
||||
try:
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
cursor.execute('''
|
||||
SELECT local_path FROM download_history
|
||||
WHERE datasource = ? AND image_url = ?
|
||||
''', (datasource, image_url))
|
||||
|
||||
result = cursor.fetchone()
|
||||
conn.close()
|
||||
|
||||
return result[0] if result else None
|
||||
|
||||
except Exception as e:
|
||||
print(f"获取本地路径失败: {e}")
|
||||
return None
|
||||
|
||||
def delete_download_record(self, datasource: str, image_url: str, delete_file: bool = True) -> bool:
|
||||
"""
|
||||
删除下载记录(可选删除物理文件)
|
||||
|
||||
Args:
|
||||
datasource: 数据源
|
||||
image_url: 图片URL
|
||||
delete_file: 是否删除物理文件
|
||||
|
||||
Returns:
|
||||
是否成功
|
||||
"""
|
||||
try:
|
||||
# 如果需要删除文件,先获取文件路径
|
||||
local_path = None
|
||||
if delete_file:
|
||||
local_path = self.get_local_path(datasource, image_url)
|
||||
|
||||
# 删除数据库记录
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
cursor.execute('''
|
||||
DELETE FROM download_history
|
||||
WHERE datasource = ? AND image_url = ?
|
||||
''', (datasource, image_url))
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
# 删除物理文件
|
||||
if delete_file and local_path and os.path.exists(local_path):
|
||||
try:
|
||||
os.remove(local_path)
|
||||
print(f"已删除本地文件: {local_path}")
|
||||
except Exception as e:
|
||||
print(f"删除本地文件失败: {e}")
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"删除下载记录失败: {e}")
|
||||
return False
|
||||
|
||||
def clear_invalid_records(self) -> int:
|
||||
"""清理文件不存在的下载记录"""
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user