Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disk space usage by duplicate attachments #465

Open
patroqueeet opened this issue May 8, 2024 · 0 comments
Open

disk space usage by duplicate attachments #465

patroqueeet opened this issue May 8, 2024 · 0 comments

Comments

@patroqueeet
Copy link
Contributor

symptom

when I send a personalised mail to many users with the same attachment, the attachment file will be duplicated for each mail and use a large amount of disk space when sent to thousands.

expected solution

have only one Attachment object for a single file linked by many Emails.

workaround as of now

run script to detect and consolidate frequently:

import hashlib
import os

for a in Attachment.objects.all():
    attachments = Attachment.objects.filter(name=a.name).exclude(pk=a.pk)
    if attachments.count() > 1:
        md5 = hashlib.md5()
        if not os.path.exists(a.file.path):
            continue
        md5.update(a.file.file.read())
        hash0 = md5.hexdigest()
        for attachment in attachments:
            md5a = hashlib.md5()
            md5a.update(attachment.file.file.read())
            hash = md5a.hexdigest()
            if hash0 == hash and attachment.name == a.name:
                print(f"{attachment} ({attachment.pk}) is duplicate of {a} ({a.pk})")
                for email in attachment.emails.all():
                    print(f"for {email.pk} add {a} ({a.pk}) and delete {attachment} ({attachment.pk})")
                    if os.path.exists(attachment.file.path):
                        os.remove(attachment.file.path)
                    email.attachments.add(a)
                    if attachment.id:
                        attachment.delete()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant