energine
[ class tree: energine ] [ index: energine ] [ all elements ]

Source for file FileObject.class.php

Documentation is available at FileObject.class.php

  1. <?php
  2.  
  3. /**
  4.  * Содержит класс FileObject и интерфейс FileSystemObject
  5.  *
  6.  * @package energine
  7.  * @subpackage core
  8.  * @author dr.Pavka
  9.  * @copyright Energine 2006
  10.  * @version $Id$
  11.  */
  12.  
  13. //require_once('core/framework/FileSystemObject.class.php');
  14. //require_once('core/modules/image/components/Image.class.php');
  15.  
  16. /**
  17.  * Класс - модель файла
  18.  *
  19.  * @package energine
  20.  * @subpackage core
  21.  * @author dr.Pavka
  22.  */
  23. class FileObject extends FileSystemObject {
  24.     /**
  25.      * Имя таблицы thumbnail
  26.      */
  27. //    const THUMB_TABLE_NAME = 'share_uploads_thumbnails';
  28.  
  29.     /**
  30.      * Полный путь к файлу
  31.      *
  32.      * @var string 
  33.      * @access private
  34.      */
  35.     private $path;
  36.     /**
  37.      * Конструктор класса
  38.      *
  39.      * @return void 
  40.      */
  41.     public function __construct({
  42.         parent::__construct();
  43.     }
  44.  
  45.     /**
  46.      * Статический метод загрузки возвращающий self
  47.      *
  48.      * @param string путь к файл
  49.      * @return FileObject 
  50.      * @access public
  51.      * @static
  52.      */
  53.  
  54.     public static function loadFrom($path{
  55.         if (!file_exists($path)) {
  56.             throw new SystemException('ERR_DEV_NO_FILE'SystemException::ERR_DEVELOPER$path);
  57.         }
  58.         if (!is_writeable($path)) {
  59.             throw new SystemException('ERR_DEV_UPLOADS_FILE_NOT_WRITABLE'SystemException::ERR_DEVELOPER$path);
  60.         }
  61.  
  62.         $result new FileObject();
  63.         $result->loadData($path);
  64.         $fileName dirname($path).'/.'.basename($path);
  65.         $data array();
  66.         //Для изображений добавляем высоту и ширину
  67.         if (self::getTypeInfo($path== FileSystemObject::IS_IMAGE{
  68.             try {
  69.                 $imgData getimagesize($path);
  70.                 if (!file_exists($fileName)) {
  71.                     $thumb new Image();
  72.                     $thumb->loadFromFile($path);
  73.                     $thumb->resize(50,50);
  74.                     $thumb->saveToFile($fileName);
  75.                 }
  76.                 $data array('thumb'=>$fileName);
  77.             }
  78.             catch (Exception $e{
  79.                 //В этом случае ничего делать не нужно
  80.  
  81.             }
  82.             $data array_merge($dataarray('width'=>$imgData[0]'height'=>$imgData[1]));
  83.         }
  84.  
  85.         $result->setData($data);
  86.  
  87.         return $result;
  88.     }
  89.  
  90.     /**
  91.      * Удаление файла
  92.      *
  93.      * @return boolean 
  94.      * @access public
  95.      */
  96.  
  97.     public function delete({
  98.         if (@unlink($this->getPath())) {
  99.             $path dirname($this->getPath()).'/.'.basename($this->getPath());
  100.             if (file_exists($path)) {
  101.                 @unlink($path);
  102.             }
  103.             parent::delete();
  104.  
  105.         }
  106.     }
  107.  
  108.     /**
  109.      * Сохранение файла
  110.      *
  111.      * @param array 
  112.      * @return boolean 
  113.      * @access public
  114.      */
  115.  
  116.     public function create($data{
  117.         $data $data[self::TABLE_NAME];
  118.         $sourceFileName $data['upl_path'];
  119.         //Копируем файл из временной директории на нужное место
  120.         copy($tmpFile self::getTmpFilePath($sourceFileName)$sourceFileName);
  121.         unlink($tmpFile);
  122.  
  123.         $uplID $this->dbh->modify(QAL::INSERTself::TABLE_NAME$data);
  124.         
  125.         if((self::getTypeInfo($sourceFileName== FileSystemObject::IS_IMAGE&& $this->getConfigValue('thumbnails')){
  126.             foreach($this->getConfigValue('thumbnails.thumbnail'as $thumbnail){
  127.                 $image new Image();
  128.                 $image->loadFromFile($sourceFileName);
  129.                 $image->resize($width = (int)$thumbnail->width$height = (int)$thumbnail->height);
  130.                 $image->saveToFile(self::getThumbFilename($sourceFileName$width$height));
  131.             }
  132.         }
  133.     }
  134.     
  135.     public static function getThumbFilename($sourceFileName$width$height){
  136.         list($dirname$basename$extension$filenamearray_values(pathinfo($sourceFileName));
  137.         return $dirname.'/'.'.'.$filename.'.'.$width.'-'.$height.'.'.$extension;
  138.     }
  139.  
  140.     public static function getTmpFilePath($filename){
  141.         return 'tmp/'.basename($filename);
  142.     }
  143.  
  144.     public static function generateFilename($dirPath$fileExtension){
  145.         /*
  146.          * Генерируем уникальное имя файла.
  147.          */
  148.         $c ''// первый вариант имени не будет включать символ '0'
  149.         do {
  150.             $filename time().rand(110000)."$c.{$fileExtension}";
  151.             $c++// при первом проходе цикла $c приводится к integer(1)
  152.         while(file_exists($dirPath.$filename));
  153.  
  154.         return $filename;
  155.     }
  156.  
  157.  
  158.     /**
  159.      * Создание файла из существующего
  160.      *
  161.      * @return void 
  162.      * @access public
  163.      */
  164.  
  165.     public function createFromPath($path$name{
  166.         $this->dbh->modify(QAL::INSERTself::TABLE_NAMEarray('upl_path'=>$path'upl_name'=>$name));
  167.     }
  168. }
В создании документации нам помог: phpDocumentor