本文整理汇总了Python中thumbor.engines.BaseEngine.get_mimetype方法的典型用法代码示例。如果您正苦于以下问题:Python BaseEngine.get_mimetype方法的具体用法?Python BaseEngine.get_mimetype怎么用?Python BaseEngine.get_mimetype使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类thumbor.engines.BaseEngine
的用法示例。
在下文中一共展示了BaseEngine.get_mimetype方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: post
# 需要导入模块: from thumbor.engines import BaseEngine [as 别名]
# 或者: from thumbor.engines.BaseEngine import get_mimetype [as 别名]
def post(self):
# Check if the image uploaded is a multipart/form-data
if self.multipart_form_data():
file_data = self.request.files['media'][0]
body = file_data['body']
# Retrieve filename from 'filename' field
filename = file_data['filename']
else:
body = self.request.body
# Retrieve filename from 'Slug' header
filename = self.request.headers.get('Slug')
# Check if the image uploaded is valid
if self.validate(body):
# Use the default filename for the uploaded images
if not filename:
content_type = self.request.headers.get('Content-Type', BaseEngine.get_mimetype(body))
extension = mimetypes.guess_extension(content_type.split(';',1)[0], False)
if extension is None: # Content-Type is unknown, try with body
extension = mimetypes.guess_extension(BaseEngine.get_mimetype(body), False)
if extension == '.jpe':
extension = '.jpg' # Hack because mimetypes return .jpe by default
if extension is None: # Even body is unknown, return an empty string to be contat
extension = ''
filename = self.context.config.UPLOAD_DEFAULT_FILENAME + extension
# Build image id based on a random uuid (32 characters)
id = str(uuid.uuid4().hex)
self.write_file(id, body)
self.set_status(201)
self.set_header('Location', self.location(id, filename))
示例2: _fetch
# 需要导入模块: from thumbor.engines import BaseEngine [as 别名]
# 或者: from thumbor.engines.BaseEngine import get_mimetype [as 别名]
def _fetch(self, url):
storage = self.context.modules.storage
buffer = yield gen.maybe_future(storage.get(url))
mime = None
if buffer is not None:
self.context.statsd_client.incr('storage.hit')
mime = BaseEngine.get_mimetype(buffer)
self.context.request.extension = EXTENSION.get(mime, '.jpg')
if mime == 'image/gif' and self.context.config.USE_GIFSICLE_ENGINE:
self.context.request.engine = self.context.modules.gif_engine
else:
self.context.request.engine = self.context.modules.engine
raise gen.Return([False, buffer, None])
else:
self.context.statsd_client.incr('storage.miss')
buffer = yield self.context.modules.loader.load(self.context, url)
if buffer is None:
raise gen.Return([False, None, None])
if mime is None:
mime = BaseEngine.get_mimetype(buffer)
if mime is None:
raise gen.Return([False, None, None])
self.context.request.extension = EXTENSION.get(mime, '.jpg')
original_preserve = self.context.config.PRESERVE_EXIF_INFO
self.context.config.PRESERVE_EXIF_INFO = True
try:
mime = BaseEngine.get_mimetype(buffer)
self.context.request.extension = extension = EXTENSION.get(mime, None)
if mime == 'image/gif' and self.context.config.USE_GIFSICLE_ENGINE:
self.context.request.engine = self.context.modules.gif_engine
else:
self.context.request.engine = self.context.modules.engine
self.context.request.engine.load(buffer, extension)
normalized = self.context.request.engine.normalize()
is_no_storage = isinstance(storage, NoStorage)
is_mixed_storage = isinstance(storage, MixedStorage)
is_mixed_no_file_storage = is_mixed_storage and isinstance(storage.file_storage, NoStorage)
if not (is_no_storage or is_mixed_no_file_storage):
buffer = self.context.request.engine.read(extension)
storage.put(url, buffer)
storage.put_crypto(url)
finally:
self.context.config.PRESERVE_EXIF_INFO = original_preserve
raise gen.Return([normalized, None, self.context.request.engine])
示例3: define_image_type
# 需要导入模块: from thumbor.engines import BaseEngine [as 别名]
# 或者: from thumbor.engines.BaseEngine import get_mimetype [as 别名]
def define_image_type(self, context, result):
if result is not None:
image_extension = BaseEngine.get_mimetype(result)
elif context.config.AUTO_WEBP and context.request.accepts_webp and not context.modules.engine.is_multiple():
image_extension = ".webp"
else:
image_extension = context.request.format
if image_extension is None:
image_extension = context.modules.engine.extension
logger.debug("No image format specified. Retrieving from the image extension: %s." % image_extension)
else:
image_extension = ".%s" % image_extension
logger.debug("Image format specified as %s." % image_extension)
content_type = CONTENT_TYPE.get(image_extension, CONTENT_TYPE[".jpg"])
if context.request.meta:
context.request.meta_callback = (
context.config.META_CALLBACK_NAME or self.request.arguments.get("callback", [None])[0]
)
content_type = "text/javascript" if context.request.meta_callback else "application/json"
logger.debug("Metadata requested. Serving content type of %s." % content_type)
logger.debug("Content Type of %s detected." % content_type)
return image_extension, content_type
示例4: define_image_type
# 需要导入模块: from thumbor.engines import BaseEngine [as 别名]
# 或者: from thumbor.engines.BaseEngine import get_mimetype [as 别名]
def define_image_type(self, context, result):
if result is not None:
if isinstance(result, ResultStorageResult):
buffer = result.buffer
else:
buffer = result
image_extension = EXTENSION.get(BaseEngine.get_mimetype(buffer), ".jpg")
else:
image_extension = context.request.format
if image_extension is not None:
image_extension = ".%s" % image_extension
logger.debug("Image format specified as %s." % image_extension)
elif self.is_webp(context):
image_extension = ".webp"
logger.debug("Image format set by AUTO_WEBP as %s." % image_extension)
else:
image_extension = context.request.engine.extension
logger.debug("No image format specified. Retrieving from the image extension: %s." % image_extension)
content_type = CONTENT_TYPE.get(image_extension, CONTENT_TYPE[".jpg"])
if context.request.meta:
context.request.meta_callback = (
context.config.META_CALLBACK_NAME or self.request.arguments.get("callback", [None])[0]
)
content_type = "text/javascript" if context.request.meta_callback else "application/json"
logger.debug("Metadata requested. Serving content type of %s." % content_type)
logger.debug("Content Type of %s detected." % content_type)
return (image_extension, content_type)
示例5: handle_loader_loaded
# 需要导入模块: from thumbor.engines import BaseEngine [as 别名]
# 或者: from thumbor.engines.BaseEngine import get_mimetype [as 别名]
def handle_loader_loaded(buffer):
if buffer is None:
callback(False, None)
return
original_preserve = self.context.config.PRESERVE_EXIF_INFO
self.context.config.PRESERVE_EXIF_INFO = True
try:
mime = BaseEngine.get_mimetype(buffer)
if mime == '.gif' and self.context.config.USE_GIFSICLE_ENGINE:
self.context.request.engine = GifEngine(self.context)
else:
self.context.request.engine = self.context.modules.engine
self.context.request.engine.load(buffer, extension)
normalized = self.context.request.engine.normalize()
is_no_storage = isinstance(storage, NoStorage)
is_mixed_storage = isinstance(storage, MixedStorage)
is_mixed_no_file_storage = is_mixed_storage and isinstance(storage.file_storage, NoStorage)
if not (is_no_storage or is_mixed_no_file_storage):
buffer = self.context.request.engine.read()
storage.put(url, buffer)
storage.put_crypto(url)
finally:
self.context.config.PRESERVE_EXIF_INFO = original_preserve
callback(normalized, engine=self.context.request.engine)
示例6: define_image_type
# 需要导入模块: from thumbor.engines import BaseEngine [as 别名]
# 或者: from thumbor.engines.BaseEngine import get_mimetype [as 别名]
def define_image_type(self, context, result):
if result is not None:
image_extension = BaseEngine.get_mimetype(result)
else:
image_extension = context.request.format
if image_extension is not None:
image_extension = '.%s' % image_extension
logger.debug('Image format specified as %s.' % image_extension)
elif context.config.AUTO_WEBP and context.request.accepts_webp and not context.request.engine.is_multiple():
image_extension = '.webp'
logger.debug('Image format set by AUTO_WEBP as %s.' % image_extension)
else:
image_extension = context.request.engine.extension
logger.debug('No image format specified. Retrieving from the image extension: %s.' % image_extension)
content_type = CONTENT_TYPE.get(image_extension, CONTENT_TYPE['.jpg'])
if context.request.meta:
context.request.meta_callback = context.config.META_CALLBACK_NAME or self.request.arguments.get('callback', [None])[0]
content_type = 'text/javascript' if context.request.meta_callback else 'application/json'
logger.debug('Metadata requested. Serving content type of %s.' % content_type)
logger.debug('Content Type of %s detected.' % content_type)
return image_extension, content_type
示例7: get
# 需要导入模块: from thumbor.engines import BaseEngine [as 别名]
# 或者: from thumbor.engines.BaseEngine import get_mimetype [as 别名]
def get(self, callback):
path = self.context.request.url
file_abspath = self.normalize_path(path)
if not self.validate_path(file_abspath):
logger.warn("[RESULT_STORAGE] unable to read from outside root path: %s" % file_abspath)
return None
logger.debug("[RESULT_STORAGE] getting from %s" % file_abspath)
if not exists(file_abspath) or self.is_expired(file_abspath):
logger.debug("[RESULT_STORAGE] image not found at %s" % file_abspath)
callback(None)
else:
with open(file_abspath, 'r') as f:
buffer = f.read()
result = ResultStorageResult(
buffer=buffer,
metadata={
'LastModified': datetime.fromtimestamp(getmtime(file_abspath)).replace(tzinfo=pytz.utc),
'ContentLength': len(buffer),
'ContentType': BaseEngine.get_mimetype(buffer)
}
)
callback(result)
示例8: put
# 需要导入模块: from thumbor.engines import BaseEngine [as 别名]
# 或者: from thumbor.engines.BaseEngine import get_mimetype [as 别名]
def put(self, path, data, metadata={}, reduced_redundancy=False, encrypt_key=False, callback=None):
"""
Stores data at given path
:param string path: Path or 'key' for created/updated object
:param bytes data: Data to write
:param dict metadata: Metadata to store with this data
:param bool reduced_redundancy: Whether to reduce storage redundancy or not?
:param bool encrypt_key: Encrypt data?
:param callable callback: Called function once done
"""
storage_class = 'REDUCED_REDUNDANCY' if reduced_redundancy else 'STANDARD'
content_type = BaseEngine.get_mimetype(data) or 'application/octet-stream'
args = dict(
callback=callback,
Bucket=self._bucket,
Key=self._clean_key(path),
Body=data,
ContentType=content_type,
Metadata=metadata,
StorageClass=storage_class,
)
if encrypt_key:
args['ServerSideEncryption'] = 'AES256'
self._put_client.call(**args)
示例9: define_image_type
# 需要导入模块: from thumbor.engines import BaseEngine [as 别名]
# 或者: from thumbor.engines.BaseEngine import get_mimetype [as 别名]
def define_image_type(self, context, result):
if result is not None:
if isinstance(result, ResultStorageResult):
buffer = result.buffer
else:
buffer = result
image_extension = EXTENSION.get(BaseEngine.get_mimetype(buffer), '.jpg')
else:
image_extension = context.request.format
if image_extension is not None:
image_extension = '.%s' % image_extension
logger.debug('Image format specified as %s.' % image_extension)
elif self.is_webp(context):
image_extension = '.webp'
logger.debug('Image format set by AUTO_WEBP as %s.' % image_extension)
elif self.can_auto_convert_png_to_jpg():
image_extension = '.jpg'
logger.debug('Image format set by AUTO_PNG_TO_JPG as %s.' % image_extension)
else:
image_extension = context.request.engine.extension
logger.debug('No image format specified. Retrieving from the image extension: %s.' % image_extension)
content_type = CONTENT_TYPE.get(image_extension, CONTENT_TYPE['.jpg'])
if context.request.meta:
context.request.meta_callback = context.config.META_CALLBACK_NAME or self.request.arguments.get('callback', [None])[0]
content_type = 'text/javascript' if context.request.meta_callback else 'application/json'
logger.debug('Metadata requested. Serving content type of %s.' % content_type)
logger.debug('Content Type of %s detected.' % content_type)
return (image_extension, content_type)
示例10: execute_image_operations
# 需要导入模块: from thumbor.engines import BaseEngine [as 别名]
# 或者: from thumbor.engines.BaseEngine import get_mimetype [as 别名]
def execute_image_operations(self):
self.context.request.quality = None
req = self.context.request
conf = self.context.config
req.extension = splitext(req.image_url)[-1].lower()
should_store = self.context.config.RESULT_STORAGE_STORES_UNSAFE or not self.context.request.unsafe
if self.context.modules.result_storage and should_store:
result = self.context.modules.result_storage.get()
if result is not None:
mime = BaseEngine.get_mimetype(result)
if mime == '.gif' and self.context.config.USE_GIFSICLE_ENGINE:
self.context.request.engine = GifEngine(self.context)
else:
self.context.request.engine = self.context.modules.engine
logger.debug('[RESULT_STORAGE] IMAGE FOUND: %s' % req.url)
self.finish_request(self.context, result)
return
if conf.MAX_WIDTH and (not isinstance(req.width, basestring)) and req.width > conf.MAX_WIDTH:
req.width = conf.MAX_WIDTH
if conf.MAX_HEIGHT and (not isinstance(req.height, basestring)) and req.height > conf.MAX_HEIGHT:
req.height = conf.MAX_HEIGHT
req.meta_callback = conf.META_CALLBACK_NAME or self.request.arguments.get('callback', [None])[0]
self.filters_runner = self.context.filters_factory.create_instances(self.context, self.context.request.filters)
self.filters_runner.apply_filters(thumbor.filters.PHASE_PRE_LOAD, self.get_image)
示例11: _load_results
# 需要导入模块: from thumbor.engines import BaseEngine [as 别名]
# 或者: from thumbor.engines.BaseEngine import get_mimetype [as 别名]
def _load_results(self, context):
image_extension, content_type = self.define_image_type(context, None)
quality = self.context.request.quality
if quality is None:
if image_extension == '.webp' and self.context.config.WEBP_QUALITY is not None:
quality = self.context.config.get('WEBP_QUALITY')
else:
quality = self.context.config.QUALITY
results = context.request.engine.read(image_extension, quality)
if context.request.max_bytes is not None:
results = self.reload_to_fit_in_kb(
context.request.engine,
results,
image_extension,
quality,
context.request.max_bytes
)
if not context.request.meta:
results = self.optimize(context, image_extension, results)
# An optimizer might have modified the image format.
content_type = BaseEngine.get_mimetype(results)
return results, content_type
示例12: validate
# 需要导入模块: from thumbor.engines import BaseEngine [as 别名]
# 或者: from thumbor.engines.BaseEngine import get_mimetype [as 别名]
def validate(self, body):
conf = self.context.config
mime = BaseEngine.get_mimetype(body)
if mime == 'image/gif' and self.context.config.USE_GIFSICLE_ENGINE:
engine = self.context.modules.gif_engine
else:
engine = self.context.modules.engine
# Check if image is valid
try:
engine.load(body, None)
except IOError:
self._error(415, 'Unsupported Media Type')
return False
# Check weight constraints
if (conf.UPLOAD_MAX_SIZE != 0 and len(self.request.body) > conf.UPLOAD_MAX_SIZE):
self._error(
412,
'Image exceed max weight (Expected : %s, Actual : %s)' % (conf.UPLOAD_MAX_SIZE, len(self.request.body)))
return False
# Check size constraints
size = engine.size
if (conf.MIN_WIDTH > size[0] or conf.MIN_HEIGHT > size[1]):
self._error(
412,
'Image is too small (Expected: %s/%s , Actual : %s/%s) % (conf.MIN_WIDTH, conf.MIN_HEIGHT, size[0], size[1])')
return False
return True
示例13: test_auto_should_optimize_jpeg_to_jpeg
# 需要导入模块: from thumbor.engines import BaseEngine [as 别名]
# 或者: from thumbor.engines.BaseEngine import get_mimetype [as 别名]
def test_auto_should_optimize_jpeg_to_jpeg(self):
optimizer = AutoOptimizer(self.get_context())
temp = tempfile.NamedTemporaryFile()
optimizer.optimize(None, fixtures_folder + '/img/bend.jpg', temp.name)
temp_buffer = open(temp.name).read()
self.assertTrue(BaseEngine.get_mimetype(temp_buffer) == 'image/jpeg', "MIME type should be image/jpeg")
示例14: _fetch
# 需要导入模块: from thumbor.engines import BaseEngine [as 别名]
# 或者: from thumbor.engines.BaseEngine import get_mimetype [as 别名]
def _fetch(self, url, extension, callback):
storage = self.context.modules.storage
buffer = storage.get(url)
if buffer is not None:
self.context.statsd_client.incr('storage.hit')
mime = BaseEngine.get_mimetype(buffer)
if mime == 'image/gif' and self.context.config.USE_GIFSICLE_ENGINE:
self.context.request.engine = self.context.modules.gif_engine
else:
self.context.request.engine = self.context.modules.engine
callback(False, buffer=buffer)
else:
self.context.statsd_client.incr('storage.miss')
def handle_loader_loaded(buffer):
if buffer is None:
callback(False, None)
return
original_preserve = self.context.config.PRESERVE_EXIF_INFO
self.context.config.PRESERVE_EXIF_INFO = True
try:
mime = BaseEngine.get_mimetype(buffer)
extension = EXTENSION.get(mime,None)
if mime == 'image/gif' and self.context.config.USE_GIFSICLE_ENGINE:
self.context.request.engine = self.context.modules.gif_engine
else:
self.context.request.engine = self.context.modules.engine
self.context.request.engine.load(buffer, extension)
normalized = self.context.request.engine.normalize()
is_no_storage = isinstance(storage, NoStorage)
is_mixed_storage = isinstance(storage, MixedStorage)
is_mixed_no_file_storage = is_mixed_storage and isinstance(storage.file_storage, NoStorage)
if not (is_no_storage or is_mixed_no_file_storage):
buffer = self.context.request.engine.read(extension)
storage.put(url, buffer)
storage.put_crypto(url)
finally:
self.context.config.PRESERVE_EXIF_INFO = original_preserve
callback(normalized, engine=self.context.request.engine)
self.context.modules.loader.load(self.context, url, handle_loader_loaded)
示例15: parse_image
# 需要导入模块: from thumbor.engines import BaseEngine [as 别名]
# 或者: from thumbor.engines.BaseEngine import get_mimetype [as 别名]
def parse_image(self, image_buffer):
tmp = BytesIO(image_buffer)
result = BytesIO()
img = PilImage.open(tmp)
ext = BaseEngine.get_mimetype(image_buffer)
try:
img.load()
except IOError:
pass
img.save(result, FORMATS.get(ext, FORMATS['.jpg']))
result_bytes = result.getvalue()
result.close()
tmp.close()
return result_bytes