Skip to content

Commit f1a3b6c

Browse files
committedFeb 12, 2025
content type issue
1 parent b6af3a1 commit f1a3b6c

File tree

6 files changed

+52
-14
lines changed

6 files changed

+52
-14
lines changed
 

‎django_petra/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
__title__ = 'Django Petra'
2-
__version__ = '0.0.5'
2+
__version__ = '0.0.6'
33
__author__ = 'Mostafa'
44

55
# Version synonym

‎django_petra/filesystem/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""
2+
This module contains the Storage class, which is used to interact with the filesystem.
3+
"""

‎django_petra/filesystem/storage.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ def move(self, source, destination):
9898
self.local_storage.move(source, destination)
9999
elif self.disk == 's3':
100100
self.s3_storage.move(source, destination)
101+
102+
def content_type(self, path):
103+
if self.disk == 'local':
104+
return self.local_storage.content_type(path)
105+
elif self.disk == 's3':
106+
return self.s3_storage.content_type(path)
101107

102108
# Create an instance of the Storage class
103-
storage = Storage()
109+
storage = Storage()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""
2+
This module contains the implementation of the storage for the filesystem.
3+
"""

‎django_petra/filesystem/the_disks/local.py

+19-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import os
2-
from django_petra.path import base_path
32
import shutil
3+
import mimetypes
4+
5+
from django_petra.path import base_path
46

57
class LocalStorage:
68
def __init__(self):
@@ -9,15 +11,19 @@ def __init__(self):
911
def get_full_path(self, path):
1012
return os.path.join(self.base_path, 'storage', 'public', path)
1113

12-
def put(self, path, contents):
14+
def put(self, path, file):
1315
full_path = self.get_full_path(path)
16+
file_content = file.read()
1417
os.makedirs(os.path.dirname(full_path), exist_ok=True)
15-
with open(full_path, 'w') as file:
16-
file.write(contents)
18+
19+
# Check if contents is bytes or string
20+
mode = 'wb' if isinstance(file_content, bytes) else 'w'
21+
with open(full_path, mode) as file:
22+
file.write(file_content)
1723

1824
def get(self, path):
1925
full_path = self.get_full_path(path)
20-
with open(full_path, 'r') as file:
26+
with open(full_path, 'rb') as file:
2127
return file.read()
2228

2329
def delete(self, path):
@@ -27,9 +33,9 @@ def delete(self, path):
2733
except FileNotFoundError as e:
2834
print(f"Error deleting local file: {e}")
2935

30-
def update(self, path, contents):
36+
def update(self, path, file):
3137
self.delete(path)
32-
self.put(path, contents)
38+
self.put(path, file)
3339

3440
def exists(self, path):
3541
full_path = self.get_full_path(path)
@@ -93,3 +99,9 @@ def move(self, source, destination):
9399
shutil.move(full_source_path, full_destination_path)
94100
except Exception as e:
95101
print(f"Error moving local file: {e}")
102+
103+
def content_type(self, path):
104+
content_type, _ = mimetypes.guess_type(path)
105+
if not content_type:
106+
content_type = 'application/octet-stream'
107+
return content_type

‎django_petra/filesystem/the_disks/s3.py

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import boto3
2+
import mimetypes
23
from botocore.exceptions import ClientError
34
from django_petra.env import get_env
45

@@ -18,16 +19,23 @@ def __init__(self):
1819
region_name=aws_region
1920
)
2021

21-
def put(self, path, contents):
22+
def put(self, path, file):
2223
try:
23-
self.s3_client.put_object(Body=contents, Bucket=self.s3_bucket, Key=path)
24+
file_content = file.read()
25+
content_type, _ = mimetypes.guess_type(file.name)
26+
self.s3_client.put_object(
27+
Body=file_content,
28+
Bucket=self.s3_bucket,
29+
Key=path,
30+
ContentType=content_type
31+
)
2432
except ClientError as e:
2533
print(f"Error putting object to S3: {e}")
2634

2735
def get(self, path):
2836
try:
2937
response = self.s3_client.get_object(Bucket=self.s3_bucket, Key=path)
30-
return response['Body'].read().decode('utf-8')
38+
return response['Body'].read()
3139
except ClientError as e:
3240
print(f"Error getting object from S3: {e}")
3341

@@ -37,9 +45,9 @@ def delete(self, path):
3745
except ClientError as e:
3846
print(f"Error deleting object from S3: {e}")
3947

40-
def update(self, path, contents):
48+
def update(self, path, file):
4149
self.delete(path)
42-
self.put(path, contents)
50+
self.put(path, file)
4351

4452
def exists(self, path):
4553
try:
@@ -99,3 +107,9 @@ def copy(self, source, destination):
99107
def move(self, source, destination):
100108
self.copy(source, destination)
101109
self.delete(source)
110+
111+
def content_type(self, path):
112+
content_type, _ = mimetypes.guess_type(path)
113+
if not content_type:
114+
content_type = 'application/octet-stream'
115+
return content_type

0 commit comments

Comments
 (0)
Please sign in to comment.