. */ namespace SP\Modules\Web\Controllers\AccountFile; use Exception; use SP\Core\Events\Event; use SP\Core\Events\EventMessage; use SP\Core\Exceptions\SPException; use SP\Modules\Web\Controllers\Traits\JsonTrait; /** * Class DownloadController * * @package SP\Modules\Web\Controllers */ final class DownloadController extends AccountFileBase { use JsonTrait; /** * Download action * * @param int $id * * @return string */ public function downloadAction(int $id): string { try { if (null === ($fileData = $this->accountFileService->getById($id))) { throw new SPException(__u('File does not exist'), SPException::INFO); } $this->eventDispatcher->notify( 'download.accountFile', new Event( $this, EventMessage::factory() ->addDescription(__u('File downloaded')) ->addDetail(__u('File'), $fileData->getName()) ) ); $response = $this->router->response(); $response->header('Content-Length', $fileData->getSize()); $response->header('Content-Type', $fileData->getType()); $response->header('Content-Description', ' sysPass file'); $response->header('Content-Transfer-Encoding', 'binary'); $response->header('Accept-Ranges', 'bytes'); $type = strtolower($fileData->getType()); if ($type === 'application/pdf') { $disposition = sprintf('inline; filename="%s"', $fileData->getName()); } else { $disposition = sprintf('attachment; filename="%s"', $fileData->getName()); $response->header('Set-Cookie', 'fileDownload=true; path=/'); } $response->header('Content-Disposition', $disposition); $response->body($fileData->getContent()); $response->send(true); } catch (Exception $e) { processException($e); $this->eventDispatcher->notify('exception', new Event($e)); } return ''; } }