public function file(Silex\Application $app)
 {
     $request = new \Flow\Request();
     $chunkNumber = $request->getCurrentChunkNumber() ?: 0;
     $chunkSize = $request->getDefaultChunkSize() ?: 0;
     $totalSize = $request->getTotalSize() ?: 0;
     $identifier = $request->getIdentifier() ?: '';
     $filename = $request->getFileName() ?: '';
     $files = $request->getFile();
     if (!$files || !$files['size']) {
         utils::log('invalid_flow_request');
         return new Response('', Response::HTTP_OK, ['Content-Type' => 'text/plain']);
     }
     $original_filename = $files['name'];
     //['originalFilename'] ;
     $validation = $this->validateRequest($chunkNumber, $chunkSize, $totalSize, $identifier, $filename, $files['size']);
     if ($validation !== 'valid') {
         utils::log($validation);
         return new Response('', Response::HTTP_OK, ['Content-Type' => 'text/plain']);
     }
     $chunkFilename = $this->getChunkFilename($chunkNumber, $identifier);
     // Save the chunk
     //$fs =new Filesystem () ;
     //$fs->rename ($files ['tmp_name'], $chunkFilename, true) ;
     move_uploaded_file($files['tmp_name'], $chunkFilename);
     // Do we have all the chunks?
     $currentTestChunk = 1;
     //$numberOfChunks =max (intval (floor ($totalSize / ($chunkSize * 1.0))), 1) ;
     $numberOfChunks = $request->getTotalChunks();
     for (; $currentTestChunk <= $numberOfChunks; $currentTestChunk++) {
         if (!utils::realpath($this->getChunkFilename($currentTestChunk, $identifier))) {
             break;
         }
     }
     if ($currentTestChunk > $numberOfChunks) {
         // done
         utils::log("POST done {$original_filename} {$identifier}");
         $data = (object) array('key' => $identifier, 'name' => $original_filename, 'size' => $totalSize, 'bytesRead' => $totalSize, 'bytesPosted' => 0);
         $path = $app->dataDir("/{$identifier}.json");
         if (file_put_contents($path, json_encode($data)) === false) {
             utils::log("Could not save {$path}");
         }
         $path = $app->tmpDir("/{$original_filename}");
         $this->save($path, $request);
         // 				var st =fs.createWriteStream ('./tmp/' + original_filename)
         // 				.on ('finish', function () {}) ;
         // 				flow.write (identifier, st, {
         // 					end: true,
         // 					onDone: function () {
         // 						flow.clean (identifier) ;
         // 					}
         // 				}) ;
     } else {
         // partly_done
         utils::log("POST partly_done {$original_filename} {$identifier}");
     }
     //if ( ACCESS_CONTROLL_ALLOW_ORIGIN )
     //	return (new Response ('', Response::HTTP_OK, [ 'Access-Control-Allow-Origin' => '*', Content-Type' => 'text/plain' ])) ;
     return new Response('', Response::HTTP_OK, ['Content-Type' => 'text/plain']);
 }
 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 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
 }