Example #1
0
 /**
  * Get web (external) path for file
  * @param File $file
  * @param ImageSize $size | string $size
  * @return string 
  */
 public function getWebPath(File $file, $size = null)
 {
     if (!$file instanceof File) {
         throw new Exception\RuntimeException('File or folder entity expected');
     }
     if ($file instanceof Image && isset($size)) {
         if (!$size instanceof ImageSize) {
             $size = $file->findImageSize($size);
         }
     } else {
         $size = null;
     }
     if ($file->isPublic()) {
         $path = $file->getPathEntity()->getWebPath();
         $pathParts = explode('/', $path);
         $fileName = array_pop($pathParts);
         // Get file storage url base in webroot
         $path = '/' . trim(implode('/', $pathParts), '/\\') . '/';
         if ($size instanceof ImageSize) {
             $path .= self::RESERVED_DIR_SIZE . '/' . rawurlencode($size->getFolderName()) . '/';
         }
         return $path . $fileName;
     }
     return null;
 }