Source for file lmbCmsFileStorage.class.php

Documentation is available at lmbCmsFileStorage.class.php

  1. <?php
  2. lmb_require('limb/fs/src/lmbFs.class.php');
  3. lmb_require('limb/net/src/lmbMimeType.class.php');
  4.  
  5. {
  6.   protected $root_dir;
  7.  
  8.   function __construct($root_dir)
  9.   {
  10.     $this->root_dir = $root_dir;
  11.     lmbFs :: mkdir($this->root_dir);
  12.   }
  13.  
  14.   function storeFile($source$mime_type null)
  15.   {
  16.     if(!$mime_type)
  17.       $mime_type lmbMimeType :: getFileMimeType($source);
  18.  
  19.     if($mime_type == 'application/octet-stream')
  20.       $mime_type 'video/x-flv';
  21.  
  22.     $file_id $this->_makeUniqueIdentifier($mime_type);
  23.  
  24.     $dest $this->_getMediaFile($file_id);
  25.     lmbFs :: cp($source$dest);
  26.     return $file_id;
  27.   }
  28.  
  29.   function removeFile($file_id)
  30.   {
  31.     if($file $this->getFilePath($file_id))
  32.     {
  33.       unlink($file);
  34.       return true;
  35.     }
  36.     return false;
  37.   }
  38.  
  39.   function getFilePath($file_id)
  40.   {
  41.     if(!file_exists($file $this->_getMediaFile($file_id)))
  42.        return null;
  43.  
  44.      return $file;
  45.   }
  46.  
  47.   function hasFile($file_id)
  48.   {
  49.     return $this->getFilePath($file_id!== null;
  50.   }
  51.  
  52.   function getFileSize($file_id)
  53.   {
  54.     if(!$file_path $this->getFilePath($file_id))
  55.       return null;
  56.     return filesize($file_path);
  57.   }
  58.  
  59.   function getFileUrl($file_id)
  60.   {
  61.     $result str_replace(strtolower($_SERVER['DOCUMENT_ROOT'])'',
  62.                           strtolower(lmbFs :: normalizePath($this->getFilePath($file_id))));
  63.     return '/' ltrim($result'/');
  64.   }
  65.  
  66.   protected function _getMediaFile($file_id)
  67.   {
  68.     $md5 md5($file_id);
  69.     return $this->root_dir . '/' $md5{0'/' $file_id;
  70.   }
  71.  
  72.   protected function _makeUniqueIdentifier($mime_type)
  73.   {
  74.     return uniqid('.' lmbMimeType :: getExtension($mime_type);
  75.   }
  76. }

Documentation generated on Wed, 07 Jan 2009 03:47:50 +0300 by phpDocumentor 1.3.2