Пример #1
0
    async def _append(self, dm, data, offset):
        if dm.size is not None:
            size = dm.size
        else:
            # assuming size will come eventually
            size = "*"
        headers = {
            "Content-Length": str(len(data)),
            "Content-Type": to_str(dm.content_type),
        }
        if len(data) != size:
            content_range = "bytes {init}-{chunk}/{total}".format(
                init=offset, chunk=offset + len(data) - 1, total=size)
            headers["Content-Range"] = content_range

        util = get_utility(IGCloudBlobStore)
        async with util.session.put(dm.get("resumable_uri"),
                                    headers=headers,
                                    data=data) as call:
            text = await call.text()  # noqa
            if call.status not in [200, 201, 308]:
                if call.status == 410:
                    raise HTTPGone(
                        content={
                            "reason": "googleError",
                            "message":
                            "Resumable upload is no longer available",
                            "info": text,
                        })
                raise GoogleCloudException(f"{call.status}: {text}")
            return call
Пример #2
0
def json_converter(value):
    if value is None:
        return value

    request = get_current_request()
    registry = task_vars.registry.get()
    settings = registry.for_interface(IImagingSettings)
    scales = {}
    url = get_url(request, request.path)
    # TODO: VIRUALHOSTMONSTER
    for size, dimension in settings["allowed_sizes"].items():
        width, _, height = dimension.partition(":")
        scales[size] = {
            "download": url + "/@@images/image/" + size,
            "height": height,
            "width": width,
        }

    return {
        "filename": value.filename,
        "content_type": to_str(value.content_type),
        "size": value.size,
        "extension": value.extension,
        "md5": value.md5,
        "download": f"{url}/@download/image",
        "scales": scales,
    }
Пример #3
0
def json_converter(value):
    if value is None:
        return value

    request = get_current_request()
    registry = task_vars.registry.get()
    settings = registry.for_interface(IImagingSettings)
    scales = {}
    url = request.url.split('?')[0]
    for size, dimension in settings['allowed_sizes'].items():
        width, _, height = dimension.partition(':')
        scales[size] = {
            "download": url + '/@@images/image/' + size,
            "height": height,
            "width": width
        }

    return {
        'filename': value.filename,
        'content_type': to_str(value.content_type),
        'size': value.size,
        'extension': value.extension,
        'md5': value.md5,
        'scales': scales
    }
Пример #4
0
 def guess_content_type(self):
     ct = to_str(self.content_type)
     if ct == 'application/octet-stream':
         # try guessing content_type
         ct, _ = mimetypes.guess_type(self.filename)
         if ct is None:
             ct = 'application/octet-stream'
     return ct
Пример #5
0
def guess_content_type(content_type, filename):
    ct = to_str(content_type)
    if not ct or ct == 'application/octet-stream':
        if not filename:
            return 'application/octet-stream'
        # try guessing content_type
        ct, _ = mimetypes.guess_type(filename)
        if ct is None:
            ct = 'application/octet-stream'
    return ct
Пример #6
0
def guess_content_type(content_type, filename):
    ct = to_str(content_type)
    if not ct or ct == 'application/octet-stream':
        if not filename:
            return 'application/octet-stream'
        # try guessing content_type
        ct, _ = mimetypes.guess_type(filename)
        if ct is None:
            ct = 'application/octet-stream'
    return ct
Пример #7
0
def json_converter(value):
    if value is None:
        return value

    return {
        'filename': value.filename,
        'content_type': to_str(value.content_type),
        'size': value.size,
        'extension': value.extension,
        'md5': value.md5
    }
Пример #8
0
def json_converter(value):
    if value is None:
        return value

    return {
        'filename': value.filename,
        'content_type': to_str(value.content_type),
        'size': value.size,
        'extension': value.extension,
        'md5': value.md5
    }
Пример #9
0
def json_converter(value):
    if value is None:
        return value

    return {
        "filename": value.filename,
        "content_type": to_str(value.content_type),
        "size": value.size,
        "extension": value.extension,
        "md5": value.md5,
    }
Пример #10
0
    async def start(self, dm):
        """Init an upload.

        _uload_file_id : temporal url to image beeing uploaded
        _resumable_uri : uri to resumable upload
        _uri : finished uploaded image
        """
        util = get_utility(IGCloudBlobStore)
        request = get_current_request()
        upload_file_id = dm.get("upload_file_id")
        if upload_file_id is not None:
            await self.delete_upload(upload_file_id)

        generator = get_multi_adapter((self.context, self.field),
                                      IFileNameGenerator)
        upload_file_id = await apply_coroutine(generator)

        init_url = "{}&name={}".format(
            UPLOAD_URL.format(bucket=await util.get_bucket_name()),
            quote_plus(upload_file_id),
        )

        creator = get_authenticated_user_id()
        metadata = json.dumps({
            "CREATOR": creator,
            "REQUEST": str(request),
            "NAME": dm.get("filename")
        })
        call_size = len(metadata)
        async with util.session.post(
                init_url,
                headers={
                    "AUTHORIZATION":
                    "Bearer {}".format(await util.get_access_token()),
                    "X-Upload-Content-Type": to_str(dm.content_type),
                    "X-Upload-Content-Length": str(dm.size),
                    "Content-Type": "application/json; charset=UTF-8",
                    "Content-Length": str(call_size),
                },
                data=metadata,
        ) as call:
            if call.status != 200:
                text = await call.text()
                raise GoogleCloudException(f"{call.status}: {text}")
            resumable_uri = call.headers["Location"]

        await dm.update(current_upload=0,
                        resumable_uri=resumable_uri,
                        upload_file_id=upload_file_id)