protected function doClean($values) { $my_id = 0; if ($values['parent_id']) { $parent = lyMediaFolderTable::getInstance()->find($values['parent_id']); } else { $my_id = $values['id']; $object = lyMediaFolderTable::getInstance()->find($my_id); $parent = $object->getNode()->getParent(); } $fs = new lyMediaFileSystem(); if (!$fs->is_dir($parent->getRelativePath()) || !$fs->is_writable($parent->getRelativePath())) { throw new sfValidatorError($this, 'parent_unwritable', array('name' => $values['name'], 'parent' => $parent->getName())); } if (!$parent || !$parent->getNode()->isValidNode()) { throw new sfValidatorError($this, 'invalid'); } if ($children = $parent->getNode()->getChildren()) { foreach ($children as $c) { if ($values['name'] == $c->getName() && $c->getId() != $my_id) { throw new sfValidatorError($this, 'folder_exists', array('name' => $values['name'], 'parent' => $parent->getName())); } } } return $values; }
/** * Gets full path of a thumbnail file of a given type. * * @param string $thumb_type thumbnail type. * @param bool $create true = create thumbnail folder if it doesn't exists. * @return string thumbnail file path. */ public function getThumbnailPath($thumb_type, $create = true) { $folder = $this->folder . self::getThumbnailFolder(); if ($create && !file_exists($folder)) { $fs = new lyMediaFileSystem(); $fs->mkdir($folder); } return $folder . DIRECTORY_SEPARATOR . $thumb_type . '_' . $this->file; }
/** * Checks if asset file and thumbnails exist in filesystem. * * @param string $folder asset folder (relative path) * @param string $file asset filename * @param boolean $check_thumbs check existence/non-existence of thumbnail files * @param boolean $exist true=must exist, false=must not exist * * @return lyMediaTestFunctional current lyMediaTestFunctional instance */ protected function checkFile($folder, $file, $check_thumbs = true, $exist = true) { $fs = new lyMediaFileSystem(); $this->test()->is($fs->is_file($folder . $file), $exist, 'File ' . $folder . $file . ($exist ? ' has ' : ' has not ') . 'been found'); if ($check_thumbs) { $tn = new lyMediaThumbnails($folder, $file); foreach ($tn->getThumbnailPaths() as $file_path) { $this->test()->is($fs->is_file($file_path), $exist, 'Thumbnail ' . basename($file_path) . ($exist ? ' has ' : ' has not ') . 'been found'); } } return $this; }
/** * Checks if asset file and asset thumbnails (if supported) exist in filesystem. * * @param lyMediaAsset $asset * @param boolean $exist true=must exist, false=must not exist * * @return lyMediaTestFunctional current lyMediaTestFunctional instance */ protected function checkFile($asset, $exist = true) { $fs = new lyMediaFileSystem(); $file_path = $asset->getPath(); $this->test()->is($fs->is_file($file_path), $exist, 'File ' . $file_path . ($exist ? ' has ' : ' has not ') . 'been found'); if ($asset->supportsThumbnails()) { $tn = new lyMediaThumbnails($file_path, in_array($asset->getType(), array('image/png', 'image/gif')) ? $asset->getType() : 'image/jpeg', $asset->getThumbnailFile(null)); foreach ($tn->getThumbnailPaths() as $file_path) { $this->test()->is($fs->is_file($file_path), $exist, 'Thumbnail ' . basename($file_path) . ($exist ? ' has ' : ' has not ') . 'been found'); } } return $this; }
protected function doClean($values) { $folder = lyMediaFolderTable::getInstance()->find($values['folder_id']); if ($folder === false) { throw new sfValidatorError($this, 'invalid'); } $fs = new lyMediaFileSystem(); if (!$fs->is_dir($folder->getRelativePath()) || !$fs->is_writable($folder->getRelativePath())) { throw new sfValidatorError($this, 'folder_unwritable', array('folder' => $folder->getRelativePath())); } $my_id = empty($values['id']) ? 0 : $values['id']; $assets = $folder->getAssets(); foreach ($assets as $a) { if ($values['filename'] == $a->getFilename() && $a->getId() != $my_id) { throw new sfValidatorError($this, 'file_exists', array('name' => $values['filename'], 'folder' => $folder->getName())); } } return $values; }
/** * preSave. * * @param Doctrine_Event $event */ public function preSave($event) { $record = $event->getInvoker(); if ($record->isNew()) { $file = $record->getFilename(); if ($file != basename($file) && is_file($file)) { $fs = new lyMediaFileSystem(); $dest_path = $record->getFolderPath(); $dest_file = $fs->generateUniqueFileName($dest_path, basename($file)); $fs->import($file, $dest_path . $dest_file); $record->setType(mime_content_type($file)); $record->setFilename($dest_file); } $record->generateThumbnails(); } else { $modified = $record->getModified(true); if (isset($modified['folder_id']) || isset($modified['filename'])) { //Selected new folder or edited filename: move/rename asset $dest_folder = lyMediaFolderTable::getInstance()->find($record->getFolderId()); $src_folder = $dest_folder; if (isset($modified['folder_id'])) { $src_folder = lyMediaFolderTable::getInstance()->find($modified['folder_id']); } $src = $src_folder->getRelativePath() . (isset($modified['filename']) ? $modified['filename'] : $record->getFileName()); $dest = $dest_folder->getRelativePath() . $record->getFileName(); $fs = new lyMediaFileSystem(); $fs->rename($src, $dest, $record->supportsThumbnails()); } } }
<?php include dirname(__FILE__) . '/../bootstrap/unit.php'; $conn = lyMediaFolderTable::getInstance()->getConnection(); $root = lyMediaFolderTable::getInstance()->createRoot('test_root'); $fs = new lyMediaFileSystem(); $t = new lime_test(30, new lime_output_color()); $t->info('Create first level folder'); $folder = new lyMediaFolder(); $folder->setName('test'); $folder->create($root); $folder->refresh(); /* * test_root * -- test */ $t->is($folder->getName(), 'test', '->getName()'); $t->is($folder->getRelativePath(), 'test_root/test/', '->getRelativePath()'); $t->ok($folder->getNode()->isValidNode(), 'Folder is a valid node'); $t->is($folder->getNode()->getPath('/', true), 'test_root/test', 'Folder has right path'); $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 */
<?php include dirname(__FILE__) . '/../bootstrap/unit.php'; $conn = lyMediaFolderTable::getInstance()->getConnection(); $root = lyMediaFolderTable::getInstance()->createRoot('test_root'); $fs = new lyMediaFileSystem(); $t = new lime_test(15, new lime_output_color()); $t->info('Create first level folder'); $folder = new lyMediaFolder(); $folder->setName('test'); $folder->create($root); $folder->refresh(); $t->is($folder->getName(), 'test', '->getName()'); $t->is($folder->getRelativePath(), 'test_root/test/', '->getRelativePath()'); $t->ok($folder->getNode()->isValidNode(), 'Folder is a valid node'); $t->is($folder->getNode()->getPath('/', true), 'test_root/test', 'Folder has right path'); $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(); $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(); $t->info('Move 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'); } } } }
<?php include dirname(__FILE__) . '/../bootstrap/unit.php'; $fs = new lyMediaFileSystem(); $t = new lime_test(5, new lime_output_color()); $t->info('->createRoot()'); $root = lyMediaFolderTable::getInstance()->createRoot('test_root'); $t->is($root->getRelativePath(), 'test_root/', '->getRelativePath()'); $t->ok($root->getNode()->isValidNode(), 'Folder is valid node'); $t->ok($root->getNode()->isRoot(), 'Folder is root'); $t->ok($fs->is_dir($root->getRelativePath()), 'Root dir created in filesystem'); $t->info('->getRoot()'); $t->is_deeply($root->toArray(), lyMediaFolderTable::getInstance()->getRoot()->toArray(), '->getRoot()');
/** * Downloads an asset file * * @param sfWebRequest $request */ public function executeDownload(sfWebRequest $request) { $asset = $this->getRoute()->getObject(); $fs = new lyMediaFileSystem(); $this->file = $fs->makePathAbsolute($asset->getPath()); $response = $this->getResponse(); $response->setHttpHeader('Content-Description', 'File Transfer'); $response->setHttpHeader('Content-disposition', 'attachment; filename=' . $asset->getFilename()); $response->setHttpHeader('Content-type', $asset->getType()); $response->setHttpHeader('Content-Transfer-Encoding', 'binary'); $response->setHttpHeader('Expires', 0); $response->setHttpHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0'); $response->setHttpHeader('Content-Length', filesize($this->file)); $this->setLayout(false); }