def format_from_ident(self, ident, potential_format): if self.default_format is not None: return self.default_format elif potential_format is not None: return potential_format elif ident.rfind('.') != -1 and (len(ident) - ident.rfind('.') <= 5): return ident.split('.')[-1] else: public_message = 'Format could not be determined for: %s.' % ( ident, ) log_message = 'Format could not be determined for: %s.' % (ident) logger.warn(log_message) raise ResolverException(404, public_message)
def __init__(self, config): super(AlmaS3Resolver, self).__init__(config) self.public_key_file = self.config.get('public_key_file') self.default_format = self.config.get('default_format', None) self.cache_root = self.config.get('cache_root') self._file_info = None logger.debug('initialize AlmaS3Resolver') try: assert self.cache_root and self.public_key_file except: message = 'Server Side Error: Configuration incomplete and cannot resolve. Settings required for cache_root and public_key_file.' logger.error(message) raise ResolverException(500, message)
def __init__(self, config): super(SimpleHTTPResolver, self).__init__(config) self.source_prefix = self.config.get('source_prefix', '') self.source_suffix = self.config.get('source_suffix', '') self.default_format = self.config.get('default_format', None) self.head_resolvable = self.config.get('head_resolvable', False) self.uri_resolvable = self.config.get('uri_resolvable', False) self.user = self.config.get('user', None) self.pw = self.config.get('pw', None) self.cert = self.config.get('cert', None) self.key = self.config.get('key', None) self.ssl_check = self.config.get('ssl_check', True) self.ident_regex = self.config.get('ident_regex', False) if 'cache_root' in self.config: self.cache_root = self.config['cache_root'] else: message = 'Server Side Error: Configuration incomplete and cannot resolve. Missing setting for cache_root.' logger.error(message) raise ResolverException(500, message) if not self.uri_resolvable and self.source_prefix == '': message = 'Server Side Error: Configuration incomplete and cannot resolve. Must either set uri_resolvable' \ ' or source_prefix settings.' logger.error(message) raise ResolverException(500, message)
def resolve(self, ident): # For this dumb version a constant path is prepended to the identfier # supplied to get the path It assumes this 'identifier' ends with a file # extension from which the format is then derived. ident = unquote(ident) fp = join(self.cache_root, ident) logger.debug('src image: %s' % (fp,)) if not exists(fp): public_message = 'Source image not found for identifier: %s.' % (ident,) log_message = 'Source image not found at %s for identifier: %s.' % (fp,ident) logger.warn(log_message) raise ResolverException(404, public_message) format = SimpleFSResolver._format_from_ident(ident) logger.debug('src format %s' % (format,)) return (fp, format)
def raise_404_for_ident(self, ident): message = 'Source image not found for identifier: %s.' % (ident, ) logger.warn(message) raise ResolverException(404, message)
def raise_404_for_ident(self, ident): message = 'Image not found for identifier: %s.' % (ident) raise ResolverException(404, message)
def raise_404_for_ident(self, ident): source_fp = self.source_file_path(ident) public_message = 'Source image not found for identifier: %s.' % (ident,) log_message = 'Source image not found at %s for identifier: %s.' % (source_fp,ident) logger.warn(log_message) raise ResolverException(404, public_message)
def resolve(self, ident): ident = unquote(ident) local_fp = join(self.cache_root, SimpleHTTPResolver._cache_subroot(ident)) local_fp = join(local_fp) if exists(local_fp): cached_object = glob.glob(join(local_fp, 'loris_cache.*')) if len(cached_object) > 0: cached_object = cached_object[0] else: public_message = 'Cached image not found for identifier: %s.' % ( ident) log_message = 'Cached image not found for identifier: %s. Empty directory where image expected?' % ( ident) logger.warn(log_message) raise ResolverException(404, public_message) format = self.format_from_ident(cached_object, None) logger.debug('src image from local disk: %s' % (cached_object, )) return (cached_object, format) else: fp = self._web_request_url(ident) logger.debug('src image: %s' % (fp, )) try: response = requests.get(fp, stream=True, **self.request_options()) except requests.exceptions.MissingSchema: public_message = 'Bad URL request made for identifier: %s.' % ( ident, ) log_message = 'Bad URL request at %s for identifier: %s.' % ( fp, ident) logger.warn(log_message) raise ResolverException(404, public_message) if response.status_code != 200: public_message = 'Source image not found for identifier: %s. Status code returned: %s' % ( ident, response.status_code) log_message = 'Source image not found at %s for identifier: %s. Status code returned: %s' % ( fp, ident, response.status_code) logger.warn(log_message) raise ResolverException(404, public_message) if 'content-type' in response.headers: try: format = self.format_from_ident( ident, constants.FORMATS_BY_MEDIA_TYPE[ response.headers['content-type']]) except KeyError: logger.warn( 'Your server may be responding with incorrect content-types. Reported %s for ident %s.' % (response.headers['content-type'], ident)) #Attempt without the content-type format = self.format_from_ident(ident, None) else: format = self.format_from_ident(ident, None) logger.debug('src format %s' % (format, )) local_fp = join(local_fp, "loris_cache." + format) try: makedirs(dirname(local_fp)) except: logger.debug( "Directory already existed... possible problem if not a different format" ) with open(local_fp, 'wb') as fd: for chunk in response.iter_content(2048): fd.write(chunk) logger.info("Copied %s to %s" % (fp, local_fp)) return (local_fp, format)