def download()

in source/api/chalicelib/efs_lambda.py [0:0]


def download(event):
    # first call {"path": "./", "filename": "test.txt"}
    # successive calls
    # {"path": "./", "filename": "test_video.mp4", "chunk_data": {'dzchunkindex': chunk['dzchunkindex'],
    # 'dzchunkbyteoffset': chunk['dzchunkbyteoffset']}}
    path = event['path']
    filename = event['filename']
    file_path = os.path.join(path, filename)
    chunk_size = 2000000  # bytes
    file_size = os.path.getsize(file_path)
    chunks = math.ceil(file_size / chunk_size)

    if "chunk_data" in event:
        start_index = event['chunk_data']['dzchunkbyteoffset']
        current_chunk = event['chunk_data']['dzchunkindex']
        try:
            with open(file_path, 'rb') as f:
                f.seek(start_index)
                file_content = f.read(chunk_size)
                encoded_chunk_content = str(base64.b64encode(file_content), 'utf-8')
                chunk_offset = start_index + chunk_size
                chunk_number = current_chunk + 1

                return {"dzchunkindex": chunk_number, "dztotalchunkcount": chunks, "dzchunkbyteoffset": chunk_offset,
                        "chunk_data": encoded_chunk_content, "dztotalfilesize": file_size}
        except OSError as error:
            print('Could not read file: {error}'.format(error=error))
            return {"message": "couldn't read the file from disk", "statusCode": 500}

    else:
        start_index = 0
        try:
            with open(file_path, 'rb') as f:
                f.seek(start_index)
                file_content = f.read(chunk_size)
                encoded_chunk_content = str(base64.b64encode(file_content), 'utf-8')
                chunk_number = 0
                chunk_offset = chunk_size

                return {"dzchunkindex": chunk_number, "dztotalchunkcount": chunks, "dzchunkbyteoffset": chunk_offset,
                        "chunk_data": encoded_chunk_content, "dztotalfilesize": file_size}

        except OSError as error:
            print('Could not read file: {error}'.format(error=error))
            return {"message": "couldn't read the file from disk", "statusCode": 500}