Skip to content

Commit 0ce7ae0

Browse files
committed
feat(publications): detect attachment MIME type for uploads [no ci]
1 parent e848eb0 commit 0ce7ae0

6 files changed

Lines changed: 26 additions & 4 deletions

File tree

INSTALL.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ curl -fsSL https://raw.githubusercontent.com/smswithoutborders/RelaySMS-Publishe
1313
```bash
1414
sudo apt-get update
1515
sudo apt-get install -y python3 python3-pip python3-venv python3-dev \
16-
build-essential pkg-config libsqlcipher-dev git make curl
16+
build-essential pkg-config libsqlcipher-dev libmagic1 git make curl
1717
```
1818

1919
### Rust

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Publish content to online platforms (Gmail, Twitter, Telegram, etc.) using SMS w
2020
**Ubuntu Dependencies:**
2121

2222
```bash
23-
sudo apt install python3-dev build-essential libsqlcipher-dev pkg-config make git
23+
sudo apt install python3-dev build-essential libsqlcipher-dev libmagic1 pkg-config make git
2424
```
2525

2626
## Installation

install.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ install_system_deps() {
3939
python3 python3-pip python3-venv python3-dev \
4040
build-essential pkg-config \
4141
libsqlcipher-dev \
42+
libmagic1 \
4243
openssl git make curl ||
4344
error "Failed to install system dependencies"
4445
}

publications.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"""Publication processing pipeline service."""
33

44
import base64
5+
import magic
6+
import uuid
57
from typing import Optional
68

79
from sqlalchemy.orm import Session
@@ -329,7 +331,25 @@ def _get_adapter_params(
329331

330332
attachment = content.get_attachment()
331333
if attachment:
332-
params["attachments"] = [{"data": base64.b64encode(attachment).decode()}]
334+
try:
335+
mimetype = magic.from_buffer(attachment[:2048], mime=True)
336+
except magic.MagicException:
337+
mimetype = None
338+
339+
if not mimetype:
340+
logger.warning("Could not determine MIME type of attachment.")
341+
mimetype = "application/octet-stream"
342+
343+
extension = mimetype.split("/")[-1] or "bin"
344+
filename = f"{uuid.uuid4().hex}.{extension}"
345+
346+
params["attachments"] = [
347+
{
348+
"data": base64.b64encode(attachment).decode(),
349+
"filename": filename,
350+
"mimetype": mimetype,
351+
}
352+
]
333353

334354
message = content.get_body().decode()
335355

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ grpcio-tools==1.80.0
1111
msgspec==0.21.1
1212
phonenumbers==9.0.30
1313
pymysql==1.1.3
14+
python-magic==0.4.27
1415
requests==2.34.2
1516
sentry-sdk[grpcio]==2.60.0
1617
sqlalchemy==2.0.50

0 commit comments

Comments
 (0)