Example #1
0
    def get_file(self, path):
        path = clean_path(path)

        real_filename, file_loader = self.get_filename_and_loader(path)
        if file_loader is None or not self.is_allowed(real_filename):
            return None

        guessed_type = mimetypes.guess_type(real_filename)
        mimetype = guessed_type[0] or self.fallback_mimetype
        f, mtime, file_size = file_loader()
        return File(f, real_filename, mtime, file_size, mimetype)
Example #2
0
File: smart.py Project: btubbs/spa
    def convert_css_url(self, css_url):
        split_url = urlsplit(css_url)
        url_path = split_url.path
        if not url_path.startswith('/'):
            abs_url_path = self.make_path_absolute(url_path)
        else:
            abs_url_path = posixpath.realpath(url_path)

        prefix = self.get_url_prefix()

        # now make the path as it would be passed in to this handler when
        # requested from the web.  From there we can use existing methods on the
        # class to resolve to a real file.
        _, _, content_filepath = abs_url_path.partition(prefix)
        content_filepath = clean_path(content_filepath)

        content_file_hash = self.hash_cache.get_path_hash(content_filepath)
        if content_file_hash is None:
            content_file = self.get_file(content_filepath)
            if content_file is None:
                return 'NOT FOUND: "%s"' % url_path
            try:
                content_file_hash = get_hash(content_file.handle)
            finally:
                content_file.handle.close()
        parts = list(split_url)
        parts[2] = add_hash_to_filepath(url_path, content_file_hash)

        url = urlunsplit(parts)

        # Special casing for a @font-face hack, like url(myfont.eot?#iefix")
        # http://www.fontspring.com/blog/the-new-bulletproof-font-face-syntax
        if '?#' in css_url:
            parts = list(urlsplit(url))
            if not parts[3]:
                parts[2] += '?'
            url = urlunsplit(parts)
        return url