<commit_before>"""
The fileresource provides a basic restish fileresource for assets and images

Requires ImageMagick for image resizing
"""
import tempfile, os, subprocess, shutil
from restish import http, resource

from formish.filestore import CachedTempFilestore, FileSystemHeaderedFilestore

import logging
log = logging.getLogger('formish')

# Binaries
IDENTIFY = '/usr/bin/identify'
CONVERT = '/usr/bin/convert'


class FileResource(resource.Resource):
    """
    A simple file serving utility
    """

    def __init__(self, filestores=None, filestore=None, segments=None, cache=None):
        if cache:
            self.cache = cache
        else:
            self.cache =  CachedTempFilestore(FileSystemHeaderedFilestore(root_dir='cache'))
        if filestore is not None:
            self.filestores = [filestore]
        elif filestores is not None:
            self.filestores = filestores
        else:
            tempfilestore = CachedTempFilestore(name='tmp')
            filestore = CachedTempFilestore(FileSystemHeaderedFilestore(root_dir='store'), name='store')
            self.filestores = [filestore, tempfilestore]
        self.filestores.append(self.cache)
        self.segments = segments
        self.resize_quality = 70


    @resource.child(resource.any)
    def child(self, request, segments):
        return FileResource(filestores=self.filestores, segments=segments, cache=self.cache), []


    def __call__(self, request):
        """
        Find the appropriate image to return including cacheing and resizing
        """
        if not self.segments:
            return None
        filename = '/'.join(self.segments)

        # get the requested filepath from the segments
        etag = str(request.if_none_match)
        for filestore in self.filestores:
            f = self.get_file(request, filestore, filename, etag)
            if f:
                return f
        return http.not_found()


    def get_file(self, request, filestore, filename, etag):
        """ get the file through the cache and possibly resizing """
        # Check the file is up to date
        try:
            cache_tag, headers, f = filestore.get(filename, etag)
            content_type = dict(headers)['Content-Type']
        except KeyError:
            # XXX if the original is not their, clear resize cache (this would mean a globbing delete every request!)
            # cache_filename = filestore.name+'_'+filename
            # self.cache.delete(cache_filename, glob=True)
            return 
        width, height = get_size_from_dict(request.GET)<commit_msg>Return the resource directly from the child method instead of returning a new resource to handle it.<commit_after>"""
The fileresource provides a basic restish fileresource for assets and images

Requires ImageMagick for image resizing
"""
import tempfile, os, subprocess, shutil
from restish import http, resource

from formish.filestore import CachedTempFilestore, FileSystemHeaderedFilestore

import logging
log = logging.getLogger('formish')

# Binaries
IDENTIFY = '/usr/bin/identify'
CONVERT = '/usr/bin/convert'


class FileResource(resource.Resource):
    """
    A simple file serving utility
    """

    def __init__(self, filestores=None, filestore=None, cache=None):
        if cache:
            self.cache = cache
        else:
            self.cache =  CachedTempFilestore(FileSystemHeaderedFilestore(root_dir='cache'))
        if filestore is not None:
            self.filestores = [filestore]
        elif filestores is not None:
            self.filestores = filestores
        else:
            tempfilestore = CachedTempFilestore(name='tmp')
            filestore = CachedTempFilestore(FileSystemHeaderedFilestore(root_dir='store'), name='store')
            self.filestores = [filestore, tempfilestore]
        self.filestores.append(self.cache)
        self.resize_quality = 70

    @resource.child(resource.any)
    def child(self, request, segments):
        """
        Find the appropriate image to return including cacheing and resizing
        """
        # Build the full filename from the segments.
        filename = '/'.join(segments)
        # get the requested filepath from the segments
        etag = str(request.if_none_match)
        for filestore in self.filestores:
            f = self.get_file(request, filestore, filename, etag)
            if f:
                return f
        return http.not_found()

    def get_file(self, request, filestore, filename, etag):
        """ get the file through the cache and possibly resizing """
        # Check the file is up to date
        try:
            cache_tag, headers, f = filestore.get(filename, etag)
            content_type = dict(headers)['Content-Type']
        except KeyError:
            # XXX if the original is not their, clear resize cache (this would mean a globbing delete every request!)
            # cache_filename = filestore.name+'_'+filename
            # self.cache.delete(cache_filename, glob=True)
            return 
        width, height = get_size_from_dict(request.GET)
{'fraction_non_alphanumeric': 0.08425196850393701, 'fraction_numerical': 0.0007874015748031496, 'mean_word_length': 2.665945165945166, 'pattern_counts': {'":': 0, '<': 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': '1645143', 'n_tokens_mistral': 1426, 'n_tokens_neox': 1403, 'n_words': 407}