public function createRoot($root_name) { $folder = new lyMediaFolder(); $folder->setName($root_name); $folder->save(); $this->getTree()->createRoot($folder); return $folder; }
/** * Used by synchronize task. * * @param string $baseFolder * @param bool $verbose * @param bool $removeOrphanAssets * @param bool $removeOrphanFolders */ public function synchronizeWith($baseFolder, $verbose = true, $removeOrphanAssets = false, $removeOrphanFolders = false) { if (!is_dir($baseFolder)) { throw new lyMediaException(sprintf('%s is not a directory', $baseFolder)); } $files = sfFinder::type('file')->maxdepth(0)->ignore_version_control()->in($baseFolder); $assets = $this->getAssetsWithFilenames(); $fs = new lyMediaFileSystem(); foreach ($files as $file) { $basename = basename($file); if (!array_key_exists($basename, $assets)) { // File exists, asset does not exist: create asset $fs->import($file, $this->getRelativePath() . $basename); $lyMediaAsset = new lyMediaAsset(); $lyMediaAsset->setFolderId($this->getId()); $lyMediaAsset->setFilename($basename); $lyMediaAsset->setType(mime_content_type($file)); $lyMediaAsset->setFilesize(filesize($file) / 1024); $lyMediaAsset->save(); if ($verbose) { lyMediaTools::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) { lyMediaTools::log(sprintf("Deleting asset %s", $asset->getPath()), 'yellow'); } } else { if ($verbose) { lyMediaTools::log(sprintf("Warning: No file for asset %s", $asset->getPath()), 'red'); } } } $dirs = sfFinder::type('dir')->maxdepth(0)->discard(lyMediaTools::getThumbnailFolder())->ignore_version_control()->in($baseFolder); $folders = $this->getSubfoldersWithFolderNames(); foreach ($dirs as $dir) { list(, $name) = lyMediaTools::splitPath($dir); if (!array_key_exists($name, $folders)) { // dir exists in filesystem, not in database: create folder in database $lyMediaFolder = new lyMediaFolder(); $lyMediaFolder->setName($name); $lyMediaFolder->create($this); if ($verbose) { lyMediaTools::log(sprintf("Importing directory %s", $dir), 'green'); } } else { // dir exists in filesystem and database: look inside $lyMediaFolder = $folders[$name]; unset($folders[$name]); } $lyMediaFolder->synchronizeWith($dir, $verbose, $removeOrphanAssets, $removeOrphanFolders); } foreach ($folders as $name => $folder) { if ($removeOrphanFolders) { $folder->delete(null, true); if ($verbose) { lyMediaTools::log(sprintf("Deleting folder %s", $folder->getRelativePath()), 'yellow'); } } else { if ($verbose) { lyMediaTools::log(sprintf("Warning: No directory for folder %s", $folder->getRelativePath()), 'red'); } } } }
$t->ok($fs->is_dir($folder->getRelativePath()), 'Folder exists in filesystem'); $t->info('Create sub-folder'); $sub = new lyMediaFolder(); $sub->setName('test-sub'); $sub->create($folder); $sub->refresh(); /* * test_root * -- test * -- -- test-sub */ $t->is($sub->getRelativePath(), 'test_root/test/test-sub/', '->getRelativePath()'); $t->ok($sub->getNode()->isValidNode(), 'Folder is a valid node'); $t->is($sub->getNode()->getPath('/', true), 'test_root/test/test-sub', 'Subfolder has right path'); $t->ok($fs->is_dir($sub->getRelativePath()), 'Folder exists in filesystem'); $folder2 = new lyMediaFolder(); $folder2->setName('test2'); $folder2->create($root); $folder2->refresh(); /* * test_root * -- test * -- -- test-sub * -- test 2 */ $t->info('Move folder'); $folder2->move($folder); $folder2->refresh(); /* * test_root * -- test
<?php include dirname(__FILE__) . '/../bootstrap/unit.php'; //Create folders for tests $root = lyMediaFolderTable::getInstance()->createRoot('test_root'); $folder = new lyMediaFolder(); $folder->setName('test'); $folder->create($root); $base = sfConfig::get('sf_web_dir') . DIRECTORY_SEPARATOR . 'test_root' . DIRECTORY_SEPARATOR; $t = new lime_test(31, new lime_output_color()); $t->info('Create asset'); $file = dirname(__FILE__) . '/../data/assets/asset1.png'; $a = new lyMediaAsset(); $a->setFolder($root); $a->setFilename($file); $a->save(); $a->refresh(); $t->is($a->getFilename(), 'asset1.png', '->getFilename()'); $t->is($a->getType(), 'image/png', '->getType()'); $t->is($a->getPath(), 'test_root/asset1.png', '->getPath()'); $t->ok(file_exists($base . 'asset1.png'), 'File exists'); $t->ok(file_exists($base . 'thumbs/small_asset1.png'), 'Small thumbnail exists'); $t->ok(file_exists($base . 'thumbs/medium_asset1.png'), 'Medium thumbnail exists'); $t->info('Create another asset with the same file'); $a2 = new lyMediaAsset(); $a2->setFolder($root); $a2->setFilename($file); $a2->save(); $a2->refresh(); $t->is($a2->getFilename(), 'asset1(1).png', 'Filename is unique'); $t->info('Rename asset: asset1.png > renamed.png');