Beispiel #1
0
 public function execute(Request $request, WorkingFolder $workingFolder, Config $config, CacheManager $cache)
 {
     $fileName = (string) $request->get('fileName');
     if (null === $fileName || !File::isValidName($fileName, $config->get('disallowUnsafeCharacters'))) {
         throw new InvalidRequestException('Invalid file name');
     }
     if (!Image::isSupportedExtension(pathinfo($fileName, PATHINFO_EXTENSION))) {
         throw new InvalidNameException('Invalid source file name');
     }
     if (!$workingFolder->containsFile($fileName)) {
         throw new FileNotFoundException();
     }
     $cachePath = Path::combine($workingFolder->getResourceType()->getName(), $workingFolder->getClientCurrentFolder(), $fileName);
     $imageInfo = array();
     $cachedInfo = $cache->get($cachePath);
     if ($cachedInfo && isset($cachedInfo['width']) && isset($cachedInfo['height'])) {
         $imageInfo = $cachedInfo;
     } else {
         $file = new DownloadedFile($fileName, $this->app);
         if ($file->isValid()) {
             $image = Image::create($file->getContents());
             $imageInfo = $image->getInfo();
             $cache->set($cachePath, $imageInfo);
         }
     }
     return $imageInfo;
 }
Beispiel #2
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;
 }
Beispiel #3
0
 public function execute(Request $request, Config $config, WorkingFolder $workingFolder, ResizedImageRepository $resizedImageRepository, CacheManager $cache)
 {
     $fileName = (string) $request->query->get('fileName');
     list($requestedWidth, $requestedHeight) = Image::parseSize((string) $request->get('size'));
     $downloadedFile = new DownloadedFile($fileName, $this->app);
     $downloadedFile->isValid();
     if (!Image::isSupportedExtension(pathinfo($fileName, PATHINFO_EXTENSION), $config->get('thumbnails.bmpSupported'))) {
         throw new InvalidExtensionException('Unsupported image type or not image file');
     }
     Utils::removeSessionCacheHeaders();
     $response = new Response();
     $response->setPublic();
     $response->setEtag(dechex($downloadedFile->getTimestamp()) . "-" . dechex($downloadedFile->getSize()));
     $lastModificationDate = new \DateTime();
     $lastModificationDate->setTimestamp($downloadedFile->getTimestamp());
     $response->setLastModified($lastModificationDate);
     if ($response->isNotModified($request)) {
         return $response;
     }
     $imagePreviewCacheExpires = (int) $config->get('cache.imagePreview');
     if ($imagePreviewCacheExpires > 0) {
         $response->setMaxAge($imagePreviewCacheExpires);
         $expireTime = new \DateTime();
         $expireTime->modify('+' . $imagePreviewCacheExpires . 'seconds');
         $response->setExpires($expireTime);
     }
     $cachedInfoPath = Path::combine($workingFolder->getResourceType()->getName(), $workingFolder->getClientCurrentFolder(), $fileName);
     $cachedInfo = $cache->get($cachedInfoPath);
     $resultImage = null;
     // Try to reuse existing resized image
     if ($cachedInfo && isset($cachedInfo['width']) && isset($cachedInfo['height'])) {
         // Fix received aspect ratio
         $size = Image::calculateAspectRatio($requestedWidth, $requestedHeight, $cachedInfo['width'], $cachedInfo['height']);
         $resizedImage = $resizedImageRepository->getResizedImageBySize($workingFolder->getResourceType(), $workingFolder->getClientCurrentFolder(), $fileName, $size['width'], $size['height']);
         if ($resizedImage) {
             $resultImage = Image::create($resizedImage->getImageData());
         }
     }
     // Fallback - get and resize the original image
     if (null === $resultImage) {
         $resultImage = Image::create($downloadedFile->getContents(), $config->get('thumbnails.bmpSupported'));
         $cache->set($cachedInfoPath, $resultImage->getInfo());
         $resultImage->resize($requestedWidth, $requestedHeight);
     }
     $mimeType = $resultImage->getMimeType();
     if (in_array($mimeType, array('image/bmp', 'image/x-ms-bmp'))) {
         $mimeType = 'image/jpeg';
         // Image::getData() by default converts resized images to JPG
     }
     $response->headers->set('Content-Type', $mimeType . '; name="' . $downloadedFile->getFileName() . '"');
     $response->setContent($resultImage->getData());
     return $response;
 }
Beispiel #4
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;
 }