/** * Physically creates asset * * @param string $assetPath path to the asset original file * @param boolean $move do move or just copy ? * @param boolean $move check duplicate? */ public function create($assetPath, $move = true, $checkDuplicate = true) { if (!is_file($assetPath)) { throw new sfAssetException('Asset "%asset%" not found', array('%asset%' => $assetPath)); } // calculate asset properties if (!$this->getFilename()) { list(, $filename) = sfAssetsLibraryTools::splitPath($assetPath); $this->setFilename($filename); } // check folder if (!$this->getFolder()->existsPhysical()) { $this->getFolder()->create(); } elseif ($checkDuplicate && sfAssetTable::getInstance()->exists($this->getFolder()->getId(), $this->filename)) { $this->setFilename(time() . $this->getFilename()); } $this->setFilesize((int) (filesize($assetPath) / 1024)); $this->autoSetType(); if (sfConfig::get('app_sfAssetsLibrary_check_type', false) && !in_array($this->getType(), sfConfig::get('app_sfAssetsLibrary_types', array('image', 'txt', 'archive', 'pdf', 'xls', 'doc', 'ppt')))) { throw new sfAssetException('Filetype "%type%" not allowed', array('%type%' => $this->getType())); } $ok = $move ? @rename($assetPath, $this->getFullPath()) : @copy($assetPath, $this->getFullPath()); if (!$ok) { // throw new sfAssetException(sprintf('A problem occurred during while saving "%s", "%s"', $this->getFullPath(), $this->filename)); throw new sfAssetException('A problem occurred during while saving "%file%"', array('%file%' => $this->getFullPath())); } if ($this->supportsThumbnails()) { sfAssetsLibraryTools::createThumbnails($this->getFolderPath(), $this->getFilename(), $this->isPdf()); } }
/** * Retrieves a sfAsset object from a relative URL like * /medias/foo/bar.jpg * i.e. the kind of URL returned by $sf_asset->getUrl() */ public function retrieveFromUrl($url) { $url = sfAssetFolderTable::getInstance()->cleanPath($url, '/'); list($relPath, $filename) = sfAssetsLibraryTools::splitPath($url, '/'); $query = $this->createQuery('a')->where('filename = ?', $filename)->leftJoin('a.Folder f')->andWhere('f.relative_path = ?', $relPath ? $relPath : null); return $query->fetchOne(); }
/** * Retrieves a sfAsset object from a relative URL like * /medias/foo/bar.jpg * i.e. the kind of URL returned by $sf_asset->getUrl() */ public static function retrieveFromUrl($url) { $url = sfAssetFolderPeer::cleanPath($url); list($relPath, $filename) = sfAssetsLibraryTools::splitPath($url); $c = new Criteria(); $c->add(sfAssetPeer::FILENAME, $filename); $c->addJoin(sfAssetPeer::FOLDER_ID, sfAssetFolderPeer::ID); $c->add(sfAssetFolderPeer::RELATIVE_PATH, $relPath ? $relPath : null); return sfAssetPeer::doSelectOne($c); }
/** * Recursively creates parent folders * * @param string $path * @return sfAssetFolder */ public static function createFromPath($path) { $path = self::cleanPath($path); list($parent_path, $name) = sfAssetsLibraryTools::splitPath($path); if (!($parent_folder = self::retrieveByPath($parent_path))) { $parent_folder = self::createFromPath($parent_path); $parent_folder->save(); } $folder = new sfAssetFolder(); $folder->setName($name); $folder->setRelativePath($path); $folder->insertAsLastChildOf($parent_folder); $folder->save(); return $folder; }
/** * Retrieves a sfAsset object from a relative URL like * /medias/foo/bar.jpg * i.e. the kind of URL returned by $sf_asset->getUrl() */ public static function retrieveFromUrl($url) { $url = sfAssetFolderPeer::cleanPath($url); list($relPath, $filename) = sfAssetsLibraryTools::splitPath($url); return Doctrine::getTable('sfAsset a')->createQuery()->leftJoin('sfAssetFolder f')->where('f.relative_path = ? AND a.filename = ', $relPath ? $relPath : null, $filename)->fetchOne(); }
/** * Overrrides the parent synchronizeWith() method to add a fix when calling * insertAsLastChildOf($this->reload()) because $this->reload() doesn't return * $this. * * @see parent::synchronizeWith() */ public function synchronizeWith($baseFolder, $verbose = true, $removeOrphanAssets = false, $removeOrphanFolders = false) { if (!is_dir($baseFolder)) { throw new sfAssetException(sprintf('%s is not a directory', $baseFolder)); } $files = sfFinder::type('file')->maxdepth(0)->ignore_version_control()->in($baseFolder); $assets = $this->getAssetsWithFilenames(); foreach ($files as $file) { if (!array_key_exists(basename($file), $assets)) { // File exists, asset does not exist: create asset $sfAsset = new sfAsset(); $sfAsset->setFolderId($this->getId()); $sfAsset->create($file, false); $sfAsset->save(); if ($verbose) { sfAssetsLibraryTools::log(sprintf("Importing file %s", $file), 'green'); } } else { // File exists, asset exists: do nothing unset($assets[basename($file)]); } } foreach ($assets as $name => $asset) { if ($removeOrphanAssets) { // File does not exist, asset exists: delete asset $asset->delete(); if ($verbose) { sfAssetsLibraryTools::log(sprintf("Deleting asset %s", $asset->getUrl()), 'yellow'); } } else { if ($verbose) { sfAssetsLibraryTools::log(sprintf("Warning: No file for asset %s", $asset->getUrl()), 'red'); } } } $dirs = sfFinder::type('dir')->maxdepth(0)->discard(sfConfig::get('app_sfAssetsLibrary_thumbnail_dir', 'thumbnail'))->ignore_version_control()->in($baseFolder); $folders = $this->getSubfoldersWithFolderNames(); foreach ($dirs as $dir) { list(, $name) = sfAssetsLibraryTools::splitPath($dir); if (!array_key_exists($name, $folders)) { $this->reload(); // dir exists in filesystem, not in database: create folder in database $sfAssetFolder = new sfAssetFolder(); $sfAssetFolder->insertAsLastChildOf($this); $sfAssetFolder->setName($name); $sfAssetFolder->save(); if ($verbose) { sfAssetsLibraryTools::log(sprintf("Importing directory %s", $dir), 'green'); } } else { // dir exists in filesystem and database: look inside $sfAssetFolder = $folders[$name]; unset($folders[$name]); } $sfAssetFolder->synchronizeWith($dir, $verbose, $removeOrphanAssets, $removeOrphanFolders); } foreach ($folders as $name => $folder) { if ($removeOrphanFolders) { $folder->delete(null, true); if ($verbose) { sfAssetsLibraryTools::log(sprintf("Deleting folder %s", $folder->getRelativePath()), 'yellow'); } } else { if ($verbose) { sfAssetsLibraryTools::log(sprintf("Warning: No directory for folder %s", $folder->getRelativePath()), 'red'); } } } }
/** * Physically creates asset * * @param string $asset_path path to the asset original file * @param bool $move do move or just copy ? */ public function create($asset_path, $move = true, $checkDuplicate = true) { if (!is_file($asset_path)) { throw new sfAssetException('Asset "%asset%" not found', array('%asset%' => $asset_path)); } // calculate asset properties if (!$this->getFilename()) { list(, $filename) = sfAssetsLibraryTools::splitPath($asset_path); $this->setFilename($filename); } // check folder if (!$this->getsfAssetFolder()->exists()) { $this->getsfAssetFolder()->create(); } else { // check if a file with this name already exists if ($checkDuplicate && sfAssetPeer::exists($this->getsfAssetFolder()->getId(), $this->getFilename())) { $this->setFilename(time() . $this->getFilename()); } } $this->setFilesize((int) filesize($asset_path) / 1024); $this->autoSetType(); if (sfConfig::get('app_sfAssetsLibrary_check_type', false) && !in_array($this->getType(), sfConfig::get('app_sfAssetsLibrary_types', array('image', 'txt', 'archive', 'pdf', 'xls', 'doc', 'ppt')))) { throw new sfAssetException('Filetype "%type%" not allowed', array('%type%' => $this->getType())); } if ($move) { rename($asset_path, $this->getFullPath()); } else { copy($asset_path, $this->getFullPath()); } if ($this->supportsThumbnails()) { sfAssetsLibraryTools::createThumbnails($this->getFolderPath(), $this->getFilename()); } }
<?php /** * @author MaGénération */ include dirname(__FILE__) . '/../../../../test/bootstrap/unit.php'; $pluginPaths = $configuration->getAllPluginPaths(); include $pluginPaths['sfDoctrineAssetsLibraryPlugin'] . '/lib/sfAssetsLibraryTools.class.php'; // run the test $t = new lime_test(5, array('options' => new lime_output_color(), 'error_reporting' => true)); $t->diag('assets tools test'); list($base, $name) = sfAssetsLibraryTools::splitPath('simple'); $t->is($name, 'simple', 'splitPath() gives same name on simple strings'); $t->is($base, '', 'splitPath() gives empty base on simple strings'); list($base, $name) = sfAssetsLibraryTools::splitPath('root' . DIRECTORY_SEPARATOR . 'file'); $t->is($name, 'file', 'splitPath() splits by / gives good name'); $t->is($base, 'root', 'splitPath() splits by / gives good simple base'); list($base, $name) = sfAssetsLibraryTools::splitPath(DIRECTORY_SEPARATOR . 'Articles' . DIRECTORY_SEPARATOR . 'People' . DIRECTORY_SEPARATOR . 'Sarkozy'); $t->is($base, DIRECTORY_SEPARATOR . 'Articles' . DIRECTORY_SEPARATOR . 'People', 'splitPath() splits by DIRECTORY_SEPARATOR gives good composed base');
<?php /** * @author MaGénération */ include dirname(__FILE__) . '/../../../../test/bootstrap/unit.php'; define('SF_APP', 'backend'); define('SF_ENVIRONMENT', 'dev'); define('SF_DEBUG', 1); require_once SF_ROOT_DIR . DIRECTORY_SEPARATOR . 'apps' . DIRECTORY_SEPARATOR . SF_APP . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'config.php'; $databaseManager = new sfDatabaseManager(); $databaseManager->initialize(); $con = Propel::getConnection(); // run the test $t = new lime_test(5, new lime_output_color()); $t->diag('assets tools test'); list($base, $name) = sfAssetsLibraryTools::splitPath('simple'); $t->is($name, 'simple', 'splitPath() gives same name on simple strings'); $t->is($base, '', 'splitPath() gives empty base on simple strings'); list($base, $name) = sfAssetsLibraryTools::splitPath('root/file'); $t->is($name, 'file', 'splitPath() splits by / gives good name'); $t->is($base, 'root', 'splitPath() splits by / gives good simple base'); list($base, $name) = sfAssetsLibraryTools::splitPath('/Articles/People/Sarkozy'); $t->is($base, '/Articles/People', 'splitPath() splits by / gives good composed base');