<commit_before>import os

from django.conf import settings
from django.core.files.base import File
from django.http import HttpResponse, HttpResponseNotFound
try:
    from django.utils.deprecation import MiddlewareMixin
except ImportError:
    MiddlewareMixin = object

class FileProvider(object):
      def get_response(self, response, **options):
          return self._get_response(response, **options)

      def _get_response(self, response, **options):
          raise NotImplemented

class XAccelFileProvider(FileProvider):
      def _get_response(self, response, **options):
          response['X-Accel-Redirect'] = response['X-File']
          del response['X-File']
          return response

class XSendFileProvider(FileProvider):
      def _get_response(self, response, **options):
          response['X-Sendfile'] = response['X-File']
          del response['X-File']
          return response

class PythonFileProvider(FileProvider):
      def _get_response(self, response, **options):
          # only need this checking when python file provider is given
          if not os.path.exists(response['X-File']):
              return HttpResponseNotFound("file not found")
          with File(open(response['X-File'], 'rb')) as f:
              response.content = f.chunks()
              del response['X-File']
          return response

# Uses X-Sendfile
LightHttpdFileProvider = ApacheFileProvider
# Uses X-Sendfile
HiawathaFileProvider = ApacheFileProvider
# Uses X-Accel-Redirect
CaddyFileProvider = NginxFileProvider

PROVIDERS = {
 'python': PythonFileProvider,
 'nginx': NginxFileProvider,
 'apache': ApacheFileProvider,
 'lighthttpd': LightHttpdFileProvider,
 'caddy': CaddyFileProvider,
 'hiawatha': HiawathaFileProvider,
 'xaccel': NginxFileProvider,
 'xsendfile': ApacheFileProvider,
}

class FileProviderMiddleware(MiddlewareMixin):
    def process_response(self, request, response):
        if response.get('X-File', "") != "":
            provider_name = getattr(settings, "FILEPROVIDER_NAME", "python")
            # provider_option = getattr(settings, "DJM_ENABLE_CACHE", True)
            provider = PROVIDERS[provider_name]
            response =  provider().get_response(response)
        return response
<commit_msg>add support for xaccel & xsendfile<commit_after>import os

from django.conf import settings
from django.core.files.base import File
from django.http import HttpResponse, HttpResponseNotFound
try:
    from django.utils.deprecation import MiddlewareMixin
except ImportError:
    MiddlewareMixin = object

class FileProvider(object):
      def get_response(self, response, **options):
          return self._get_response(response, **options)

      def _get_response(self, response, **options):
          raise NotImplemented

class XAccelFileProvider(FileProvider):
      def _get_response(self, response, **options):
          response['X-Accel-Redirect'] = response['X-File']
          del response['X-File']
          return response

class XSendFileProvider(FileProvider):
      def _get_response(self, response, **options):
          response['X-Sendfile'] = response['X-File']
          del response['X-File']
          return response

class PythonFileProvider(FileProvider):
      def _get_response(self, response, **options):
          # only need this checking when python file provider is given
          if not os.path.exists(response['X-File']):
              return HttpResponseNotFound("file not found")
          with File(open(response['X-File'], 'rb')) as f:
              response.content = f.chunks()
              del response['X-File']
          return response

# Uses X-Sendfile
ApacheFileProvider = XSendFileProvider
LightHttpdFileProvider = XSendFileProvider
HiawathaFileProvider = XSendFileProvider
# Uses X-Accel-Redirect
NginxFileProvider = XAccelFileProvider
CaddyFileProvider = XAccelFileProvider

PROVIDERS = {
 'python': PythonFileProvider,
 'nginx': NginxFileProvider,
 'apache': ApacheFileProvider,
 'lighthttpd': LightHttpdFileProvider,
 'caddy': CaddyFileProvider,
 'hiawatha': HiawathaFileProvider,
 'xaccel': XAccelFileProvider,
 'xsendfile': XSendFileProvider,
}

class FileProviderMiddleware(MiddlewareMixin):
    def process_response(self, request, response):
        if response.get('X-File', "") != "":
            provider_name = getattr(settings, "FILEPROVIDER_NAME", "python")
            # provider_option = getattr(settings, "DJM_ENABLE_CACHE", True)
            provider = PROVIDERS[provider_name]
            response =  provider().get_response(response)
        return response

{'fraction_non_alphanumeric': 0.10118006993006994, 'fraction_numerical': 0.0, 'mean_word_length': 3.679959100204499, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2942993', 'n_tokens_mistral': 1271, 'n_tokens_neox': 1257, 'n_words': 302}