/** * テンプレート一覧からのダウンロード * * @param Application $app * @param Request $request * @param $id */ public function download(Application $app, Request $request, $id) { /** @var $Template \Eccube\Entity\Template */ $Template = $app['eccube.repository.template']->find($id); if (!$Template) { throw new NotFoundHttpException(); } // 該当テンプレートのディレクトリ $config = $app['config']; $templateCode = $Template->getCode(); $targetRealDir = $config['root_dir'] . '/app/template/' . $templateCode; $targetHtmlRealDir = $config['root_dir'] . '/html/template/' . $templateCode; // 一時ディレクトリ $uniqId = sha1(Str::random(32)); $tmpDir = $config['template_temp_realdir'] . '/' . $uniqId; $appDir = $tmpDir . '/app'; $htmlDir = $tmpDir . '/html'; // ファイル名 $tarFile = $config['template_temp_realdir'] . '/' . $uniqId . '.tar'; $tarGzFile = $tarFile . '.gz'; $downloadFileName = $Template->getCode() . '.tar.gz'; // 該当テンプレートを一時ディレクトリへコピーする. $fs = new Filesystem(); $fs->mkdir(array($appDir, $htmlDir)); $fs->mirror($targetRealDir, $appDir); $fs->mirror($targetHtmlRealDir, $htmlDir); // tar.gzファイルに圧縮する. $phar = new \PharData($tarFile); $phar->buildFromDirectory($tmpDir); // appディレクトリがない場合は, 空ディレクトリを追加 // @see https://github.com/EC-CUBE/ec-cube/issues/742 if (empty($phar['app'])) { $phar->addEmptyDir('app'); } $phar->compress(\Phar::GZ); // ダウンロード完了後にファイルを削除する. // http://stackoverflow.com/questions/15238897/removing-file-after-delivering-response-with-silex-symfony $app->finish(function (Request $request, Response $response, \Silex\Application $app) use($tmpDir, $tarFile, $tarGzFile) { $app['monolog']->addDebug('remove temp file: ' . $tmpDir); $app['monolog']->addDebug('remove temp file: ' . $tarFile); $app['monolog']->addDebug('remove temp file: ' . $tarGzFile); $fs = new Filesystem(); $fs->remove($tmpDir); $fs->remove($tarFile); $fs->remove($tarGzFile); }); return $app->sendFile($tarGzFile)->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $downloadFileName); }