本文整理汇总了Python中libthumbor.CryptoURL.generate方法的典型用法代码示例。如果您正苦于以下问题:Python CryptoURL.generate方法的具体用法?Python CryptoURL.generate怎么用?Python CryptoURL.generate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类libthumbor.CryptoURL
的用法示例。
在下文中一共展示了CryptoURL.generate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_image_already_generated_by_thumbor_2_times
# 需要导入模块: from libthumbor import CryptoURL [as 别名]
# 或者: from libthumbor.CryptoURL import generate [as 别名]
def test_image_already_generated_by_thumbor_2_times(self):
with open(
normalize_unicode_path(u'./tests/fixtures/images/alabama1_ap620é.jpg'), 'r'
) as f:
self.context.modules.storage.put(
quote("http://test.com/smart/alabama1_ap620é"),
f.read()
)
crypto = CryptoURL('ACME-SEC')
image_url = self.get_url(
crypto.generate(
image_url=quote(self.get_url(
crypto.generate(
image_url=quote("http://test.com/smart/alabama1_ap620é")
)
))
)
)
url = crypto.generate(
image_url=quote(image_url)
)
response = self.fetch(url)
expect(response.code).to_equal(200)
示例2: test_thumbor_filter
# 需要导入模块: from libthumbor import CryptoURL [as 别名]
# 或者: from libthumbor.CryptoURL import generate [as 别名]
def test_thumbor_filter(self):
testing.setUp(settings={
'thumbor.security_key': 'foo',
})
crypto = CryptoURL(key='foo')
self.assertEqual(
thumbor_filter({}, 'image', 25, 25),
crypto.generate(width=25, height=25, image_url='image'))
self.assertEqual(
thumbor_filter({}, 'image', 25),
crypto.generate(width=25, image_url='image'))
self.assertEqual(
thumbor_filter({}, 'image', 25, None),
crypto.generate(width=25, height=0, image_url='image'))
示例3: test_usage
# 需要导入模块: from libthumbor import CryptoURL [as 别名]
# 或者: from libthumbor.CryptoURL import generate [as 别名]
def test_usage():
key = "my-security-key"
image = "s.glbimg.com/et/bb/f/original/2011/03/24/VN0JiwzmOw0b0lg.jpg"
thumbor_crypto = Crypto(salt=key)
thumbor_options = thumbor_crypto.encrypt(
width=300,
height=200,
smart=True,
fit_in=False,
flip_horizontal=False,
flip_vertical=False,
halign='center',
valign='middle',
crop_left=0,
crop_top=0,
crop_right=0,
crop_bottom=0,
image=image
)
thumbor_url = "/%s/%s" % (thumbor_options, image)
crypto = CryptoURL(key=key)
url = crypto.generate(
width=300,
height=200,
smart=True,
image_url=image
)
assert url == thumbor_url
示例4: test_usage_new_format
# 需要导入模块: from libthumbor import CryptoURL [as 别名]
# 或者: from libthumbor.CryptoURL import generate [as 别名]
def test_usage_new_format():
key = "my-security-key"
image = "s.glbimg.com/et/bb/f/original/2011/03/24/VN0JiwzmOw0b0lg.jpg"
thumbor_signer = Signer(key)
thumbor_url = Url.generate_options(
width=300,
height=200,
smart=True,
adaptive=False,
fit_in=False,
horizontal_flip=False,
vertical_flip=False,
halign='center',
valign='middle',
crop_left=0,
crop_top=0,
crop_right=0,
crop_bottom=0,
filters=[]
)
thumbor_url = ('%s/%s' % (thumbor_url, image)).lstrip('/')
thumbor_url = '/%s/%s' % (thumbor_signer.signature(thumbor_url), thumbor_url)
crypto = CryptoURL(key=key)
url = crypto.generate(
width=300,
height=200,
smart=True,
image_url=image
)
assert url == thumbor_url
示例5: test_thumbor_can_decrypt_lib_thumbor_generated_url
# 需要导入模块: from libthumbor import CryptoURL [as 别名]
# 或者: from libthumbor.CryptoURL import generate [as 别名]
def test_thumbor_can_decrypt_lib_thumbor_generated_url():
key = "my-security-key"
image = "s.glbimg.com/et/bb/f/original/2011/03/24/VN0JiwzmOw0b0lg.jpg"
thumbor_crypto = Cryptor(key)
crypto = CryptoURL(key=key)
url = crypto.generate(
width=300,
height=200,
smart=True,
image_url=image,
old=True
)
reg = "/([^/]+)/(.+)"
options = re.match(reg, url).groups()[0]
decrypted_url = thumbor_crypto.decrypt(options)
assert decrypted_url
assert decrypted_url['height'] == 200
assert decrypted_url['width'] == 300
assert decrypted_url['smart']
assert decrypted_url['image_hash'] == hashlib.md5(image).hexdigest()
示例6: main
# 需要导入模块: from libthumbor import CryptoURL [as 别名]
# 或者: from libthumbor.CryptoURL import generate [as 别名]
def main(arguments=None):
"""Converts a given url with the specified arguments."""
parsed_options, arguments = get_options(arguments)
image_url = arguments[0]
image_url = quote(image_url)
try:
config = Config.load(None)
except:
config = None
if not parsed_options.key and not config:
sys.stdout.write("Error: The -k or --key argument is mandatory. For more information type thumbor-url -h\n")
return
security_key, thumbor_params = get_thumbor_params(image_url, parsed_options, config)
crypto = CryptoURL(key=security_key)
url = crypto.generate(**thumbor_params)
sys.stdout.write("URL:\n")
sys.stdout.write("%s\n" % url)
return url
示例7: thumb
# 需要导入模块: from libthumbor import CryptoURL [as 别名]
# 或者: from libthumbor.CryptoURL import generate [as 别名]
def thumb(self, image, **kwargs):
url = image.url
if not url:
return None
if settings.THUMBOR_BASE_URL:
# If THUMBOR_BASE_URL is explicity set, use that
base = settings.THUMBOR_BASE_URL
else:
# otherwise assume that thumbor is setup behind the same
# CDN behind the `thumbor` namespace.
scheme, netloc = urlparse.urlsplit(url)[:2]
base = '{}://{}/thumbor'.format(scheme, netloc)
crypto = CryptoURL(key=settings.THUMBOR_KEY)
# just for code clarity
thumbor_kwargs = kwargs
if not 'fit_in' in thumbor_kwargs:
thumbor_kwargs['fit_in'] = False
thumbor_kwargs['image_url'] = url
path = crypto.generate(**thumbor_kwargs)
return u'{}{}'.format(base, path)
示例8: generate_thumbnail_url
# 需要导入模块: from libthumbor import CryptoURL [as 别名]
# 或者: from libthumbor.CryptoURL import generate [as 别名]
def generate_thumbnail_url(path: str, size: str='0x0') -> str:
if not (path.startswith('https://') or path.startswith('http://')):
path = '/' + path
if not is_thumbor_enabled():
if path.startswith('http://'):
return get_camo_url(path)
return path
if not user_uploads_or_external(path):
return path
source_type = get_source_type(path)
safe_url = base64.urlsafe_b64encode(path.encode()).decode('utf-8')
image_url = '%s/source_type/%s' % (safe_url, source_type)
width, height = map(int, size.split('x'))
crypto = CryptoURL(key=settings.THUMBOR_KEY)
encrypted_url = crypto.generate(
width=width,
height=height,
smart=True,
filters=['no_upscale()', 'sharpen(0.5,0.2,true)'],
image_url=image_url
)
if settings.THUMBOR_URL == 'http://127.0.0.1:9995':
# If THUMBOR_URL is the default then thumbor is hosted on same machine
# as the Zulip server and we should serve a relative URL.
# We add a /thumbor in front of the relative url because we make
# use of a proxy pass to redirect request internally in Nginx to 9995
# port where thumbor is running.
thumbnail_url = '/thumbor' + encrypted_url
else:
thumbnail_url = urllib.parse.urljoin(settings.THUMBOR_URL, encrypted_url)
return thumbnail_url
示例9: test_usage
# 需要导入模块: from libthumbor import CryptoURL [as 别名]
# 或者: from libthumbor.CryptoURL import generate [as 别名]
def test_usage():
key = "my-security-key"
image = "s.glbimg.com/et/bb/f/original/2011/03/24/VN0JiwzmOw0b0lg.jpg"
thumbor_crypto = Crypto(salt=key)
thumbor_options = thumbor_crypto.encrypt(
300,
200,
True,
False,
False,
'center',
'middle',
0, 0, 0, 0,
image=image
)
thumbor_url = "/%s/%s" % (thumbor_options, image)
crypto = CryptoURL(key=key)
url = crypto.generate(
width=300,
height=200,
smart=True,
image_url=image
)
assert url == thumbor_url
示例10: ThumborService
# 需要导入模块: from libthumbor import CryptoURL [as 别名]
# 或者: from libthumbor.CryptoURL import generate [as 别名]
class ThumborService(object):
def __init__(self, baseurl='http://localhost:8888/', secretkey='MY_SECURE_KEY'):
self.baseurl = baseurl.rstrip('/')
self._urlgen = CryptoURL(key=secretkey)
def generate_url(self, options):
return self.baseurl + self._urlgen.generate(**options)
示例11: thumb
# 需要导入模块: from libthumbor import CryptoURL [as 别名]
# 或者: from libthumbor.CryptoURL import generate [as 别名]
def thumb(img, **kwargs):
'''
returns a thumbor url for 'img' with **kwargs as thumbor options.
Positional arguments:
img -- can be a string representing the image path or any object with a url property.
Keyword arguments:
For the complete list of thumbor options
https://github.com/globocom/thumbor/wiki/Usage
and the actual implementation for the url generation
https://github.com/heynemann/libthumbor/blob/master/libthumbor/url.py
'''
# url is DNS sharded url
try:
url = img.url
except ValueError:
# When trying to access the url of an image object when
# the image does not exist, a ValueError is raised
logger.exception("No file for object (%s) in thumbor", img.instance)
return ''
except AttributeError:
# If it's an attribute error, assume it's a string already
url = img
if settings.THUMBOR_BASE_URL:
# If THUMBOR_BASE_URL is explicity set, use that
base = settings.THUMBOR_BASE_URL
else:
# otherwise assume that thumbor is setup behind the same
# CDN behind the `thumbor` namespace.
scheme, netloc = urlparse.urlsplit(url)[:2]
base = '{}://{}/thumbor'.format(scheme, netloc)
url = remove_url_scheme(url)
# just for code clarity
thumbor_kwargs = kwargs
if 'fit_in' not in thumbor_kwargs:
thumbor_kwargs['fit_in'] = True
# normalizes url to make it work localy
if settings.LOCAL:
thumbor_kwargs['unsafe'] = True
# strips 'media/' from the start of the url
if url.startswith('media/'):
url = url[len('media/'):]
url = '{}/{}'.format(settings.AWS_S3_CUSTOM_DOMAIN, url)
thumbor_kwargs['image_url'] = url
crypto = CryptoURL(key=settings.THUMBOR_KEY)
path = crypto.generate(**thumbor_kwargs).lstrip('/')
return '{}/{}'.format(base, path)
示例12: fit_in_urls
# 需要导入模块: from libthumbor import CryptoURL [as 别名]
# 或者: from libthumbor.CryptoURL import generate [as 别名]
def fit_in_urls(self, original_url, sizes):
crypto = CryptoURL(key=self._config.THUMBOR_SECURITY_KEY)
unschemed_original_url = original_url.replace('http://', '')
urls = {}
for size in sizes:
split_size = size.split('x')
path = crypto.generate(image_url=unschemed_original_url, width=split_size[0], height=split_size[1], fit_in=True)
urls[size] = self._config.THUMBOR_SERVER_URL.rstrip('/') + path
return urls
示例13: own_thumbnail
# 需要导入模块: from libthumbor import CryptoURL [as 别名]
# 或者: from libthumbor.CryptoURL import generate [as 别名]
def own_thumbnail(m, s):
global thumbor
proto = 'http%s' % s
thumbor_base = config.thumbor % { 'proto': proto }
if thumbor is None:
thumbor = CryptoURL(key=config.thumbor_key)
return thumbor_base+thumbor.generate(image_url=m(0), **config.thumbor_pars)
示例14: test_image_already_generated_by_thumbor
# 需要导入模块: from libthumbor import CryptoURL [as 别名]
# 或者: from libthumbor.CryptoURL import generate [as 别名]
def test_image_already_generated_by_thumbor(self):
with open('./tests/fixtures/images/image.jpg', 'r') as f:
self.context.modules.storage.put(
quote("http://test.com/smart/image.jpg"),
f.read()
)
crypto = CryptoURL('ACME-SEC')
image_url = self.get_url(
crypto.generate(
image_url=quote("http://test.com/smart/image.jpg")
)
)
url = crypto.generate(
image_url=quote(image_url)
)
response = self.fetch(url)
expect(response.code).to_equal(200)
示例15: get_image_url
# 需要导入模块: from libthumbor import CryptoURL [as 别名]
# 或者: from libthumbor.CryptoURL import generate [as 别名]
def get_image_url(self, image_host, image_uuid, width=None, height=None):
security_key = self.settings.get('thumbor.security_key')
if not (security_key and image_host and image_uuid):
return ''
crypto = CryptoURL(key=security_key)
if not (width or height):
image_url = crypto.generate(image_url=image_uuid)
elif width and height:
image_url = crypto.generate(
width=width, height=height, image_url=image_uuid)
elif width:
image_url = crypto.generate(
width=width, height=0, image_url=image_uuid)
else:
image_url = crypto.generate(
width=0, height=height, image_url=image_uuid)
return urljoin(image_host, image_url)