abhishek-ram / django-pyas2

AS2 file transfer Server built on Python and Django.
https://django-pyas2.readthedocs.io
GNU General Public License v3.0
78 stars 31 forks source link

Error receiving messages with duplicate filenames #43

Open lakemist opened 4 years ago

lakemist commented 4 years ago

One of our partners always sends us AS2 messages with the same filename, even though the contents are different. To handle this use case, we make sure that the "Keeo Original Filename" is NOT set for the partner.

This works well in the Inbox as it create a new file for every message with filename: <msg_id>.msg.

However, there is a problem with the Message model's payload FileField. The logic used for the file field overwrites the previous file if it is sent on the same day. Here is the relevant code: payload = models.FileField(upload_to=get_message_store, null=True, blank=True)

def get_message_store(instance, filename):
    current_date = timezone.now().strftime("%Y%m%d")
    if instance.direction == "OUT":
        target_dir = os.path.join(
            "messages", "__store", "payload", "sent", current_date
        )
    else:
        target_dir = os.path.join(
            "messages", "__store", "payload", "received", current_date
        )
    return "{0}/{1}".format(target_dir, filename)

This code will generate the same filename for multiple records, which results in the last record overwriting files from previous ones.

Perhaps the msg_id should be added to the target_dir?

lakemist commented 4 years ago

Just tried locally... works well but, if you do it, make sure to extend the max_length of the file fields as it defaults to 100.