@@ -39,46 +39,50 @@ class Meta:
39
39
fields = "__all__"
40
40
41
41
42
- # MARK: Clear out image meta data. Used in Image Serializer, below.
42
+ # MARK: Clear Metadata
43
+
44
+
43
45
def scrub_exif (image_file : InMemoryUploadedFile ) -> InMemoryUploadedFile :
44
46
"""
45
- Remove EXIF data from JPEGs and text metadata from PNGs.
47
+ Remove EXIF metadata from JPEGs and text metadata from PNGs.
46
48
"""
47
49
try :
48
50
img : PILImage .Image = PILImage .open (image_file )
49
51
output_format = img .format
50
52
51
53
if output_format == "JPEG" :
52
54
img = img .convert ("RGB" )
55
+
53
56
elif output_format == "PNG" :
54
57
img = img .copy ()
55
58
img .info = {}
59
+
56
60
else :
57
- return image_file # Return as-is if it's not JPEG or PNG
61
+ return image_file # return as-is if it's not JPEG or PNG
58
62
59
- # Save the cleaned image into a buffer
63
+ # Save the cleaned image into a buffer.
60
64
output = BytesIO ()
61
65
img .save (
62
66
output ,
63
67
format = output_format ,
64
- quality = 95 if output_format == "JPEG" else None , # Set JPEG quality
65
- optimize = True if output_format == "JPEG" else False , # Optimize JPEG
68
+ quality = 95 if output_format == "JPEG" else None , # set JPEG quality
69
+ optimize = output_format == "JPEG" , # optimize JPEG
66
70
)
67
71
output .seek (0 )
68
72
69
73
# Return a new InMemoryUploadedFile
70
74
return InMemoryUploadedFile (
71
75
output ,
72
- image_file .field_name , # Use original field name
76
+ image_file .field_name , # use original field name
73
77
image_file .name ,
74
78
f"image/{ output_format .lower ()} " ,
75
79
output .getbuffer ().nbytes ,
76
- image_file .charset , # Preserve charset (if applicable)
80
+ image_file .charset , # preserve charset (if applicable)
77
81
)
78
82
79
83
except Exception as e :
80
84
print (f"Error scrubbing EXIF: { e } " )
81
- return image_file # Return original file in case of error
85
+ return image_file # return original file in case of error
82
86
83
87
84
88
class ImageSerializer (serializers .ModelSerializer [Image ]):
@@ -92,8 +96,7 @@ def validate(self, data: Dict[str, UploadedFile]) -> Dict[str, UploadedFile]:
92
96
raise serializers .ValidationError ("No file was submitted." )
93
97
94
98
# DATA_UPLOAD_MAX_MEMORY_SIZE and IMAGE_UPLOAD_MAX_FILE_SIZE are set in core/settings.py.
95
- # For whatever reason, the file size limit is not being enforced. To get around this,
96
- # we're checking the file size here.
99
+ # The file size limit is not being enforced. We're checking the file size here.
97
100
if (
98
101
data ["file_object" ].size is not None
99
102
and data ["file_object" ].size > settings .IMAGE_UPLOAD_MAX_FILE_SIZE
@@ -111,10 +114,10 @@ def create(self, validated_data: Dict[str, Any]) -> Image:
111
114
if file_obj := request .FILES .get ("file_object" ):
112
115
validated_data ["file_object" ] = scrub_exif (file_obj )
113
116
114
- # Create the image instance
117
+ # Create the image instance.
115
118
image = super ().create (validated_data )
116
119
117
- # Handle organization image indexing if applicable
120
+ # Handle organization image indexing if applicable.
118
121
if organization_id := request .data .get ("organization_id" ):
119
122
next_index = OrganizationImage .objects .filter (
120
123
org_id = organization_id
0 commit comments