Skip to content

Commit 19b0a58

Browse files
committed
Fixed issue with attachments storage dir.
see #196
1 parent 51fb053 commit 19b0a58

File tree

6 files changed

+38
-23
lines changed

6 files changed

+38
-23
lines changed

README.rst

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
modoboa-webmail
22
===============
33

4-
|travis| |codecov| |landscape| |rtfd|
4+
|gha| |codecov| |rtfd|
55

66
The webmail of Modoboa.
77

@@ -38,12 +38,8 @@ Run the following commands to setup the database tables and collect static files
3838
Finally, restart the python process running modoboa (uwsgi, gunicorn,
3939
apache, whatever).
4040

41-
.. |travis| image:: https://travis-ci.org/modoboa/modoboa-webmail.svg?branch=master
42-
:target: https://travis-ci.org/modoboa/modoboa-webmail
43-
44-
.. |landscape| image:: https://landscape.io/github/modoboa/modoboa-webmail/master/landscape.svg?style=flat
45-
:target: https://landscape.io/github/modoboa/modoboa-webmail/master
46-
:alt: Code Health
41+
.. |gha| image:: https://github.com/modoboa/modoboa-webmail/actions/workflows/plugin.yml/badge.svg
42+
:target: https://github.com/modoboa/modoboa-webmail/actions/workflows/plugin.yml
4743

4844
.. |codecov| image:: https://codecov.io/gh/modoboa/modoboa-webmail/branch/master/graph/badge.svg
4945
:target: https://codecov.io/gh/modoboa/modoboa-webmail

modoboa_webmail/constants.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
"""Webmail constants."""
22

3+
import os
4+
5+
from django.conf import settings
36
from django.utils.translation import ugettext_lazy as _
47

58

@@ -9,3 +12,5 @@
912
("size", _("Size")),
1013
("subject", _("Subject")),
1114
]
15+
16+
WEBMAIL_STORAGE_DIR = os.path.join(settings.MEDIA_ROOT, "webmail")

modoboa_webmail/lib/attachments.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
from email import encoders
22
from email.mime.base import MIMEBase
33
import os
4+
import uuid
45

56
import six
67

7-
from django.conf import settings
88
from django.core.files.uploadhandler import FileUploadHandler, SkipFile
99
from django.utils.encoding import smart_bytes
1010

1111
from modoboa.lib.exceptions import InternalError
1212
from modoboa.lib.web_utils import size2integer
1313
from modoboa.parameters import tools as param_tools
1414

15+
from .. import constants
1516
from .rfc6266 import build_header
1617

1718

@@ -25,7 +26,6 @@ def set_compose_session(request):
2526
:param request: a Request object.
2627
:return: the new unique ID.
2728
"""
28-
import uuid
2929
randid = str(uuid.uuid4()).replace("-", "")
3030
request.session["compose_mail"] = {"id": randid, "attachments": []}
3131
return randid
@@ -43,9 +43,9 @@ def save_attachment(f):
4343
"""
4444
from tempfile import NamedTemporaryFile
4545

46-
dstdir = os.path.join(settings.MEDIA_ROOT, "webmail")
4746
try:
48-
fp = NamedTemporaryFile(dir=dstdir, delete=False)
47+
fp = NamedTemporaryFile(
48+
dir=constants.WEBMAIL_STORAGE_DIR, delete=False)
4949
except Exception as e:
5050
raise InternalError(str(e))
5151
if isinstance(f, (six.binary_type, six.text_type)):
@@ -64,8 +64,7 @@ def clean_attachments(attlist):
6464
following information : (random name, real name).
6565
"""
6666
for att in attlist:
67-
fullpath = os.path.join(
68-
settings.MEDIA_ROOT, "modoboa_webmail", att["tmpname"])
67+
fullpath = os.path.join(constants.WEBMAIL_STORAGE_DIR, att["tmpname"])
6968
try:
7069
os.remove(fullpath)
7170
except OSError:
@@ -88,9 +87,8 @@ def create_mail_attachment(attdef, payload=None):
8887
return None
8988
res = MIMEBase(maintype, subtype)
9089
if payload is None:
91-
with open(os.path.join(
92-
settings.MEDIA_ROOT, "modoboa_webmail", attdef["tmpname"]),
93-
"rb") as fp:
90+
path = os.path.join(constants.WEBMAIL_STORAGE_DIR, attdef["tmpname"])
91+
with open(path, "rb") as fp:
9492
res.set_payload(fp.read())
9593
else:
9694
res.set_payload(payload)

modoboa_webmail/lib/imapemail.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from modoboa.lib import u2u_decode
2121
from modoboa.lib.email_utils import Email, EmailAddress
2222

23+
from .. import constants
2324
from . import imapheader
2425
from .imaputils import (
2526
get_imapconnector, BodyStructure
@@ -213,9 +214,13 @@ def _fetch_inlines(self):
213214
for cid, params in list(self.bs.inlines.items()):
214215
if re.search(r"\.\.", cid):
215216
continue
216-
fname = "modoboa_webmail/%s_%s" % (self.mailid, cid)
217-
path = os.path.join(settings.MEDIA_ROOT, fname)
218-
params["fname"] = os.path.join(settings.MEDIA_URL, fname)
217+
fname = "{}_{}".format(self.mailid, cid)
218+
path = os.path.join(constants.WEBMAIL_STORAGE_DIR, fname)
219+
params["fname"] = os.path.join(
220+
settings.MEDIA_URL,
221+
os.path.basename(constants.WEBMAIL_STORAGE_DIR),
222+
fname
223+
)
219224
if default_storage.exists(path):
220225
continue
221226
pdef, content = self.imapc.fetchpart(

modoboa_webmail/lib/sendmail.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import smtplib
2+
13
from django.core import mail
24
from django.template.loader import render_to_string
35

46
from modoboa.lib.cryptutils import get_password
57
from modoboa.parameters import tools as param_tools
68

7-
from ..exceptions import WebmailInternalError
89
from . import get_imapconnector, clean_attachments
910

1011

@@ -48,8 +49,12 @@ def send_mail(request, form, posturl=None):
4849
with mail.get_connection(**options) as connection:
4950
msg.connection = connection
5051
msg.send()
51-
except Exception as inst:
52-
raise WebmailInternalError(str(inst))
52+
except smtplib.SMTPResponseException as inst:
53+
return False, {"status": "ko", "error": inst.smtp_error}
54+
except smtplib.SMTPRecipientsRefused as inst:
55+
error = ", ".join(["{}: {}".format(rcpt, error)
56+
for rcpt, error in inst.recipients.items()])
57+
return False, {"status": "ko", "error": error}
5358

5459
# Copy message to sent folder
5560
sentfolder = request.user.parameters.get_value("sent_folder")

setup.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ def get_requirements(requirements_file):
4343
with io.open(path.join(HERE, "README.rst"), encoding="utf-8") as readme:
4444
LONG_DESCRIPTION = readme.read()
4545

46+
def local_scheme(version):
47+
"""Skip the local version (eg. +xyz of 0.6.1.dev4+gdf99fe2)
48+
to be able to upload to Test PyPI"""
49+
return ""
50+
4651
setup(
4752
name="modoboa-webmail",
4853
description="The webmail of Modoboa",
@@ -62,6 +67,7 @@ def get_requirements(requirements_file):
6267
"Programming Language :: Python :: 3.5",
6368
"Programming Language :: Python :: 3.6",
6469
"Programming Language :: Python :: 3.7",
70+
"Programming Language :: Python :: 3.8",
6571
"Topic :: Communications :: Email",
6672
"Topic :: Internet :: WWW/HTTP",
6773
],
@@ -70,6 +76,6 @@ def get_requirements(requirements_file):
7076
include_package_data=True,
7177
zip_safe=False,
7278
install_requires=INSTALL_REQUIRES,
73-
use_scm_version=True,
79+
use_scm_version={"local_scheme": local_scheme},
7480
setup_requires=["setuptools_scm"],
7581
)

0 commit comments

Comments
 (0)