예제 #1
0
 public function execute(Request $request, WorkingFolder $workingFolder, EventDispatcher $dispatcher)
 {
     $fileName = (string) $request->query->get('fileName');
     $downloadedFile = new DownloadedFile($fileName, $this->app);
     $downloadedFile->isValid();
     $downloadedFileEvent = new DownloadFileEvent($this->app, $downloadedFile);
     $dispatcher->dispatch(CKFinderEvent::DOWNLOAD_FILE, $downloadedFileEvent);
     if ($downloadedFileEvent->isPropagationStopped()) {
         throw new AccessDeniedException();
     }
     $response = new StreamedResponse();
     $response->headers->set('Cache-Control', 'cache, must-revalidate');
     $response->headers->set('Pragma', 'public');
     $response->headers->set('Expires', '0');
     if ($request->get('format') === 'text') {
         $response->headers->set('Content-Type', 'text/plain; charset=utf-8');
     } else {
         $userAgent = !empty($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
         $encodedName = str_replace("\"", "\\\"", $fileName);
         if (strpos($userAgent, 'MSIE') !== false) {
             $encodedName = str_replace(array("+", "%2E"), array(" ", "."), urlencode($encodedName));
         }
         $response->headers->set('Content-Type', 'application/octet-stream; name="' . $fileName . '"');
         $response->headers->set('Content-Disposition', 'attachment; filename="' . $encodedName . '"');
     }
     $response->headers->set('Content-Length', $downloadedFile->getSize());
     $fileStream = $workingFolder->readStream($downloadedFile->getFilename());
     $chunkSize = 1024 * 100;
     // how many bytes per chunk
     $response->setCallback(function () use($fileStream, $chunkSize) {
         if ($fileStream === false) {
             return false;
         }
         while (!feof($fileStream)) {
             echo fread($fileStream, $chunkSize);
             flush();
             @set_time_limit(8);
         }
         return true;
     });
     return $response;
 }
예제 #2
0
 public function execute(Request $request, WorkingFolder $workingFolder, EventDispatcher $dispatcher, Config $config)
 {
     $fileName = (string) $request->query->get('fileName');
     $thumbnailFileName = (string) $request->query->get('thumbnail');
     if (!File::isValidName($fileName, $config->get('disallowUnsafeCharacters'))) {
         throw new InvalidRequestException(sprintf('Invalid file name: %s', $fileName));
     }
     $cacheLifetime = (int) $request->query->get('cache');
     if (!$workingFolder->containsFile($fileName)) {
         throw new FileNotFoundException();
     }
     if ($thumbnailFileName) {
         if (!File::isValidName($thumbnailFileName, $config->get('disallowUnsafeCharacters'))) {
             throw new InvalidRequestException(sprintf('Invalid resized image file name: %s', $fileName));
         }
         if (!$workingFolder->getResourceType()->isAllowedExtension(pathinfo($thumbnailFileName, PATHINFO_EXTENSION))) {
             throw new InvalidExtensionException();
         }
         $resizedImageRespository = $this->app->getResizedImageRepository();
         $file = $resizedImageRespository->getExistingResizedImage($workingFolder->getResourceType(), $workingFolder->getClientCurrentFolder(), $fileName, $thumbnailFileName);
         $dataStream = $file->readStream();
     } else {
         $file = new DownloadedFile($fileName, $this->app);
         $file->isValid();
         $dataStream = $workingFolder->readStream($file->getFilename());
     }
     $proxyDownload = new ProxyDownloadEvent($this->app, $file);
     $dispatcher->dispatch(CKFinderEvent::PROXY_DOWNLOAD, $proxyDownload);
     if ($proxyDownload->isPropagationStopped()) {
         throw new AccessDeniedException();
     }
     $response = new StreamedResponse();
     $response->headers->set('Content-Type', $file->getMimeType());
     $response->headers->set('Content-Length', $file->getSize());
     $response->headers->set('Content-Disposition', 'inline; filename="' . $fileName . '"');
     if ($cacheLifetime > 0) {
         Utils::removeSessionCacheHeaders();
         $response->setPublic();
         $response->setEtag(dechex($file->getTimestamp()) . "-" . dechex($file->getSize()));
         $lastModificationDate = new \DateTime();
         $lastModificationDate->setTimestamp($file->getTimestamp());
         $response->setLastModified($lastModificationDate);
         if ($response->isNotModified($request)) {
             return $response;
         }
         $response->setMaxAge($cacheLifetime);
         $expireTime = new \DateTime();
         $expireTime->modify('+' . $cacheLifetime . 'seconds');
         $response->setExpires($expireTime);
     }
     $chunkSize = 1024 * 100;
     $response->setCallback(function () use($dataStream, $chunkSize) {
         if ($dataStream === false) {
             return false;
         }
         while (!feof($dataStream)) {
             echo fread($dataStream, $chunkSize);
             flush();
             @set_time_limit(8);
         }
         return true;
     });
     return $response;
 }