Skip to content

feat: ✨ added cors headers for share images #492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion apps/blob_storage/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AbstractBlobStorage(models.Model):
size_bytes = models.PositiveIntegerField(default=0)
contents = CompressedBinaryField(
max_length=htk_setting('HTK_BLOB_CONTENT_MAX_LENGTH'),
editable=True
editable=True,
Copy link
Contributor Author

@jeffyang400 jeffyang400 Jul 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comma here so that when I click 'Save', it doesn't become a one-liner with a max_length.

)

class Meta:
Expand Down Expand Up @@ -48,6 +48,21 @@ def as_response(self):
self.contents,
content_type=self.content_type,
)

# Add CORS headers for images to allow social media platforms to access them
if self.content_type.startswith('image/'):
response['Access-Control-Allow-Origin'] = '*'
response['Access-Control-Allow-Methods'] = 'GET, HEAD, OPTIONS'
response['Access-Control-Allow-Headers'] = (
'Accept, Accept-Language, Content-Language, Content-Type'
)
response['Access-Control-Max-Age'] = '86400' # 24 hours
# Add cache headers for better performance
response['Cache-Control'] = (
'public, max-age=86400, immutable' # 24 hours
)
response['Vary'] = 'Accept-Encoding'

# response = FileResponse(
# self.contents,
# as_attachment=True,
Expand Down