{% for parameter in method.parameters.all %}
{% if parameter.type == 'file' %}
        $size = 0;
        $mimeType = null;
        $postedName = null;
        if(empty(${{ parameter.name | caseCamel }}->getPath() ?? null)) {
            $size = strlen(${{ parameter.name | caseCamel }}->getData());
            $mimeType = ${{ parameter.name | caseCamel }}->getMimeType();
            $postedName = ${{ parameter.name | caseCamel }}->getFilename();
            if ($size <= Client::CHUNK_SIZE) {
                $apiParams['{{ parameter.name | caseCamel }}'] = new \CURLFile('data://' . $mimeType . ';base64,' . base64_encode(${{ parameter.name | caseCamel }}->getData()), $mimeType, $postedName);
                return $this->client->call(Client::METHOD_POST, $apiPath, [
    {% for param in method.parameters.header %}
                    '{{ param.name }}' => ${{ param.name | caseCamel }},
    {% endfor %}
    {% for key, header in method.headers %}
                    '{{ key }}' => '{{ header }}',
    {% endfor %}
                    ], $apiParams);
            }
        } else {
            $size = filesize(${{ parameter.name | caseCamel }}->getPath());
            $mimeType = ${{ parameter.name | caseCamel }}->getMimeType() ?? mime_content_type(${{ parameter.name | caseCamel }}->getPath());
            $postedName = ${{ parameter.name | caseCamel }}->getFilename() ?? basename(${{ parameter.name | caseCamel }}->getPath());
            //send single file if size is less than or equal to 5MB
            if ($size <= Client::CHUNK_SIZE) {
                $apiParams['{{ parameter.name }}'] = new \CURLFile(${{ parameter.name | caseCamel }}->getPath(), $mimeType, $postedName);
                return $this->client->call(Client::METHOD_{{ method.method | caseUpper }}, $apiPath, [
    {% for param in method.parameters.header %}
                    '{{ param.name }}' => ${{ param.name | caseCamel }},
    {% endfor %}
    {% for key, header in method.headers %}
                    '{{ key }}' => '{{ header }}',
    {% endfor %}
                    ], $apiParams);
            }
        }

        $id = '';
        $counter = 0;

{% for parameter in method.parameters.all %}
{% if parameter.isUploadID %}
        if(${{ parameter.name | caseCamel | escapeKeyword }} != 'unique()') {
            try {
                $response = $this->client->call(Client::METHOD_GET, $apiPath . '/' . ${{ parameter.name }});
                $counter = $response['chunksUploaded'] ?? 0;
            } catch(\Exception $e) {
            }
        }
{% endif %}
{% endfor %}

        $apiHeaders = ['content-type' => 'multipart/form-data'];
        $handle = null;

        if(!empty(${{parameter.name}}->getPath())) {
            $handle = @fopen(${{parameter.name}}->getPath(), "rb");
        }

        $start = $counter * Client::CHUNK_SIZE;
        while ($start < $size) {
            $chunk = '';
            if(!empty($handle)) {
                fseek($handle, $start);
                $chunk = @fread($handle, Client::CHUNK_SIZE);
            } else {
                $chunk = substr($file->getData(), $start, Client::CHUNK_SIZE);
            }
            $apiParams['{{ parameter.name }}'] = new \CURLFile('data://' . $mimeType . ';base64,' . base64_encode($chunk), $mimeType, $postedName);
            $apiHeaders['content-range'] = 'bytes ' . ($counter * Client::CHUNK_SIZE) . '-' . min(((($counter * Client::CHUNK_SIZE) + Client::CHUNK_SIZE) - 1), $size - 1) . '/' . $size;
            if(!empty($id)) {
                $apiHeaders['x-{{spec.title | caseLower }}-id'] = $id;
            }
            $response = $this->client->call(Client::METHOD_POST, $apiPath, $apiHeaders, $apiParams);
            $counter++;
            $start += Client::CHUNK_SIZE;
            if(empty($id)) {
                $id = $response['$id'];
            }
            if($onProgress !== null) {
                $onProgress([
                    '$id' => $response['$id'],
                    'progress' => min(((($counter * Client::CHUNK_SIZE) + Client::CHUNK_SIZE)), $size) / $size * 100,
                    'sizeUploaded' => min($counter * Client::CHUNK_SIZE),
                    'chunksTotal' => $response['chunksTotal'],
                    'chunksUploaded' => $response['chunksUploaded'], 
                ]);
            }
        }
        if(!empty($handle)) {
            @fclose($handle);
        }
        return $response;
{% endif %}
{% endfor %}