Skip to content

Commit

Permalink
Merge pull request #4 from stickman33/main
Browse files Browse the repository at this point in the history
Files removal fix
  • Loading branch information
aigoncharov authored Sep 26, 2024
2 parents ae573b5 + 2c1868c commit 7ed740e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
19 changes: 11 additions & 8 deletions telegram_to_rss/models/feed_entry.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import asyncio
import logging
from tortoise.models import Model
from tortoise import fields
from tortoise.signals import post_delete
Expand All @@ -18,14 +18,17 @@ class FeedEntry(Model):
has_unsupported_media = fields.BooleanField(default=False)


@post_delete
@post_delete(FeedEntry)
async def remove_associated_file(
sender: Type[FeedEntry],
instance: FeedEntry,
using_db
) -> None:
await asyncio.gather(
*[
Path(static_path).joinpath(media_relative_path).unlink(missing_ok=True)
for media_relative_path in instance.media
]
)
try:
for media_relative_path in instance.media:
file_path = Path(static_path).joinpath(media_relative_path)
await file_path.unlink(missing_ok=True)
logging.debug(f"File removed: {file_path}")

except Exception as e:
logging.error(f"Error while removing FeedEntry id {instance.id}: {e}")
6 changes: 3 additions & 3 deletions telegram_to_rss/poll_telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ async def update_feed(self, dialog: custom.Dialog):
.offset(self._message_limit)
)

await FeedEntry.filter(
Q(id__in=[entry.id for entry in old_feed_entries])
).delete()
for entry in old_feed_entries:
logging.debug(f"Deleting FeedEntry with id: {entry.id}")
await entry.delete()

async def _process_new_dialog_messages(
self, feed: Feed, dialog_messages: list[custom.Message]
Expand Down

0 comments on commit 7ed740e

Please sign in to comment.