Jump to content

Downloading files with Slim and cURL


NotionCommotion

Recommended Posts

I have the following REST API #1 endpoint which will download a non-text (PCAP) file to a web browser.

 

API #1 doesn't directly have the file, and must make another HTTP request to API #2 using cURL.

 

For small files, it works fine, but for larger files, it provides a file with zero size.

 

Any ideas where the problem is?

 

Thank you

 

API #1

$app->get('/{guids}/{guid}/logs/{id:[0-9]+}', function (Request $request, Response $response, $args) {
    $config=$this->get('settings')['server'];
    $query=$request->getUri()->getQuery();
    $query=$query?"?$query":null;
    $url=$config['ip']._VER_."/guids/$args[guid]/logs/$args[id]$query";
    $context = stream_context_create(['http'=>['header'=>'X-Access-Key: '.$config['key']]]);
    $fh = fopen($url, 'rb', false, $context);  //r or rb?
    $stream = new \Slim\Http\Stream($fh);
    $headers = $stream->getMetadata()['wrapper_data'];
    $forwardHeader=[
        'Content-Description'=>'File Transfer',
        'Content-Type'=>'application/octet-stream',
        'Content-Transfer-Encoding'=>'binary',
        'Content-Disposition'=>'attachment; filename="replaced_name"',
        'Expires'=>0,
        'Cache-Control'=>'must-revalidate, post-check=0, pre-check=0',
        'Pragma'=>'public',
        'Content-Length'=>false,   //Possible to get stream length?
    ];
    foreach ($headers as $header) {
        $header=explode(':',$header);
        if($header && count($header)==2 && isset($forwardHeader[$header[0]])){
            //Should I really be modifying the $response?
            $response=$response->withHeader($header[0],trim($header[1]));
            unset($forwardHeader[$header[0]]);
        }
    }
    //Is this necessary?
    foreach ($forwardHeader as $key=>$value) {
        if($value!==false){
            $response=$response->withHeader($key,$value);
        }
    }
    return $response->withBody($stream);
});

API #2

$app->get(_VER_.'/guids/{guid}/logs/{id:[0-9]+}', function (Request $request, Response $response, $args) {
    $doc=$this->get('Guids')->getUploadInfo($args['guid'],$args['id']);   //Returns object with path, name, size, and will throw an exception if path doesn't exist
    $fh = fopen($doc->path, 'rb'); //r for readonly, and b for binary?
    $stream = new \Slim\Http\Stream($fh);
    return $response->withBody($stream)
    //->setOutputBuffering(false)
    ->withHeader('Content-Description', 'File Transfer')
    ->withHeader('Content-Transfer-Encoding', 'binary')
    ->withHeader('Content-Type', 'application/octet-stream')
    //->withHeader('Content-Type', 'application/force-download')    //Don't use?
    //->withHeader('Content-Type', 'application/download')          //Don't use?
    ->withHeader('Content-Disposition', "attachment; filename='$doc->name'")
    ->withHeader('Expires', '0')
    ->withHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
    ->withHeader('Pragma', 'public')
    ->withHeader('Content-Length', $doc->size);
});
Link to comment
Share on other sites

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.