Ejemplo n.º 1
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $identifier = $input->getArgument('identifier');
     if (!$identifier) {
         $identifier = '1799-Auobj';
     }
     $path = utils::normalize(__DIR__ . "/../data/{$identifier}.json");
     $content = file_get_contents($path);
     $data = json_decode($content);
     $localFile = utils::normalize(__DIR__ . "/../tmp/{$data->name}");
     $file = fopen($localFile, 'wb');
     $http = new \ADN\Extract\HttpRequest($data->uri, [], null, null);
     $response = $http->get(function ($code, $chunk) use($file, $data, $path) {
         if ($code != 'data') {
             return false;
         }
         fwrite($file, $chunk);
         $data->bytesRead += strlen($chunk);
         file_put_contents($path, json_encode($data));
         return true;
     });
     fclose($file);
     if (!$response || $response->code != Response::HTTP_OK) {
         $output->writeln('oops');
         $fs = new Filesystem();
         $fs->remove($path);
         $fs->remove($localFile);
         return;
     }
     $data->size = utils::findKey($response->headers, 'Content-Length');
     $data->bytesRead = $data->size;
     file_put_contents($path, json_encode($data));
     utils::log('ok');
 }
 public function uri(Silex\Application $app, Request $request)
 {
     $bodyParams = json_decode($request->getContent());
     $uri = $bodyParams->uri;
     $identifier = $bodyParams->identifier;
     $original_filename = $bodyParams->name ?: str_replace('/.*\\//', '', str_replace('/[\\?#].*$/', '', urldecode($uri)));
     Unirest\Request::verifyPeer(false);
     //$response =Unirest\Request::head ($uri, [], null) ;
     $http = new \ADN\Extract\HttpRequest($uri, [], null, null);
     $response = $http->head();
     if (!$request || $response->code != Response::HTTP_OK) {
         return new Response('', $response->code, ['Content-Type' => 'text/plain']);
     }
     $length = utils::findKey($response->headers, 'Content-Length') ?: -1;
     $data = (object) array('key' => $identifier, 'name' => $original_filename, 'uri' => $uri, 'size' => $length, 'bytesRead' => 0, 'bytesPosted' => 0);
     $path = utils::normalize(__DIR__ . "/../data/{$identifier}.json");
     if (file_put_contents($path, json_encode($data)) === false) {
         return new Response('', Response::HTTP_INTERNAL_SERVER_ERROR, ['Content-Type' => 'text/plain']);
     }
     $result = utils::executeScript("/dl.php lmv:dl {$identifier}");
     if ($result === false) {
         return new Response('', Response::HTTP_INTERNAL_SERVER_ERROR, ['Content-Type' => 'text/plain']);
     }
     $data = (object) array('status' => $identifier);
     return new JsonResponse($data, Response::HTTP_OK);
 }