def __call__(self, context, request): if self.use_subpath: path_tuple = request.subpath else: path_tuple = traversal_path_info(request.environ["PATH_INFO"]) path = _secure_path(path_tuple) if path is None: raise HTTPNotFound("Out of bounds: %s" % request.url) if self.package_name: # package resource resource_path = "%s/%s" % (self.docroot.rstrip("/"), path) if resource_isdir(self.package_name, resource_path): if not request.path_url.endswith("/"): self.add_slash_redirect(request) resource_path = "%s/%s" % (resource_path.rstrip("/"), self.index) if not resource_exists(self.package_name, resource_path): raise HTTPNotFound(request.url) filepath = resource_filename(self.package_name, resource_path) else: # filesystem file # os.path.normpath converts / to \ on windows filepath = normcase(normpath(join(self.norm_docroot, path))) if isdir(filepath): if not request.path_url.endswith("/"): self.add_slash_redirect(request) filepath = join(filepath, self.index) if not exists(filepath): raise HTTPNotFound(request.url) content_type, content_encoding = _guess_type(filepath) return FileResponse(filepath, request, self.cache_max_age, content_type, content_encoding=None)
def __call__(self, context, request): resource_name = self.get_resource_name(request) files = self.get_possible_files(resource_name) filepath, content_encoding = self.find_best_match(request, files) if filepath is None: raise HTTPNotFound(request.url) content_type, _ = _guess_type(resource_name) response = FileResponse( filepath, request, self.cache_max_age, content_type, content_encoding, ) if len(files) > 1: _add_vary(response, 'Accept-Encoding') return response
def __call__(self, context, request): if self.use_subpath: path_tuple = request.subpath else: path_tuple = traversal_path_info(request.path_info) path = _secure_path(path_tuple) if path is None: raise HTTPNotFound('Out of bounds: %s' % request.url) if self.package_name: # package resource resource_path = '%s/%s' % (self.docroot.rstrip('/'), path) if resource_isdir(self.package_name, resource_path): if not request.path_url.endswith('/'): self.add_slash_redirect(request) resource_path = '%s/%s' % ( resource_path.rstrip('/'), self.index, ) if not resource_exists(self.package_name, resource_path): raise HTTPNotFound(request.url) filepath = resource_filename(self.package_name, resource_path) else: # filesystem file # os.path.normpath converts / to \ on windows filepath = normcase(normpath(join(self.norm_docroot, path))) if isdir(filepath): if not request.path_url.endswith('/'): self.add_slash_redirect(request) filepath = join(filepath, self.index) if not exists(filepath): raise HTTPNotFound(request.url) content_type, content_encoding = _guess_type(filepath) return FileResponse( filepath, request, self.cache_max_age, content_type, content_encoding=None, )
def get(request): image_name = request.matchdict['name'] category_name = request.matchdict['category'] # TODO exceptions parsed_id, variant_name = FileID.parse_name(image_name, category_name) try: Category = registry.get_category(category_name) variant = Category(parsed_id).get_variant(variant_name) except StoreException as e: log.warn('1 %r', e) # TODO raise HTTPNotFound() if variant.exists(): return FileResponse(variant.fs_path(), request=request) try: data, length = variant.generate() except FileException as e: log.warn('2 %r', e) raise HTTPNotFound() except StoreException as e: log.warn('2 %r', e) raise HTTPNotFound() content_type, content_encoding = _guess_type('x.' + variant.parsed_id.ext) return Response( request=request, app_iter=FileIter(data), content_type=content_type, content_encoding=content_encoding, content_length = length #last_modified = now TODO )