* @package WB * @subpackage vfs */ WBClass::load('WBLog' , 'WBVFS' , 'WBVFS_File'); /** * Virtual File System: Mime * * @version 1.0.0 * @package WB * @subpackage vfs */ abstract class WBVFS_Mime extends WBStdClass { /** * list of mime types that my convert to this one * @var array */ protected $convertable = array(); /** * file name * @var string */ protected $file = null; /** * virtual file * @var WBVFS_File */ protected $vfile; /** * file information * @var array */ protected $info = array(); /** * logger * @var WBLog */ protected $log; /** * major mime type * @var string */ protected $mimeMajor = ''; /** * minor mime type * @var string */ protected $mimeMinor = ''; /** * ask for special minor mime type * @var string */ protected $requestedMimeMinor = WBVFS::MIME_PLAIN; /** * file name extension * @var string */ protected $extension = 'bin'; /** * Whether file view may be incremented * @var bool */ protected $incrementable = false; /** * constructor * * Configure file utiltiy object * * @param array $parameter */ final public function __construct($parameter = array()) { $this->log = WBLog::start(__CLASS__); $this->mimeMajor = strtolower(substr(get_class($this), (strlen(__CLASS__) + 1))); $this->init(); } /** * 2nd constructor * */ protected function init() { } /** * tell mime handler wich file to work with * * @param string $file * @param string $minor * @param string $name */ final public function setFile($file, $minor, $name = '') { $this->file = $file; $this->mimeMinor = $minor; $this->onSetFile($name); } /** * inform when file was set * * @param string $name */ protected function onSetFile($name = '') { } /** * set viretual file * * This function return whether it is possible to convert the file * to target mime type * * @param WBVFS_File $file * @return bool */ final public function setVirtualFile(WBVFS_File $file) { $this->vfile = $file; $this->mimeMajor = $this->vfile->getMime(WBVFS_File::MIME_MAJOR); $this->mimeMinor = $this->vfile->getMime(WBVFS_File::MIME_MINOR); $this->onSetVirtualFile(); $myMime = strtolower(substr(get_class($this), strlen('WBVFS_Mime_'))); if ($this->mimeMajor == $myMime) { return true; } if (!in_array($this->mimeMajor, $this->convertable)) { return false; } return true; } /** * inform when virtual file was set * */ protected function onSetVirtualFile() { } /** * get viretual file * * @return WBVFS_File $file */ final public function getVirtualFile() { return $this->vfile; } /** * execute command * * Each mime type may implement commands like unpack for ziped archives * or rotating for images * * @param string $cmd * @param array $arg */ public function execute($cmd, $arg = array()) { $log = array( 'file' => $this->vfile->getId(), 'mime' => $this->mimeMajor, 'cmd' => $cmd, 'status' => 'notimplemented' ); $this->log->err($log); } /** * get file name of requested file * * This is just a template method that forward call * * @see doGetRequestedFile() * @throws WBException_Argument * @param WBRequest $req * @param WBResponse $res * @param bool $redirect whether to redirect * @param string|null requested minor mime type * @return string */ final public function getRequestedFile($req, $res, &$redirect, $minor = null) { $this->req = $req; $this->res = $res; $this->incrementable = true; if ($minor) { $this->requestedMimeMinor = $minor; } return $this->doGetRequestedFile($redirect); } /** * get file name of requested file * * @param bool $redirect whether to redirect * @return string */ protected function doGetRequestedFile(&$redirect) { $this->mimeMajor = $this->vfile->getMime(WBVFS_File::MIME_MAJOR); $this->mimeMinor = $this->vfile->getMime(WBVFS_File::MIME_MINOR); return $this->vfile->getPath(); } /** * Increment View? * * Tell whether file view should be incremented * @return bool */ public function isIncrementable() { return $this->incrementable; } /** * fetch dynamic mime type * * @param int $type * @return string */ final public function getMime($type = WBVFS_File::MIME_MAJOR) { if ($type == WBVFS_File::MIME_MAJOR) { return $this->mimeMajor; } if ($type == WBVFS_File::MIME_MINOR) { return $this->mimeMinor; } if ($type == (WBVFS_File::MIME_MAJOR | WBVFS_File::MIME_MINOR) ) { return $this->mimeMajor . '/' . $this->mimeMinor; } return null; } /** * get file extension * * Return Windows friendly file extension * * @return string */ public function getExtension() { return $this->extension; } /** * gather information about this file * * Get with and height of images or videos, exif headers, * or content of ZIP-archive. * * @return array */ public function getInfo() { return $this->info; } /** * import file * * Do whatever is required to import file, * of course, this may replace the original file */ public function import() { } /** * tell whether file needs to be queued * * If a file is put in queue, it waits for asynchronious processing. * This is very importand for tasks that just take too long to to * right on import * * @deprecated use event system * @return bool */ public function queue() { return false; } /** * make cache folder * * @return string */ protected function mkCacheDir() { $file = WBClass::create('WBFile'); /** @var $file WBFile */ $varDir = realpath(WBParam::get('wb/dir/base') . '/var'); $myMime = strtolower(substr(get_class($this), strlen('WBVFS_Mime_'))); $cacheDir = 'var/cache' . substr($this->vfile->getPath(), strlen($varDir)) . '/' . $myMime; $file->mkdir($cacheDir); return $file->realpath(); } /** * Execute Command * * @see exec() * @param string * @return */ protected function exec($cmd, $vars = array()) { $cmd = WBString::populate($cmd, $vars); $out = array(); $ret = null; $now = WBClock::now(); exec($cmd, $out, $ret); $log = array( 'action' => 'exec', 'status' => 'ok', 'elapsed' => WBClock::stop($now, 1000, 1) . 'ms', 'cmd' => $cmd ); if (0 < $ret) { $log['status'] = 'failed'; $this->log->warn($log); return false; } $this->log->info($log); return true; } }