2025-01-18 20:06:12 -05:00
|
|
|
from adventures.models import AdventureImage, Attachment
|
|
|
|
|
|
|
|
protected_paths = ['images/', 'attachments/']
|
|
|
|
|
|
|
|
def checkFilePermission(fileId, user, mediaType):
|
|
|
|
if mediaType not in protected_paths:
|
|
|
|
return True
|
|
|
|
if mediaType == 'images/':
|
|
|
|
try:
|
|
|
|
# Construct the full relative path to match the database field
|
|
|
|
image_path = f"images/{fileId}"
|
|
|
|
# Fetch the AdventureImage object
|
|
|
|
adventure = AdventureImage.objects.get(image=image_path).adventure
|
|
|
|
if adventure.is_public:
|
|
|
|
return True
|
|
|
|
elif adventure.user_id == user:
|
|
|
|
return True
|
2025-06-12 15:54:01 -04:00
|
|
|
elif adventure.collections.exists():
|
|
|
|
# Check if the user is in any collection's shared_with list
|
|
|
|
for collection in adventure.collections.all():
|
|
|
|
if collection.shared_with.filter(id=user.id).exists():
|
|
|
|
return True
|
|
|
|
return False
|
2025-01-18 20:06:12 -05:00
|
|
|
else:
|
|
|
|
return False
|
|
|
|
except AdventureImage.DoesNotExist:
|
|
|
|
return False
|
|
|
|
elif mediaType == 'attachments/':
|
|
|
|
try:
|
|
|
|
# Construct the full relative path to match the database field
|
|
|
|
attachment_path = f"attachments/{fileId}"
|
|
|
|
# Fetch the Attachment object
|
2025-06-12 15:54:01 -04:00
|
|
|
attachment = Attachment.objects.get(file=attachment_path)
|
|
|
|
adventure = attachment.adventure
|
|
|
|
if adventure.is_public:
|
2025-01-18 20:06:12 -05:00
|
|
|
return True
|
2025-06-12 15:54:01 -04:00
|
|
|
elif adventure.user_id == user:
|
2025-01-18 20:06:12 -05:00
|
|
|
return True
|
2025-06-12 15:54:01 -04:00
|
|
|
elif adventure.collections.exists():
|
|
|
|
# Check if the user is in any collection's shared_with list
|
|
|
|
for collection in adventure.collections.all():
|
|
|
|
if collection.shared_with.filter(id=user.id).exists():
|
|
|
|
return True
|
|
|
|
return False
|
2025-01-18 20:06:12 -05:00
|
|
|
else:
|
|
|
|
return False
|
|
|
|
except Attachment.DoesNotExist:
|
|
|
|
return False
|