NotionCommotion Posted March 14, 2018 Share Posted March 14, 2018 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 More sharing options...
dalecosp Posted March 14, 2018 Share Posted March 14, 2018 Nothing for sure .. off the top of my head ... time-outs, file size limits? cURL should have some debugging capability ... you might try curl_getinfo() to see what's going on with the request. Link to comment Share on other sites More sharing options...
NotionCommotion Posted March 23, 2018 Author Share Posted March 23, 2018 Turned out to be the last line in API 2. I didn't notice the warning, and $doc->size was not defined. I don't know why small files were downloaded yet not large ones, but all is well. ->withHeader('Content-Length', $doc->size Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.