Skip to content

Commit

Permalink
adding endpoint to force delete every file except placeholder file
Browse files Browse the repository at this point in the history
  • Loading branch information
Deutscher775 committed Aug 31, 2024
1 parent a35f5ba commit 592bdd1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import fastapi.security
import secrets
from typing import Annotated
import astroidapi.attachment_processor
import astroidapi.endpoint_update_handler
import astroidapi.errors
import astroidapi.get_channel_information
Expand Down Expand Up @@ -309,6 +310,20 @@ def new_token(endpoint: int,
else:
return fastapi.responses.JSONResponse(status_code=403, content={"message": "The provided token is invalid."})

@api.delete("/tempattachments", description="Clear the temporary attachments.")
async def clear_temporary_attachments(master_token: Annotated[str, fastapi.Query(max_length=85, min_length=85)]):
if master_token == Bot.config.MASTER_TOKEN:
try:
total_files = len(os.listdir(f"{pathlib.Path(__file__).parent.resolve()}/astroidapi/TMP_attachments")) - 1
astroidapi.attachment_processor.force_clear_temporary_attachments()
requests.post("https://discord.com/api/webhooks/1279497897016299553/3GrZI75dDYwIkwYBac4o2ApJgzlVVCPIZnon_iE5RtaRIyiYUwcdaXxA327oNZyWZXs4", json={"content": f"[Astroid API - TMP Attachments] Deleted {total_files} temporary attachments."})
return fastapi.responses.JSONResponse(status_code=200, content={"message": "Success."})
except Exception as e:
logging.exception(traceback.print_exc())
requests.post("https://discord.com/api/webhooks/1279497897016299553/3GrZI75dDYwIkwYBac4o2ApJgzlVVCPIZnon_iE5RtaRIyiYUwcdaXxA327oNZyWZXs4", json={"content": f"[Astroid API - TMP Attachments] An error occurred while deleting temporary attachments:\n\n `{e}`"})
return fastapi.responses.JSONResponse(status_code=500, content={"message": f"An error occurred: {e}"})
else:
return fastapi.responses.JSONResponse(status_code=401, content={"message": "The provided token is invalid."})

@api.post("/update/{endpoint}", description="Modify an endpoint.", response_description="Endpoint with updated data.")
async def post_endpoint(
Expand Down
12 changes: 11 additions & 1 deletion src/astroidapi/attachment_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,14 @@ def clear_temporary_attachment(attachment_path):
try:
pathlib.Path(attachment_path.replace("\\", "/")).unlink()
except Exception as e:
raise errors.AttachmentProcessError.AttachmentClearError.DeletionError(f"Error deleting temporary attachment. Error: {e}")
raise errors.AttachmentProcessError.AttachmentClearError.DeletionError(f"Error deleting temporary attachment. Error: {e}")

def force_clear_temporary_attachments():
try:
path = f"{pathlib.Path(__file__).parent.resolve()}/TMP_attachments"
for file in pathlib.Path(path).iterdir():
if file.name == ".placeholder":
continue
file.unlink()
except Exception as e:
raise errors.AttachmentProcessError.AttachmentClearError.DeletionError(f"Error deleting temporary attachments. Error: {e}")

0 comments on commit 592bdd1

Please sign in to comment.