from django.conf import settings
from django.core.exceptions import PermissionDenied
from django.http import Http404
from django.views.decorators.cache import never_cache

from easy_thumbnails.files import ThumbnailFile

from .. import settings as filer_settings
from ..models import File
from ..utils.filer_easy_thumbnails import thumbnail_to_original_filename


server = filer_settings.FILER_PRIVATEMEDIA_SERVER
thumbnail_server = filer_settings.FILER_PRIVATEMEDIA_THUMBNAIL_SERVER


@never_cache
def serve_protected_file(request, path):
    """
    Serve protected files to authenticated users with read permissions.
    """
    try:
        file_obj = File.objects.get(file=path, is_public=False)
    except File.DoesNotExist:
        raise Http404('File not found')
    if not file_obj.has_read_permission(request):
        if settings.DEBUG:
            raise PermissionDenied
        else:
            raise Http404('File not found')
    return server.serve(request, file_obj=file_obj, save_as=False)


@never_cache
def serve_protected_thumbnail(request, path):
    """
    Serve protected thumbnails to authenticated users.
    If the user doesn't have read permissions, redirect to a static image.
    """
    source_path = thumbnail_to_original_filename(path)
    if not source_path:
        raise Http404('File not found')
    try:
        file_obj = File.objects.get(file=source_path, is_public=False)
    except File.DoesNotExist:
        raise Http404('File not found')
    if not file_obj.has_read_permission(request):
        if settings.DEBUG:
            raise PermissionDenied
        else:
            raise Http404('File not found')
    try:
        thumbnail = ThumbnailFile(name=path, storage=file_obj.file.thumbnail_storage)
        thumbnail_temp_file = File(file=thumbnail, mime_type=file_obj.mime_type)
        return thumbnail_server.serve(request, thumbnail_temp_file, save_as=False)
    except Exception:
        raise Http404('File not found')

filer/server/views.py
from django.conf import settings
from django.core.exceptions import PermissionDenied
from django.http import Http404
from django.views.decorators.cache import never_cache

from easy_thumbnails.files import ThumbnailFile

from .. import settings as filer_settings
from ..models import File
from ..utils.filer_easy_thumbnails import thumbnail_to_original_filename


server = filer_settings.FILER_PRIVATEMEDIA_SERVER
thumbnail_server = filer_settings.FILER_PRIVATEMEDIA_THUMBNAIL_SERVER


@never_cache
def serve_protected_file(request, path):
    """
    Serve protected files to authenticated users with read permissions.
    """
    try:
        file_obj = File.objects.get(file=path, is_public=False)
    except File.DoesNotExist:
        raise Http404('File not found')
    if not file_obj.has_read_permission(request):
        if settings.DEBUG:
            raise PermissionDenied
        else:
            raise Http404('File not found')
    return server.serve(request, file_obj, save_as=False)


@never_cache
def serve_protected_thumbnail(request, path):
    """
    Serve protected thumbnails to authenticated users.
    If the user doesn't have read permissions, redirect to a static image.
    """
    source_path = thumbnail_to_original_filename(path)
    if not source_path:
        raise Http404('File not found')
    try:
        file_obj = File.objects.get(file=source_path, is_public=False)
    except File.DoesNotExist:
        raise Http404('File not found')
    if not file_obj.has_read_permission(request):
        if settings.DEBUG:
            raise PermissionDenied
        else:
            raise Http404('File not found')
    try:
        thumbnail = ThumbnailFile(name=path, storage=file_obj.file.thumbnail_storage)
        thumbnail_temp_file = File(file=thumbnail, mime_type=file_obj.mime_type)
        return thumbnail_server.serve(request, thumbnail_temp_file, save_as=False)
    except Exception:
        raise Http404('File not found')

filer/server/views.py
diff --git a/filer/server/views.py b/filer/server/views.py
index 5b87abb6d..df7762fff 100644
--- a/filer/server/views.py
+++ b/filer/server/views.py
@@ -28,7 +28,7 @@ def serve_protected_file(request, path):
             raise PermissionDenied
         else:
             raise Http404('File not found')
-    return server.serve(request, file_obj=file_obj, save_as=False)
+    return server.serve(request, file_obj, save_as=False)
 
 
 @never_cache

filter.server.views `serve() missing 1 required positional argument: 'filer_file'`
A bug was introduced here when `filer_file` was made into a kwarg:

https://github.com/divio/django-filer/blame/29eea047f425254af9f4d68b24096fc3fd3a7197/filer/server/views.py#L31

Exception:
```
TypeError at /smedia/filer/filer_private/2d/ba/2dbaf742-4b45-456f-95b1-f560ec64e637/xxx.png
serve() missing 1 required positional argument: 'filer_file'


/usr/local/lib/python3.8/site-packages/filer/server/views.py, line 31, in serve_protected_file
    return server.serve(request, file_obj=file_obj, save_as=False) 

Variable | Value
-- | --
file_obj | <File:xxxpng>
path | 'filer_private/2d/ba/2dbaf742-4b45-456f-95b1-f560ec64e637/xxx.png'
request | <WSGIRequest: GET '/smedia/filer/filer_private/2d/ba/2dbaf742-4b45-456f-95b1-f560ec64e637/axxx.png'>
```
