Example #1
0
 /**
  * Returns file metadata.
  *
  * @return array
  */
 public function getMetadata()
 {
     if (null === $this->metadata) {
         $filePath = $this->getFilePath();
         $this->metadata = $this->resourceType->getBackend()->getWithMetadata($filePath, array('mimetype', 'timestamp'));
     }
     return $this->metadata;
 }
 /**
  * @param ResourceType $sourceFileResourceType
  * @param string       $sourceFileDir
  * @param string       $sourceFileName
  * @param int          $requestedWidth
  * @param int          $requestedHeight
  */
 public function __construct(ResourceType $sourceFileResourceType, $sourceFileDir, $sourceFileName, $requestedWidth, $requestedHeight)
 {
     $this->sourceFileResourceType = $sourceFileResourceType;
     $this->sourceFileDir = $sourceFileDir;
     $this->sourceFileName = $sourceFileName;
     $this->requestedWidth = $requestedWidth;
     $this->requestedHeight = $requestedHeight;
     $this->backend = $sourceFileResourceType->getBackend();
 }
 /**
  * Adds current folder info to the response
  *
  * @param FilterResponseEvent $event
  */
 public function addCurrentFolderInfo(FilterResponseEvent $event)
 {
     /* @var JsonResponse $response */
     $response = $event->getResponse();
     if ($response instanceof JsonResponse) {
         $responseData = (array) $response->getData();
         $responseData = array('resourceType' => $this->getResourceTypeName(), 'currentFolder' => array('path' => $this->getClientCurrentFolder(), 'acl' => $this->getAclMask())) + $responseData;
         $baseUrl = $this->backend->getBaseUrl();
         if (null !== $baseUrl) {
             $responseData['currentFolder']['url'] = Path::combine($baseUrl, Utils::encodeURLParts(Path::combine($this->resourceType->getDirectory(), $this->getClientCurrentFolder())));
         }
         $response->setData($responseData);
     }
 }
Example #4
0
 /**
  * Returns a URL to a file.
  *
  * If the useProxyCommand option is set for a backend, the returned
  * URL will point to the CKFinder connector Proxy command.
  *
  * @param ResourceType $resourceType      the file resource type
  * @param string       $folderPath        the resource-type relative folder path
  * @param string       $fileName          the file name
  * @param string|null  $thumbnailFileName the thumbnail file name - if the file is a thumbnail
  *
  * @return string|null URL to a file or `null` if the backend does not support it.
  */
 public function getFileUrl(ResourceType $resourceType, $folderPath, $fileName, $thumbnailFileName = null)
 {
     if (isset($this->backendConfig['useProxyCommand'])) {
         $connectorUrl = $this->app->getConnectorUrl();
         $queryParameters = array('command' => 'Proxy', 'type' => $resourceType->getName(), 'currentFolder' => $folderPath, 'fileName' => $fileName);
         if ($thumbnailFileName) {
             $queryParameters['thumbnail'] = $thumbnailFileName;
         }
         $proxyCacheLifetime = (int) $this->ckConfig->get('cache.proxyCommand');
         if ($proxyCacheLifetime > 0) {
             $queryParameters['cache'] = $proxyCacheLifetime;
         }
         return $connectorUrl . '?' . http_build_query($queryParameters, '', '&');
     }
     $path = $thumbnailFileName ? Path::combine($resourceType->getDirectory(), $folderPath, ResizedImage::DIR, $fileName, $thumbnailFileName) : Path::combine($resourceType->getDirectory(), $folderPath, $fileName);
     if (isset($this->backendConfig['baseUrl'])) {
         return Path::combine($this->backendConfig['baseUrl'], Utils::encodeURLParts($path));
     }
     $baseAdapter = $this->getBaseAdapter();
     if (method_exists($baseAdapter, 'getFileUrl')) {
         return $baseAdapter->getFileUrl($path);
     }
     return null;
 }
Example #5
0
 /**
  * @param ResourceType $resourceType resource type
  * @param string       $path         resource type relative path
  */
 public function __construct(ResourceType $resourceType, $path)
 {
     $this->resourceType = $resourceType;
     $this->path = Path::combine($resourceType->getDirectory(), $path);
 }
Example #6
0
 /**
  * Checks if directory contains subdirectories
  *
  * @param Backend      $backend
  * @param ResourceType $resourceType
  * @param string       $clientPath
  * @param Acl          $acl
  *
  * @return bool
  */
 public function containsDirectories(Backend $backend, ResourceType $resourceType, $clientPath, Acl $acl)
 {
     $location = rtrim($this->applyPathPrefix(Path::combine($resourceType->getDirectory(), $clientPath)), '/\\') . '/';
     if (!is_dir($location) || false === ($fh = @opendir($location))) {
         return false;
     }
     $hasChildren = false;
     $resourceTypeName = $resourceType->getName();
     $clientPath = rtrim($clientPath, '/\\') . '/';
     while (false !== ($filename = readdir($fh))) {
         if ($filename == '.' || $filename == '..') {
             continue;
         }
         if (is_dir($location . $filename)) {
             if (!$acl->isAllowed($resourceTypeName, $clientPath . $filename, Permission::FOLDER_VIEW)) {
                 continue;
             }
             if ($backend->isHiddenFolder($filename)) {
                 continue;
             }
             $hasChildren = true;
             break;
         }
     }
     closedir($fh);
     return $hasChildren;
 }
 /**
  * Deletes all thumbnails under the given path defined by the resource type,
  * path and file name.
  *
  * @param ResourceType $resourceType
  * @param string       $path
  * @param string       $fileName
  *
  * @return bool `true` if deleted successfully
  */
 public function deleteThumbnails(ResourceType $resourceType, $path, $fileName = null)
 {
     $path = Path::combine($this->getThumbnailsPath(), $resourceType->getName(), $path, $fileName);
     if ($this->thumbsBackend->has($path)) {
         return $this->thumbsBackend->deleteDir($path);
     }
     return false;
 }
 /**
  * @param ResourceType $sourceFileResourceType
  * @param string       $sourceFilePath
  * @param string       $sourceFileName
  * @param int          $width
  * @param int          $height
  *
  * @return ResizedImage|null
  */
 public function getResizedImageBySize(ResourceType $sourceFileResourceType, $sourceFilePath, $sourceFileName, $width, $height)
 {
     $resizedImagesPath = Path::combine($sourceFileResourceType->getDirectory(), $sourceFilePath, ResizedImage::DIR, $sourceFileName);
     $backend = $sourceFileResourceType->getBackend();
     if (!$backend->hasDirectory($resizedImagesPath)) {
         return null;
     }
     $resizedImagesFiles = array_filter($backend->listContents($resizedImagesPath), function ($v) {
         return isset($v['type']) && $v['type'] === 'file';
     });
     $thresholdPixels = $this->config->get('images.threshold.pixels');
     $thresholdPercent = (double) $this->config->get('images.threshold.percent') / 100;
     foreach ($resizedImagesFiles as $resizedImage) {
         $resizedImageSize = ResizedImage::getSizeFromFilename($resizedImage['basename']);
         $resizedImageWidth = $resizedImageSize['width'];
         $resizedImageHeight = $resizedImageSize['height'];
         if ($resizedImageWidth >= $width && ($resizedImageWidth <= $width + $thresholdPixels || $resizedImageWidth <= $width + $width * $thresholdPercent) && $resizedImageHeight >= $height && ($resizedImageHeight <= $height + $thresholdPixels || $resizedImageHeight <= $height + $height * $thresholdPercent)) {
             $resizedImage = new ResizedImage($this, $sourceFileResourceType, $sourceFilePath, $sourceFileName, $resizedImageWidth, $resizedImageHeight);
             if ($resizedImage->exists()) {
                 $resizedImage->load();
                 return $resizedImage;
             }
         }
     }
     return null;
 }
Example #9
0
 /**
  * Returns a path based on resource type and resource type relative path
  *
  * @param ResourceType $resourceType resource type
  * @param string       $path         resource type relative path
  *
  * @return string path to be used with backend adapter
  */
 public function buildPath(ResourceType $resourceType, $path)
 {
     return Path::combine($resourceType->getDirectory(), $path);
 }
Example #10
0
 /**
  * Replaces double extensions disallowed for the resource type.
  *
  * @param string       $fileName
  * @param ResourceType $resourceType
  *
  * @return string file name with replaced double extensions.
  */
 public static function replaceDisallowedExtensions($fileName, ResourceType $resourceType)
 {
     $pieces = explode('.', $fileName);
     $basename = array_shift($pieces);
     $lastExtension = array_pop($pieces);
     foreach ($pieces as $ext) {
         $basename .= $resourceType->isAllowedExtension($ext) ? '.' : '_';
         $basename .= $ext;
     }
     // Add the last extension to the final name.
     return $basename . '.' . $lastExtension;
 }
 /**
  * Creates a path in format:
  * [backend name]://backend/relative/path
  *
  * @param ResourceType $resourceType resource type
  * @param string       $path         backend relative path
  * @return string formatted path
  */
 protected function createPath(ResourceType $resourceType, $path)
 {
     return $resourceType->getBackend()->getName() . '://' . $path;
 }