protected function execute(InputInterface $input, OutputInterface $output)
 {
     $identifier = $input->getArgument('identifier');
     if (!$identifier) {
         $identifier = '1799-Auobj';
     }
     $keys = $input->getOption('keys');
     foreach ($keys as $envkey) {
         putenv($envkey);
     }
     $bucket = lmv::getDefaultBucket();
     $lmv = new lmv($bucket);
     $urn = $lmv->getURN($identifier);
     //$path =utils::normalize (__DIR__ . '/../data/' . $identifier . '.lock') ;
     //file_put_contents ($path, json_encode ([ '*****@*****.**' ])) ;
     $extractor = new Extractor($identifier, $bucket);
     $bSuccess = $extractor->extract();
     utils::log($bSuccess ? 'ok' : 'oops');
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $identifier = $input->getArgument('identifier');
     if (!$identifier) {
         $identifier = '1799-Auobj';
     }
     $keys = $input->getOption('keys');
     foreach ($keys as $envkey) {
         putenv($envkey);
     }
     $bucket = lmv::getDefaultBucket();
     $translator = new Translator($identifier, $bucket);
     $bSuccess = $translator->translate();
     utils::log($bSuccess ? 'ok' : 'oops');
 }
 public function dl(Silex\Application $app, $identifier, $fragment)
 {
     $bucket = lmv::getDefaultBucket();
     $path = $app->dataDir("/{$identifier}.resultdb.json", true);
     if (!$path) {
         return new Response('', Response::HTTP_NOT_FOUND, ['Content-Type' => 'text/plain']);
     }
     $content = file_get_contents($path);
     $data = json_decode($content);
     $guid = $data->urn;
     $urn = "urn:adsk.viewing:fs.file:{$guid}/output/{$fragment}";
     $lmv = new lmv($bucket);
     $response = $lmv->downloadItem($urn);
     if (is_int($reponse)) {
         return new Response('', Response::HTTP_NOT_FOUND, ['Content-Type' => 'text/plain']);
     }
     $filename = basename($fragment);
     return new Response($response, Response::HTTP_OK, ['Cache-Control' => 'private', 'Content-Type' => 'application/octet-stream', 'Content-Length' => strlen($response), 'Content-Disposition' => "attachment; filename=\"{$filename}\""]);
     // Send headers before outputting anything?
     //$response->sendHeaders () ;
 }
 public function translate()
 {
     //utils::log ('translate launched!') ;
     try {
         $path = $this->lmv->dataDir("/{$this->identifier}.dependencies.json", true);
         if ($path === false) {
             throw new Exception('No dependency file found');
         }
         $content = file_get_contents($path);
         $connections = json_decode($content);
         if ($content === false || !is_object($connections) || is_null($connections[0])) {
             utils::log('ERROR: project dependencies corrupted :(');
             // Rebuild one master only
             $connections = (object) array('uniqueIdentifier' => $this->identifier, 'children' => []);
         }
         $items = [$this->identifier];
         $items = array_merge($items, $this->traverseConnections($connections->children));
         // This is to help the upload progress bar to be more precise
         if (file_put_contents($path, json_encode($items)) === false) {
             utils::log('ERROR: project dependencies not saved :(');
             throw new Exception('ERROR: project dependencies not saved :(');
         }
         // Bucket
         $policy = 'transient';
         $r1 = $this->lmv->createBucketIfNotExist($policy);
         if ($r1 == null) {
             // No need to continue if the bucket was not created
             utils::log('Failed to create bucket!');
             throw new Exception('Failed to create bucket!');
         }
         utils::log('Bucket created (or did exist already)!');
         // Uploading file(s)
         utils::log('Uploading(s)');
         foreach ($items as $item) {
             utils::log(" uploading {$item}");
             $r2 = $this->lmv->uploadFile($item);
             if ($r2 == null) {
                 utils::log(" Failed to upload {$item}");
                 throw new Exception("Failed to upload {$item}");
             }
             utils::log("{$item} upload completed!");
         }
         utils::log('All files uploaded');
         // Dependencies
         $r3 = $this->lmv->setDependencies(count($items) == 1 ? null : $connections);
         if ($r3 == null) {
             utils::log('Failed to set connections');
             throw new Exception('Failed to set connections');
         }
         utils::log('References set, launching translation');
         // Register
         $r4 = $this->lmv->register($connections);
         if ($r3 == null) {
             utils::log('URN registration for translation failed');
             throw new Exception('URN registration for translation failed');
         }
         utils::log('URN registered for translation');
         // We are done for now!
         // Just remember locally we did submit the project for translation
         $urn = $this->lmv->getURN($this->identifier);
         $urn = base64_encode($urn);
         $data = (object) array('guid' => $urn, 'progress' => '0% complete', 'startedAt' => gmdate(DATE_RFC2822), 'status' => 'requested', 'success' => '0%', 'urn' => $urn);
         $path = $this->lmv->dataDir("/{$this->identifier}.resultdb.json");
         if (file_put_contents($path, json_encode($data)) === false) {
             utils::log("Could not save file to disk - {$path}");
         }
         $filesystem = new Filesystem();
         foreach ($items as $item) {
             $filesystem->remove($this->lmv->dataDir("/{$item}.json"));
         }
         $filesystem->remove($this->lmv->dataDir("/{$this->identifier}.dependencies.json"));
         $filesystem->remove($this->lmv->dataDir("/{$this->identifier}.connections.json"));
         return true;
     } catch (Exception $ex) {
         //- We are done! email me if any error
         utils::log('Something wrong happened during upload');
         $loader = new Twig_Loader_Filesystem(array($this->lmv->viewsDir('', true)));
         $environment = new Twig_Environment($loader, array('debug' => false, 'charset' => 'utf-8', 'strict_variables' => true));
         $content = $environment->render('email-xlt-failed.twig', array('ID' => $this->identifier));
         $config = lmv::config();
         $mjet = new Mailjet($config['MAILJET1'], $config['MAILJET2']);
         $mjet->sendContent('ADN Sparks <*****@*****.**>', '*****@*****.**', 'Autodesk View & Data API Extractor app failed to translate a project', 'html', $content);
         $filesystem = new Filesystem();
         $filesystem->rename($this->lmv->dataDir("/{$this->identifier}.resultdb.json", true), $this->lmv->dataDir("/{$this->identifier}.resultdb.failed"), true);
         return false;
     }
 }
 public function thumbnail($urn, $width = null, $height = null)
 {
     $encodedURN = base64_encode($urn);
     $config = lmv::config();
     $endpoint = sprintf($config['getThumbnailsEndPoint'], $encodedURN);
     $query = [];
     if ($width !== null) {
         $query['width'] = $width;
     }
     if ($height !== null) {
         $query['height'] = $height;
     }
     Unirest\Request::verifyPeer(false);
     $response = Unirest\Request::get($endpoint, array('Authorization' => 'Bearer ' . lmv::bearer()), $query);
     if ($response->code != lmv::HTTP_OK) {
         utils::log('thumbnail fail ' . $response->code);
         return null;
     }
     return $response->raw_body;
 }
 public function __construct($env)
 {
     utils::log('WEB Extract Application running');
     parent::__construct($env);
     $app = $this;
     // Override these values in resources/config/prod.php file
     //$app ['var_dir'] =$this->rootDir . '/var' ;
     //$app ['locale'] ='fr' ;
     //$app ['http_cache.cache_dir'] =$app->share (function (Application $app) {
     //	return ($app ['var_dir'] . '/http') ;
     //}) ;
     //$app ['monolog.options'] =[
     //	'monolog.logfile' => $app['var_dir'].'/logs/app.log',
     //	'monolog.name' => 'app',
     //	'monolog.level' => 300, // = Logger::WARNING
     //] ;
     //$app ['security.users'] =array( 'alice' => array ( 'ROLE_USER', 'password' ) ) ;
     lmv::tokenObject();
     //$app->register (new HttpCacheServiceProvider ()) ;
     $app->register(new SessionServiceProvider());
     //$app->register (new ValidatorServiceProvider ()) ;
     //$app->register (new FormServiceProvider ()) ;
     //$app->register (new UrlGeneratorServiceProvider ()) ;
     //$app->register (new DoctrineServiceProvider ()) ;
     //$app->register (new SecurityServiceProvider (), array (
     //	'security.firewalls' => array (
     //		'admin' => array (
     //			'pattern' => '^/',
     //			'form' => array (
     //				'login_path' => '/login',
     //			),
     //			'logout' => true,
     //			'anonymous' => true,
     //			'users' => $app ['security.users'],
     //		),
     //	),
     //)) ;
     //$app ['security.encoder.digest'] =$app->share (function ($app) {
     //	return (new PlaintextPasswordEncoder ()) ;
     //}) ;
     //$app ['security.utils'] =$app->share (function ($app) {
     //	return (new AuthenticationUtils ($app ['request_stack'])) ;
     //}) ;
     //$app->register (new TranslationServiceProvider ()) ;
     //$app ['translator'] =$app->share($app->extend ('translator', function ($translator, $app) {
     //	$translator->addLoader ('yaml', new YamlFileLoader ()) ;
     //	$translator->addResource ('yaml', $this->rootDir.'/resources/translations/fr.yml', 'fr') ;
     //	return ($translator) ;
     //})) ;
     //$app->register (new MonologServiceProvider (), $app ['monolog.options']) ;
     $app->register(new TwigServiceProvider(), array('twig.path' => array($this->rootDir . '/views')));
     //$app ['twig'] = $app->share ($app->extend ('twig', function ($twig, $app) {
     //	$twig->addFunction (new \Twig_SimpleFunction ('asset', function ($asset) use ($app) {
     //		$base = $app['request_stack']->getCurrentRequest ()->getBasePath () ;
     //		return (sprintf ($base . '/' . $asset, ltrim ($asset, '/'))) ;
     //	})) ;
     //	return ($twig) ;
     //})) ;
     //$app->mount ('', new ControllerProvider ()) ;
     $app->mount('/api', new LmvFile());
     $app->mount('/api/projects', new LmvProjects());
     $app->mount('/api/results', new LmvResults());
     $app->get('/', function () use($app) {
         return $app['twig']->render('index.html.twig', []);
     });
     $app->get('/explore/{identifier}', function ($identifier) use($app) {
         $bucket = lmv::getDefaultBucket();
         $zipExist = $app->extractDir("/{$identifier}.zip", true) !== false;
         $path = $app->dataDir("/{$identifier}.resultdb.json");
         $content = file_get_contents($path);
         if ($content === false) {
             $app->abort(Response::HTTP_NOT_ACCEPTABLE, "DB record not present");
             return;
         }
         $data = json_decode($content);
         return $app['twig']->render('explore.twig', ['bucket' => $bucket, 'extracted' => $zipExist ? 'true' : 'false', 'urn' => $data->urn, 'accessToken' => lmv::bearer(), 'root' => $identifier]);
     });
 }
 public function submitProject(Silex\Application $app, Request $request)
 {
     $bucket = lmv::getDefaultBucket();
     $policy = 'transient';
     $connections = json_decode($request->getContent());
     utils::log("master: {$connections->uniqueIdentifier}");
     // Save parameters for the translate process
     $path = $app->dataDir("/{$connections->uniqueIdentifier}.dependencies.json");
     if (file_put_contents($path, json_encode($connections)) === false) {
         utils::log('ERROR: project dependencies not saved :(');
         return new Response('', Response::HTTP_NOT_FOUND, ['Content-Type' => 'text/plain']);
     }
     $result = utils::executeScript("/translate.php lmv:translator {$connections->uniqueIdentifier}", true);
     if ($result === false) {
         return new Response('', Response::HTTP_INTERNAL_SERVER_ERROR, ['Content-Type' => 'text/plain']);
     }
     // We submitted, no clue if it was successful or if it will fail.
     $response = (object) array('status' => 'submitted');
     return new JsonResponse($response, Response::HTTP_OK);
     //- 202 Accepted
 }
 protected function NotifyError()
 {
     $loader = new Twig_Loader_Filesystem(array($this->lmv->viewsDir('', true)));
     $environment = new Twig_Environment($loader, array('debug' => false, 'charset' => 'utf-8', 'strict_variables' => true));
     $content = $environment->render('email-extract-failed.twig', array('ID' => $this->identifier));
     $config = lmv::config();
     $mjet = new Mailjet($config['MAILJET1'], $config['MAILJET2']);
     $mjet->sendContent('ADN Sparks <*****@*****.**>', $config['mailTo'], 'Autodesk View & Data API Extraction failed', 'html', $content);
     //'replyTo': '*****@*****.**',
     //'forceEmbeddedImages': true
 }