Ejemplo n.º 1
0
def upload_large(file, **options):
    """ Upload large files. """
    upload_id = utils.random_public_id()
    with open(file, 'rb') as file_io:
        upload = None
        current_loc = 0
        chunk_size = options.get("chunk_size", 20000000)
        file_size = getsize(file)
        chunk = file_io.read(chunk_size)
        while (chunk):
            chunk_io = BytesIO(chunk)
            chunk_io.name = basename(file)
            range = "bytes {0}-{1}/{2}".format(current_loc,
                                               current_loc + len(chunk) - 1,
                                               file_size)
            current_loc += len(chunk)
            upload = upload_large_part(chunk_io,
                                       http_headers={
                                           "Content-Range": range,
                                           "X-Unique-Upload-Id": upload_id
                                       },
                                       **options)
            options["public_id"] = upload.get("public_id")
            chunk = file_io.read(chunk_size)
        return upload
Ejemplo n.º 2
0
def upload_large(file, **options):
    """ Upload large files. """
    upload_id = utils.random_public_id()
    with open(file, 'rb') as file_io:
        upload = None
        current_loc = 0
        chunk_size = options.get("chunk_size", 20000000)
        file_size = getsize(file)
        chunk = file_io.read(chunk_size)        
        while (chunk):
            chunk_io = BytesIO(chunk)
            chunk_io.name = basename(file)
            range = "bytes {0}-{1}/{2}".format(current_loc, current_loc + len(chunk) - 1, file_size)
            current_loc += len(chunk)
            upload = upload_large_part(chunk_io, http_headers={"Content-Range": range, "X-Unique-Upload-Id": upload_id}, **options)
            options["public_id"] = upload.get("public_id")
            chunk = file_io.read(chunk_size)
        return upload
Ejemplo n.º 3
0
def upload_large(file, **options):
    """ Upload large files. """
    if utils.is_remote_url(file):
        return upload(file, **options)

    if hasattr(file, 'read') and callable(file.read):
        file_io = file
    else:
        file_io = open(file, 'rb')

    upload_result = None

    with file_io:
        upload_id = utils.random_public_id()
        current_loc = 0
        chunk_size = options.get("chunk_size", UPLOAD_LARGE_CHUNK_SIZE)
        file_size = utils.file_io_size(file_io)

        file_name = options.get(
            "filename", file_io.name if hasattr(file_io, 'name')
            and isinstance(file_io.name, str) else "stream")

        chunk = file_io.read(chunk_size)

        while chunk:
            content_range = "bytes {0}-{1}/{2}".format(
                current_loc, current_loc + len(chunk) - 1, file_size)
            current_loc += len(chunk)
            http_headers = {
                "Content-Range": content_range,
                "X-Unique-Upload-Id": upload_id
            }

            upload_result = upload_large_part((file_name, chunk),
                                              http_headers=http_headers,
                                              **options)

            options["public_id"] = upload_result.get("public_id")

            chunk = file_io.read(chunk_size)

    return upload_result
Ejemplo n.º 4
0
def upload_large(file, **options):
    """ Upload large files. """
    if utils.is_remote_url(file):
        return upload(file, **options)

    if hasattr(file, 'read') and callable(file.read):
        file_io = file
    else:
        file_io = open(file, 'rb')

    upload_result = None

    with file_io:
        upload_id = utils.random_public_id()
        current_loc = 0
        chunk_size = options.get("chunk_size", UPLOAD_LARGE_CHUNK_SIZE)
        file_size = utils.file_io_size(file_io)

        file_name = options.get(
            "filename",
            file_io.name if hasattr(file_io, 'name') and isinstance(file_io.name, str) else "stream")

        chunk = file_io.read(chunk_size)

        while chunk:
            content_range = "bytes {0}-{1}/{2}".format(current_loc, current_loc + len(chunk) - 1, file_size)
            current_loc += len(chunk)
            http_headers = {"Content-Range": content_range, "X-Unique-Upload-Id": upload_id}

            upload_result = upload_large_part((file_name, chunk), http_headers=http_headers, **options)

            options["public_id"] = upload_result.get("public_id")

            chunk = file_io.read(chunk_size)

    return upload_result