static size_t S3_HLS_Upload_Data()

in S3_HLS_S3_Put_Client.c [71:115]


static size_t S3_HLS_Upload_Data(void *ptr, size_t size, size_t nmemb, void *stream) {
    S3_HLS_UPLOAD_CTX* ctx = (S3_HLS_UPLOAD_CTX*)stream;
    PUT_DEBUG("Upload Data! %d %d %d %ld\n", size, nmemb, ctx->pos, stream);
    
    if(NULL == ctx->first_part_start && 0 != ctx->first_part_length) {
        return S3_HLS_INVALID_PARAMETER;
    }
    
    if(NULL == ctx->second_part_start && 0 != ctx->second_part_length) {
        return S3_HLS_INVALID_PARAMETER;
    }
    
    if(ctx->pos == ctx->first_part_length + ctx->second_part_length) {
        PUT_DEBUG("Upload Data Done! %d\n", ctx->pos);
        return 0;
    }
    
    if(0 == size || 0 == nmemb || (size*nmemb) < 1) {
        return 0;
    }
    
    size_t len = size * nmemb;
    size_t bytes_written = 0;
    if(ctx->pos < ctx->first_part_length) {
        size_t bytes_to_write = len <= (ctx->first_part_length - ctx->pos) ? len : (ctx->first_part_length - ctx->pos);
        memcpy(ptr, ctx->first_part_start + ctx->pos, bytes_to_write);
        ctx->pos += bytes_to_write;
        len -= bytes_to_write;
        ptr += bytes_to_write;
        
        bytes_written += bytes_to_write;
    }
    
    if(len > 0) {
        if(ctx->pos < ctx->first_part_length + ctx->second_part_length) { // write in second part
        size_t bytes_to_write = len <= (ctx->first_part_length + ctx->second_part_length - ctx->pos) ? len : (ctx->first_part_length + ctx->second_part_length - ctx->pos);
            memcpy(ptr, ctx->second_part_start + (ctx->pos - ctx->first_part_length), bytes_to_write);
            ctx->pos += bytes_to_write;
            bytes_written += bytes_to_write;
        }
    }
    
    PUT_DEBUG("Upload Bytes Written: %d\n", bytes_written);
    return bytes_written;
}