Beispiel #1
0
def qiniu_preview(url, *args, **kwargs):
    """
    we use weui as default size.
    :param element:
    :param args:
    :return:
    """

    width = kwargs.get('width', 75)
    height = kwargs.get('height', 75)
    scale = kwargs.get('scale', True)
    domain = kwargs.get('domain', True)

    if domain:
        url = '{}://{}/{}'.format('http' if get_qiniu_config(
            'QINIU_SECURE_URL') is not True else 'https',
                                  get_qiniu_config('QINIU_BUCKET_DOMAIN'), url)

    # use original image
    if width == 'auto' and height == 'auto':
        return qiniu_private(url)

    if scale:
        width_str = '/w/{}'.format(width) if width != 'auto' else ''
        height_str = '/h/{}'.format(height) if height != 'auto' else ''
        return qiniu_private('{}?imageView2/{}{}{}'.format(url, '2', width_str,
                                                           height_str))  # mode=2,limit width and height
    else:
        width_str = width if width != 'auto' else ''
        height_str = height if height != 'auto' else ''
        return qiniu_private(
            '{}?imageMogr2/thumbnail/{}x{}!'.format(url, width_str,
                                                    height_str))

    pass
    def teardown_class(cls):
        """Delete all files in the test bucket.
        """
        storage = QiniuPrivateStorage(
            bucket_name=get_qiniu_config('QINIU_PRIVATE_BUCKET_NAME'),
            bucket_domain=get_qiniu_config('QINIU_PRIVATE_BUCKET_DOMAIN'),
        )
        auth = storage.auth
        bucket = BucketManager(auth)

        while True:
            ret, eof, info = bucket.list(storage.bucket_name, limit=100)

            if ret is None:
                print(info)
                break

            for item in ret['items']:
                name = item['key']
                if six.PY2:
                    name = name.encode('utf-8')
                ret, info = bucket.delete(storage.bucket_name, name)
                if ret is None:
                    print(info)
            if eof:
                break
Beispiel #3
0
    def teardown_class(cls):
        """Delete all files in the test bucket.
        """
        storage = QiniuPrivateStorage(
            bucket_name=get_qiniu_config('QINIU_PRIVATE_BUCKET_NAME'),
            bucket_domain=get_qiniu_config('QINIU_PRIVATE_BUCKET_DOMAIN'),
        )
        auth = storage.auth
        bucket = BucketManager(auth)

        while True:
            ret, eof, info = bucket.list(storage.bucket_name, limit=100)

            if ret is None:
                print(info)
                break

            for item in ret['items']:
                name = item['key']
                if six.PY2:
                    name = name.encode('utf-8')
                ret, info = bucket.delete(storage.bucket_name, name)
                if ret is None:
                    print(info)
            if eof:
                break
Beispiel #4
0
def qiniu_preview(url, *args, **kwargs):
    """
    we use weui as default size.
    :param element:
    :param args:
    :return:
    """

    width = kwargs.get('width', 75)
    height = kwargs.get('height', 75)
    scale = kwargs.get('scale', True)
    domain = kwargs.get('domain', True)

    if domain:
        url = '{}://{}/{}'.format(
            'http'
            if get_qiniu_config('QINIU_SECURE_URL') is not True else 'https',
            get_qiniu_config('QINIU_BUCKET_DOMAIN'), url)

    # use original image
    if width == 'auto' and height == 'auto':
        return qiniu_private(url)

    if scale:
        width_str = '/w/{}'.format(width) if width != 'auto' else ''
        height_str = '/h/{}'.format(height) if height != 'auto' else ''
        return qiniu_private('{}?imageView2/{}{}{}'.format(
            url, '2', width_str, height_str))  # mode=2,limit width and height
    else:
        width_str = width if width != 'auto' else ''
        height_str = height if height != 'auto' else ''
        return qiniu_private('{}?imageMogr2/thumbnail/{}x{}!'.format(
            url, width_str, height_str))

    pass
Beispiel #5
0
def qiniu_private(base_url):
    """
    get private resource
    """
    cache_key = 'st:qiniu:' + base_url

    cache_value = cache.get(cache_key)
    if cache_value:
        return cache_value

    q = Auth(get_qiniu_config('QINIU_ACCESS_KEY'),
             get_qiniu_config('QINIU_SECRET_KEY'))
    expire = 3600 if not hasattr(settings,
                                 'QINIU_PREVIEW_EXPIRE') else settings.QINIU_PREVIEW_EXPIRE
    private_url = q.private_download_url(base_url, expires=expire)

    cache.set(cache_key, private_url, timeout=max(10, expire - 10))

    return private_url
Beispiel #6
0
def qiniu_private(base_url):
    """
    get private resource
    """
    cache_key = 'st:qiniu:' + base_url

    cache_value = cache.get(cache_key)
    if cache_value:
        return cache_value

    q = Auth(get_qiniu_config('QINIU_ACCESS_KEY'),
             get_qiniu_config('QINIU_SECRET_KEY'))
    expire = 3600 if not hasattr(
        settings, 'QINIU_PREVIEW_EXPIRE') else settings.QINIU_PREVIEW_EXPIRE
    private_url = q.private_download_url(base_url, expires=expire)

    cache.set(cache_key, private_url, timeout=max(10, expire - 10))

    return private_url
Beispiel #7
0
def qiniu_image_info(url, *args, **kwargs):
    """
    get image info

    ..todo::
        maybe we should add cache support

    :param element:
    :param args:
    :return:
    """

    domain = kwargs.get('domain', True)

    if domain:
        url = '{}://{}/{}'.format('http' if get_qiniu_config(
            'QINIU_SECURE_URL') is not True else 'https',
                                  get_qiniu_config('QINIU_BUCKET_DOMAIN'), url)

    url = qiniu_private('{}?imageInfo'.format(url))
    rtn = requests.get(url)
    return rtn.json()
Beispiel #8
0
def qiniu_image_info(url, *args, **kwargs):
    """
    get image info

    ..todo::
        maybe we should add cache support

    :param element:
    :param args:
    :return:
    """

    domain = kwargs.get('domain', True)

    if domain:
        url = '{}://{}/{}'.format(
            'http'
            if get_qiniu_config('QINIU_SECURE_URL') is not True else 'https',
            get_qiniu_config('QINIU_BUCKET_DOMAIN'), url)

    url = qiniu_private('{}?imageInfo'.format(url))
    rtn = requests.get(url)
    return rtn.json()
 def setUp(self):
     self.storage = QiniuPrivateStorage(
         bucket_name=get_qiniu_config('QINIU_PRIVATE_BUCKET_NAME'),
         bucket_domain=get_qiniu_config('QINIU_PRIVATE_BUCKET_DOMAIN'),
     )
Beispiel #10
0
class QiniuPRStorageTest(StorageTestMixin, unittest.TestCase):
    storage = QiniuPrivateStorage(
        bucket_name=get_qiniu_config('QINIU_PRIVATE_BUCKET_NAME'),
        bucket_domain=get_qiniu_config('QINIU_PRIVATE_BUCKET_DOMAIN'),
    )
Beispiel #11
0
 def setUp(self):
     self.storage = QiniuPrivateStorage(
         bucket_name=get_qiniu_config('QINIU_PRIVATE_BUCKET_NAME'),
         bucket_domain=get_qiniu_config('QINIU_PRIVATE_BUCKET_DOMAIN'),
     )