/**
  * @Request({"type"}, csrf=true)
  * @Response("json")
  */
 public function uploadAction($type = null)
 {
     try {
         $file = $this['request']->files->get('file');
         if ($file === null || !$file->isValid()) {
             throw new Exception(__('No file uploaded.'));
         }
         $package = $this->loadPackage($upload = $file->getPathname());
         if ($type != $package->getType()) {
             throw new Exception(__('Invalid package type.'));
         }
         if ($this->isCore($package->getName())) {
             throw new Exception(__('Core extensions may not be installed.'));
         }
         Zip::extract($upload, "{$this->temp}/" . ($path = sha1($upload)));
         $extra = $package->getExtra();
         if (isset($extra['image'])) {
             $extra['image'] = $this['url']->to("{$this->temp}/{$path}/" . $extra['image']);
         } else {
             $extra['image'] = $this['url']->to('extension://system/assets/images/placeholder-icon.svg');
         }
         $response = ['package' => ['name' => $package->getName(), 'type' => $package->getType(), 'title' => $package->getTitle(), 'description' => $package->getDescription(), 'version' => $package->getVersion(), 'author' => $package->getAuthor(), 'shasum' => sha1_file($upload), 'extra' => $extra], 'install' => $path];
     } catch (Exception $e) {
         $response = ['error' => $e->getMessage()];
     }
     return $response;
 }
 public function testExtract()
 {
     // create test zip
     $zipFile = $this->workspace . '/test.zip';
     $zipArchive = new \ZipArchive();
     if (!$zipArchive->open($zipFile, \ZIPARCHIVE::OVERWRITE)) {
         $this->markTestIncomplete(sprintf('Unable to open zip archive at %s.', $zipFile));
     }
     $zipArchive->addFile(__DIR__ . '/Fixtures/test', 'test');
     if (!$zipArchive->status == \ZIPARCHIVE::ER_OK) {
         $this->markTestIncomplete(sprintf('Unable to build zip archive at %s.', $zipFile));
     }
     $zipArchive->close();
     $this->zip->extract($zipFile, $this->workspace . '/testFolder');
     $this->assertFileExists($this->workspace . '/testFolder/test');
 }
 /**
  * Download a package file.
  *
  * @param string $path
  * @param string $url
  * @param string $shasum
  */
 public function downloadFile($path, $url, $shasum = '')
 {
     $file = $path . '/' . uniqid();
     try {
         $data = $this->client->get($url)->getBody();
         if ($shasum && sha1($data) !== $shasum) {
             throw new ChecksumVerificationException("The file checksum verification failed");
         }
         if (!$this->files->makeDir($path) || !$this->files->putContents($file, $data)) {
             throw new NotWritableException("The path is not writable ({$path})");
         }
         if (Zip::extract($file, $path) !== true) {
             throw new ArchiveExtractionException("The file extraction failed");
         }
         $this->files->delete($file);
     } catch (\Exception $e) {
         $this->files->delete($path);
         if ($e instanceof TransferException) {
             if ($e instanceof BadResponseException) {
                 throw new UnauthorizedDownloadException("Unauthorized download ({$url})");
             }
             throw new DownloadErrorException("The file download failed ({$url})");
         }
         throw $e;
     }
 }