public function save() { $values = $this->getValues(); // TODO : find a better way to populate values. foreach ($values['sfAsset'] as $key => $value) { $sf_asset = new sfAsset(); $sf_asset->fromArray($value); $sf_asset->setBinaryContent($value['binary_content']); $sf_asset->save(); } }
/** * save all assets * @param PropelPDO $con */ protected function doSave($con = null) { if (null === $con) { $con = $this->getConnection(); } for ($i = 1; $i <= $this->getOption('size'); $i++) { if ($file = $this->getValue('file_' . $i)) { $asset = new sfAsset(); $asset->setFolderId($this->getValue('folder_id')); $asset->setDescription($file->getOriginalName()); $asset->setAuthor($this->getOption('author')); $asset->setFilename($file->getOriginalName()); $asset->create($file->getTempName()); $asset->save(); $this->assets[] = $asset; } } }
/** * 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'); } } } }
$t->is($sfAssetFolder3->getRelativePath(), $sfAssetFolder2->getRelativePath() . '/' . $sfAssetFolder3->getName(), 'getRelativePath() returns the folder relative path, including its ancestors names'); $id3 = $sfAssetFolder3->getId(); # $sfAsset is /root/Test_Directory/Test_Sub-directory/raikkonen.jpg $assets_path = dirname(__FILE__) . '/../assets/'; $test_asset = $assets_path . 'raikkonen.jpg'; $sfAsset = new sfAsset(); $sfAsset->setFolder($sfAssetFolder2); $sfAsset->create($test_asset, false); $sfAsset->save(); $sf_asset_id = $sfAsset->getId(); # $sfAsset2 is /root/Test_Directory/Test_Sub-directory/Test_Sub-sub-directory/toto $sfAsset2 = new sfAsset(); $sfAsset2->setFolder($sfAssetFolder3); $sfAsset2->setFilename('toto'); $sfAsset2->create($test_asset, false); $sfAsset2->save(); $sf_asset2_id = $sfAsset2->getId(); # So now we have: # root/ # Test_Directory/ sfAssetFolder # Test_Sub-directory/ sfAssetFolder2 # Test_Sub-sub-directory/ sfAssetFolder3 # toto # raikkonen.jpg $t->diag('sfAssetFolder::move()'); # move $sfAssetFolder2 from /root/Test_Directory/Test_Sub-directory to /root/Test_Sub-directory $sfAssetFolder2->move($root); $sfAssetFolder2->save(); # So now we have: # root/ # Test_Directory/ sfAssetFolder
<?php $app = "frontend"; include dirname(__FILE__) . '/../../../../test/bootstrap/functional.php'; $browser = new sfTestBrowser(); $browser->initialize(); $con = Propel::getConnection(); $con->begin(); try { $t = new lime_test(5, new lime_output_color()); $t->diag('sfAssetPeer'); $con->begin(); $t->is(sfAssetPeer::retrieveFromUrl(sfAssetFolderPeer::getRoot()->getRelativePath() . '/filename.jpg'), null, 'sfAssetPeer::retrieveFromUrl() returns null when a URL is not found'); $t->is(sfAssetPeer::exists(sfAssetFolderPeer::getRoot()->getId(), 'filename.jpg'), false, 'sfAssetPeer::exists() returns false when an asset is not found'); $sfAsset = new sfAsset(); $sfAsset->setsfAssetFolder(sfAssetFolderPeer::getRoot()); $sfAsset->setFilename('filename.jpg'); $sfAsset->save($con); $t->is(sfAssetPeer::retrieveFromUrl(sfAssetFolderPeer::getRoot()->getRelativePath() . '/filename.jpg')->getId(), $sfAsset->getId(), 'sfAssetPeer::retrieveFromUrl() finds an asset from its URL'); $t->is(sfAssetPeer::retrieveFromUrl($sfAsset->getUrl())->getId(), $sfAsset->getId(), 'sfAssetPeer::retrieveFromUrl() finds an asset from the result of `getUrl()`'); $t->is(sfAssetPeer::exists(sfAssetFolderPeer::getRoot()->getId(), 'filename.jpg'), true, 'sfAssetPeer::exists() returns true when an asset is found'); } catch (Exception $e) { echo $e->getMessage(); } // reset DB $con->rollback();
include dirname(__FILE__) . '/../../../../test/bootstrap/functional.php'; include $configuration->getSymfonyLibDir() . '/vendor/lime/lime.php'; $databaseManager = new sfDatabaseManager($configuration); $con = Doctrine::getConnectionByTableName('sfAsset'); $con->beginTransaction(); try { // prepare test environment sfAssetFolderTable::getInstance()->createQuery()->delete()->execute(); sfAssetTable::getInstance()->createQuery()->delete()->execute(); sfConfig::set('app_sfAssetsLibrary_upload_dir', 'mediaTEST'); $f = new sfAssetFolder(); $f->setName(sfConfig::get('app_sfAssetsLibrary_upload_dir')); $tree = sfAssetFolderTable::getInstance()->getTree(); $tree->createRoot($f); $f->save(); $t = new lime_test(5, array('options' => new lime_output_color(), 'error_reporting' => true)); $t->diag('sfAssetPeer'); $t->is(sfAssetTable::getInstance()->retrieveFromUrl(sfAssetFolderTable::getInstance()->getRoot()->getRelativePath() . '/filename.jpg'), null, 'sfAssetPeer::retrieveFromUrl() returns null when a URL is not found'); $t->is(sfAssetTable::getInstance()->exists(sfAssetFolderTable::getInstance()->getRoot()->getId(), 'filename.jpg'), false, 'sfAssetPeer::exists() returns false when an asset is not found'); $sfAsset = new sfAsset(); $sfAsset->setFolder(sfAssetFolderTable::getInstance()->getRoot()); $sfAsset->setFilename('filename.jpg'); $sfAsset->save(); $t->is(sfAssetTable::getInstance()->retrieveFromUrl(sfAssetFolderTable::getInstance()->getRoot()->getRelativePath() . '/filename.jpg')->getId(), $sfAsset->getId(), 'sfAssetPeer::retrieveFromUrl() finds an asset from its URL'); $t->is(sfAssetTable::getInstance()->retrieveFromUrl($sfAsset->getUrl())->getId(), $sfAsset->getId(), 'sfAssetPeer::retrieveFromUrl() finds an asset from the result of `getUrl()`'); $t->is(sfAssetTable::getInstance()->exists(sfAssetFolderTable::getInstance()->getRoot()->getId(), 'filename.jpg'), true, 'sfAssetPeer::exists() returns true when an asset is found'); } catch (Exception $e) { echo $e->getMessage(); } // reset DB $con->rollBack();
public function executeMassUpload() { if ($this->getRequest()->getMethod() == sfRequest::POST) { $folder = sfAssetFolderPeer::retrieveByPath($this->getRequestParameter('parent_folder')); $this->forward404Unless($folder); try { $nbFiles = 0; for ($i = 1; $i <= sfConfig::get('app_sfAssetsLibrary_mass_upload_size', 5); $i++) { if ($filename = $this->getRequest()->getFileName('files[' . $i . ']')) { $asset = new sfAsset(); $asset->setsfAssetFolder($folder); $asset->setDescription($filename); try { $asset->setAuthor($this->getUser()->getUsername()); } catch (sfException $e) { // no getUsername() method in sfUser, all right: do nothing } $asset->setFilename($filename); $asset->create($this->getRequest()->getFilePath('files[' . $i . ']')); $asset->save(); $nbFiles++; } } } catch (sfAssetException $e) { $this->setFlash('warning_message', $e->getMessage()); $this->setFlash('warning_params', $e->getMessageParams()); $this->redirectToPath('sfAsset/list?dir=' . $folder->getRelativePath()); } $this->setFlash('notice', 'Files successfully uploaded'); $this->redirectToPath('sfAsset/list?dir=' . $folder->getRelativePath()); } }
/** * upload many assets * @param sfWebRequest $request */ public function executeMassUpload(sfWebRequest $request) { $this->form = new sfAssetsForm(null, array('size' => sfConfig::get('app_sfAssetsLibrary_mass_upload_size', 5))); if ($request->getMethod() == sfRequest::POST) { $this->form->bind($request->getParameter($this->form->getName()), $request->getFiles($this->form->getName())); if ($this->form->isValid()) { $folder = sfAssetFolderPeer::retrieveByPK($this->form->getValue('folder_id')); $this->forward404Unless($folder, 'folder not found'); try { $nbFiles = 0; for ($i = 1; $i <= sfConfig::get('app_sfAssetsLibrary_mass_upload_size', 5); $i++) { if ($file = $this->form->getValue('file_' . $i)) { $asset = new sfAsset(); $asset->setsfAssetFolder($folder); $asset->setDescription($file->getOriginalName()); try { $asset->setAuthor($this->getUser()->getUsername()); } catch (sfException $e) { // no getUsername() method in sfUser, all right: do nothing } $asset->setFilename($file->getOriginalName()); $asset->create($file->getTempName()); $asset->save(); $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $asset))); $nbFiles++; } } } catch (sfAssetException $e) { $this->getUser()->setFlash('warning_message', $e->getMessage()); $this->getUser()->setFlash('warning_params', $e->getMessageParams()); $this->redirectToPath('@sf_asset_library_dir?dir=' . $folder->getRelativePath()); } $this->getUser()->setFlash('notice', 'Files successfully uploaded'); $this->redirectToPath('@sf_asset_library_dir?dir=' . $folder->getRelativePath()); } } }
$t->ok(is_file($test_asset), 'test asset found'); $sfAsset = new sfAsset(); $sfAsset->setsfAssetFolder(sfAssetFolderTable::getRoot()); $sfAsset->createAsset($test_asset, false); $t->is($sfAsset->getFilename(), 'raikkonen.jpg', 'create() gives correct filename'); $t->is((int) $sfAsset->getFilesize(), 18, 'create() gives correct size'); $t->ok($sfAsset->isImage(), 'create() gives correct type'); $t->ok(is_file($sfAsset->getFullPath()), 'create() physically copies asset'); if ($sfAsset->supportsThumbnails()) { $t->ok(is_file($sfAsset->getFullPath('large')), 'create() physically creates thumbnail'); } else { $t->diag('please activate thumbnails support'); } $old_path = $sfAsset->getFullPath(); $old_thumb_path = $sfAsset->getFullPath('large'); $sfAsset->save($doctrine_conn); $sfAsset->setFilename('raikkonen2.jpg'); $sfAsset->save($doctrine_conn); $t->is($sfAsset->getFilename(), 'raikkonen2.jpg', 'move() changes filename'); $t->ok(is_file($sfAsset->getFullPath()), 'move() physically moves asset'); $t->ok(!is_file($old_path), 'move() physically moves asset'); if ($sfAsset->supportsThumbnails()) { $t->ok(is_file($sfAsset->getFullPath('large')), 'move() physically moves thumbnail'); $t->ok(!is_file($old_thumb_path), 'move() physically moves thumbnail'); } else { $t->diag('please activate thumbnails support'); } $old_path = $sfAsset->getFullPath(); $old_thumb_path = $sfAsset->getFullPath('large'); $old_id = $sfAsset->getId(); $sfAsset->delete();