Refactor: file api in to seperate api app & use drf api views
This commit is contained in:
parent
1f75b1bf17
commit
bd2791c155
6 changed files with 150 additions and 71 deletions
32
backend/api/files/serializers.py
Normal file
32
backend/api/files/serializers.py
Normal file
|
@ -0,0 +1,32 @@
|
|||
from rest_framework import serializers
|
||||
from apps.files.models import PostFileModel
|
||||
|
||||
|
||||
class PostFileSerializer(serializers.ModelSerializer):
|
||||
"""Serializer for PostFileModel."""
|
||||
|
||||
class Meta:
|
||||
model = PostFileModel
|
||||
fields = ["hash_blake3", "file_type", "file", "thumbnail"]
|
||||
# Add any other fields you need
|
||||
|
||||
def to_representation(self, instance):
|
||||
"""Customize the representation of the model."""
|
||||
representation = super().to_representation(instance)
|
||||
|
||||
# Add file name from related model
|
||||
try:
|
||||
representation["filename"] = instance.name.first().filename
|
||||
except (AttributeError, IndexError):
|
||||
representation["filename"] = "Unknown"
|
||||
|
||||
# Add URLs for different thumbnail sizes
|
||||
base_url = f"/api/files/{instance.hash_blake3}/"
|
||||
thumbnails = {}
|
||||
for size_key in THUMBNAIL_SIZES:
|
||||
thumbnails[size_key] = f"{base_url}?t={size_key}"
|
||||
|
||||
representation["thumbnails"] = thumbnails
|
||||
representation["download_url"] = f"{base_url}?d=0"
|
||||
|
||||
return representation
|
Loading…
Add table
Add a link
Reference in a new issue