Skip to content

Commit

Permalink
In Django 4.2.18, the filepath for storing the files is not allowed t…
Browse files Browse the repository at this point in the history
…o be an absolute path.
  • Loading branch information
chadgates committed Sep 18, 2024
1 parent 3d5c1f3 commit 29a2572
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
28 changes: 20 additions & 8 deletions pyas2/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from email.parser import HeaderParser
from uuid import uuid4

import django
import requests
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage
Expand All @@ -24,6 +25,21 @@
from pyas2 import settings
from pyas2.utils import run_post_send

# Check if running Django >= 4.2
if django.VERSION >= (4, 2):
try:
from django.core.files.storage import storages

as2files_storage = storages[
"as2files"
] # Use 'as2files' storage if defined in Django 4.2+
except KeyError:
# If 'as2files' is not configured, fallback to default storage
as2files_storage = default_storage
else:
# In Django 4.1 or lower, fallback to default storage
as2files_storage = default_storage

logger = logging.getLogger("pyas2")


Expand Down Expand Up @@ -349,18 +365,14 @@ def create_from_as2message(
# Save the payload to the inbox folder
full_filename = None
if direction == "IN" and status == "S":
if settings.DATA_DIR:
dirname = os.path.join(
settings.DATA_DIR, "messages", organization, "inbox", partner
)
else:
dirname = os.path.join("messages", organization, "inbox", partner)
dirname = os.path.join("messages", organization, "inbox", partner)
if not message.partner.keep_filename or not filename:
filename = f"{message.message_id}.msg"
full_filename = default_storage.generate_filename(

full_filename = as2files_storage.generate_filename(
posixpath.join(dirname, filename)
)
default_storage.save(name=full_filename, content=ContentFile(payload))
as2files_storage.save(name=full_filename, content=ContentFile(payload))

return message, full_filename

Expand Down
9 changes: 9 additions & 0 deletions pyas2/settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os

import django
from django.conf import settings

APP_SETTINGS = getattr(settings, "PYAS2", {})
Expand All @@ -20,3 +21,11 @@

# Max number of days worth of messages to be saved in archive
MAX_ARCH_DAYS = APP_SETTINGS.get("MAX_ARCH_DAYS", 30)

if django.VERSION >= (4, 2) and "as2files" not in settings.STORAGES:
settings.STORAGES["as2files"] = {
"BACKEND": "django.core.files.storage.FileSystemStorage",
"OPTIONS": {
"location": DATA_DIR,
},
}

0 comments on commit 29a2572

Please sign in to comment.