/** * 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; }
/** * * @param object $task * @param array $args */ function run_sfassetlibrary_synchronize($task, $args, $options) { if (!count($args)) { sfAssetsLibraryTools::log('Usage: php symfony sfassetlibrary-synchronize [app] [dirname] --notVerbose --removeOrphanAssets --removeOrphanFolders'); return; } $app = $args[0]; if (!is_dir(sfConfig::get('sf_app_dir') . DIRECTORY_SEPARATOR . $app)) { throw new Exception('The app "' . $app . '" does not exist.'); } if (!isset($args[1])) { throw new Exception('You must define a sychronization folder'); } $base_folder = $args[1]; $verbose = array_key_exists('notVerbose', $options) ? false : true; $removeOrphanAssets = array_key_exists('removeOrphanAssets', $options) ? true : false; $removeOrphanFolders = array_key_exists('removeOrphanFolders', $options) ? true : false; // define constants define('SF_ROOT_DIR', sfConfig::get('sf_root_dir')); define('SF_APP', $app); define('SF_ENVIRONMENT', 'dev'); define('SF_DEBUG', true); // get configuration require_once SF_ROOT_DIR . DIRECTORY_SEPARATOR . 'apps' . DIRECTORY_SEPARATOR . SF_APP . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'config.php'; // initialize database manager $databaseManager = new sfDatabaseManager(); $databaseManager->initialize(); $con = Propel::getConnection(); // synchronize sfAssetsLibraryTools::log(sprintf("Comparing files from %s with assets stored in the database...", $base_folder), 'green'); $rootFolder = sfAssetFolderPeer::getRoot(); try { $rootFolder->synchronizeWith($base_folder, $verbose, $removeOrphanAssets, $removeOrphanFolders); } catch (sfAssetException $e) { throw new sfException(strtr($e->getMessage(), $e->getMessageParams())); } echo pakeColor::colorize("Synchronization complete\n", 'INFO'); }
/** * 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'); } } } }
/** * Change asset directory and/or name * * @param sfAssetFolder $newFolder * @param string $newFilename */ public function move(sfAssetFolder $newFolder, $newFilename = null) { if (sfAssetPeer::exists($newFolder->getId(), $newFilename ? $newFilename : $this->getFilename())) { throw new sfAssetException('The target folder "%folder%" already contains an asset named "%name%". The asset has not been moved.', array('%folder%' => $newFolder->getName(), '%name%' => $newFilename ? $newFilename : $this->getFilename())); } $oldFilepaths = $this->getFilepaths(); if ($newFilename) { if (sfAssetsLibraryTools::sanitizeName($newFilename) != $newFilename) { throw new sfAssetException('The filename "%name%" contains incorrect characters. The asset has not be altered.', array('%name%' => $newFilename)); } $this->setFilename($newFilename); } $this->setFolderId($newFolder->getId()); $success = true; foreach ($oldFilepaths as $type => $filepath) { $success = rename($filepath, $this->getFullPath($type)) && $success; } if (!$success) { throw new sfAssetException('Some or all of the file operations failed. It is possible that the moved asset or its thumbnails are missing.'); } }
/** * Change folder name * * @param string $name */ private function rename() { if ($this->hasSibling($this->getName())) { throw new sfAssetException('The parent folder already contains a folder named "%name%". The folder has not been renamed.', array('%name%' => $this->getName())); } if (sfAssetsLibraryTools::sanitizeName($this->getName()) != $this->getName()) { throw new sfAssetException('The target folder name "%name%" contains incorrect characters. The folder has not be renamed.', array('%name%' => $this->getName())); } $new_path = $this->getFullPath(); $old_path = $this->old_path; // move its assets self::movePhysically($old_path, $new_path); if ($this->getNode()->hasChildren()) { $children = $this->getNode()->getChildren(); foreach ($children as $descendant) { $descendant->save(); } } }
/** * Also delete all contents * * @param Connection $con * @param Boolean $force If true, do not throw an exception if the physical directories cannot be removed */ public function delete(PropelPDO $con = null, $force = false) { $success = true; foreach ($this->getDescendants() as $descendant) { $success = $descendant->delete($con, $force) && $success; } foreach ($this->getsfAssets() as $asset) { $success = $asset->delete() && $success; } // Remove thumbnail subdir $success = rmdir(sfAssetsLibraryTools::getThumbnailDir($this->getFullPath())) && $success; // Remove dir itself $success = rmdir($this->getFullPath()) && $success; if ($success || $force) { parent::delete($con); } else { throw new sfAssetException('Impossible to delete folder "%name%"', array('%name%' => $this->getName())); } }
<?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 // guess current application if (!isset($app)) { $app = 'frontend'; } require_once dirname(__FILE__) . '/../../../../config/ProjectConfiguration.class.php'; $configuration = ProjectConfiguration::getApplicationConfiguration($app, 'test', isset($debug) ? $debug : true); sfContext::createInstance($configuration); // remove all cache sfToolkit::clearDirectory(sfConfig::get('sf_app_cache_dir')); sfConfig::set('app_sfAssetsLibrary_upload_dir', 'media'); // clear possible fixture directories $mediaDir = sfAssetsLibraryTools::getMediaDir(true) . sfConfig::get('app_sfAssetsLibrary_upload_dir', 'media'); @unlink($mediaDir . '/TESTsubdir1/thumbnail/large_demo.png'); @unlink($mediaDir . '/TESTsubdir1/thumbnail/small_demo.png'); @unlink($mediaDir . '/TESTsubdir1/demo.png'); @rmdir($mediaDir . '/TESTsubdir1/foobar/thumbnail'); @rmdir($mediaDir . '/TESTsubdir1/foobar/'); // @rmdir($mediaDir . '/TESTsubdir1/thumbnail/'); @rmdir($mediaDir . '/TESTsubdir1/'); @rmdir($mediaDir . '/TESTsubdir2/thumbnail/'); @rmdir($mediaDir . '/TESTsubdir2/'); @rmdir($mediaDir . '/TESTsubdir3/'); // cp data files - why are they deleted during tests? :-| copy(dirname(__FILE__) . '/../data/demo1.png', dirname(__FILE__) . '/../data/demo.png'); copy(dirname(__FILE__) . '/../data/propel1.gif', dirname(__FILE__) . '/../data/propel.gif'); copy(dirname(__FILE__) . '/../data/demo1.png', dirname(__FILE__) . '/../data/demo2.png'); copy(dirname(__FILE__) . '/../data/propel1.gif', dirname(__FILE__) . '/../data/propel2.gif'); // load fixtures
<?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');
<?php // guess current application if (!isset($app)) { $app = 'backend'; } require_once dirname(__FILE__) . '/../../../../config/ProjectConfiguration.class.php'; $configuration = ProjectConfiguration::getApplicationConfiguration($app, 'test', isset($debug) ? $debug : true); sfContext::createInstance($configuration); // remove all cache sfToolkit::clearDirectory(sfConfig::get('sf_app_cache_dir')); // clear possible fixture directories $mediaDir = sfAssetsLibraryTools::getMediaDir(); @unlink($mediaDir . '/TESTsubdir1/'); @unlink($mediaDir . '/TESTsubdir2/'); @unlink($mediaDir . '/TESTsubdir3/'); // cp data files - why are they deleted during tests? :-| copy(dirname(__FILE__) . '/../data/demo1.png', dirname(__FILE__) . '/../data/demo.png'); copy(dirname(__FILE__) . '/../data/propel1.gif', dirname(__FILE__) . '/../data/propel.gif'); copy(dirname(__FILE__) . '/../data/demo1.png', dirname(__FILE__) . '/../data/demo2.png'); copy(dirname(__FILE__) . '/../data/propel1.gif', dirname(__FILE__) . '/../data/propel2.gif'); // load fixtures $data = new sfPropelData(); $data->loadData(dirname(__FILE__) . '/../data/fixtures/');
/** * Physically creates asset * * @param string $asset_path path to the asset original file * @param bool $move do move or just copy ? */ public function createAsset($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()->presents()) { $this->getsfAssetFolder()->createFolder(); } else { // check if a file with this name already exists if ($checkDuplicate && sfAssetTable::presents($this->getsfAssetFolder()->getId(), $this->getFilename())) { $this->setFilename(time() . $this->getFilename()); } } $this->setFilesize((int) filesize($asset_path) / 1024); $this->autoSetType(); if (sfConfig::get('app_sfDoctrineAssetsLibrary_check_type', false) && !in_array($this->getType(), sfConfig::get('app_sfDoctrineAssetsLibrary_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()); } }