/**
  * @Request({"update": "json"})
  * @Response("json")
  */
 public function downloadAction($update = null)
 {
     try {
         if ($update) {
             $this['session']->set('system.update', $update);
         } else {
             throw new Exception(__('Unable to find update.'));
         }
         $this['session']->set('system.updateDir', $path = $this->temp . '/' . sha1(uniqid()));
         $client = new Client();
         $client->setDefaultOption('query/api_key', $this->apiKey);
         $downloader = new PackageDownloader($client);
         $downloader->downloadFile($path, $update['url'], $update['shasum']);
         $response = ['message' => __('Copying files...'), 'step' => $this['url']->route('@system/update/copy'), 'progress' => 33];
     } catch (ArchiveExtractionException $e) {
         $response = ['error' => __('Package extraction failed.')];
     } catch (ChecksumVerificationException $e) {
         $response = ['error' => __('Package checksum verification failed.')];
     } catch (UnauthorizedDownloadException $e) {
         $response = ['error' => __('Invalid API key.')];
     } catch (DownloadErrorException $e) {
         $response = ['error' => __('Package download failed.')];
     } catch (NotWritableException $e) {
         $response = ['error' => __('Path is not writable.')];
     } catch (Exception $e) {
         $response = ['error' => $e->getMessage()];
     }
     return $response;
 }
 /**
  * @Request({"package": "json", "path": "alnum"}, csrf=true)
  * @Response("json")
  */
 public function installAction($package = null, $path = '')
 {
     try {
         if ($package !== null && isset($package['dist'])) {
             $path = sha1(json_encode($package));
             $client = new Client();
             $client->setDefaultOption('query/api_key', $this->apiKey);
             $downloader = new PackageDownloader($client);
             $downloader->downloadFile("{$this->temp}/{$path}", $package['dist']['url'], $package['dist']['shasum']);
         }
         if (!$path) {
             throw new Exception(__('Path not found.'));
         }
         $package = $this->loadPackage($path = "{$this->temp}/{$path}");
         if ($package->getType() == 'theme') {
             $this->installTheme("{$path}/theme.json", $package);
         } else {
             $this->installExtension("{$path}/extension.json", $package);
         }
         $this['system']->clearCache();
         $response = ['message' => __('Package "%name%" installed.', ['%name%' => $package->getName()])];
     } catch (ArchiveExtractionException $e) {
         $response = ['error' => __('Package extraction failed.')];
     } catch (ChecksumVerificationException $e) {
         $response = ['error' => __('Package checksum verification failed.')];
     } catch (UnauthorizedDownloadException $e) {
         $response = ['error' => __('Invalid API key.')];
     } catch (DownloadErrorException $e) {
         $response = ['error' => __('Package download failed.')];
     } catch (NotWritableException $e) {
         $response = ['error' => __('Path is not writable.')];
     } catch (Exception $e) {
         $response = ['error' => $e->getMessage()];
     }
     if (strpos($path, $this->temp) === 0 && file_exists($path)) {
         $this['file']->delete($path);
     }
     return $response;
 }