CC-2977: Never delete files from the database

- using mtab(previous and current) files to figure out directories to be
added or removed
- adding index to cc_files
- see the tickets and media monitor design page for more info
This commit is contained in:
James 2012-01-12 17:55:05 -05:00
parent d5d4d50a9e
commit 08a09e4096
21 changed files with 297 additions and 220 deletions

View file

@ -45,20 +45,30 @@ class Application_Model_MusicDir {
$this->_dir->save();
}
public function setRemovedFlag($flag){
$this->_dir->setRemoved($flag);
public function setWatchedFlag($flag){
$this->_dir->setWatched($flag);
$this->_dir->save();
}
public function getRemovedFlag(){
return $this->_dir->getRemoved();
public function getWatchedFlag(){
return $this->_dir->getWatched();
}
public function getExistsFlag(){
return $this->_dir->getExists();
}
public function remove($setRemovedFlag=true)
/** There are 2 cases where this function can be called.
* 1. When watched dir was removed
* 2. When some dir was watched, but it was unmounted
*
* In case of 1, $userAddedWatchedDir should be true
* In case of 2, $userAddedWatchedDir should be false
*
* When $userAddedWatchedDir is true, it will set "Watched" flag to false
* otherwise, it will set "Exists" flag to true
**/
public function remove($userAddedWatchedDir=true)
{
global $CC_DBC;
@ -77,13 +87,13 @@ class Application_Model_MusicDir {
foreach( $files as $file_row ){
$temp_file = Application_Model_StoredFile::Recall($file_row['id']);
if($temp_file != null){
$temp_file->setFileExistFlag(false);
$temp_file->setFileExistsFlag(false);
}
}
// set RemovedFlag to true
if($setRemovedFlag){
self::setRemovedFlag(true);
if($userAddedWatchedDir){
self::setWatchedFlag(false);
}else{
self::setExistsFlag(false);
}
@ -148,7 +158,19 @@ class Application_Model_MusicDir {
}
}
public static function addDir($p_path, $p_type, $setRemovedFlag=true)
/** There are 2 cases where this function can be called.
* 1. When watched dir was added
* 2. When some dir was watched, but it was unmounted somehow, but gets mounted again
*
* In case of 1, $userAddedWatchedDir should be true
* In case of 2, $userAddedWatchedDir should be false
*
* When $userAddedWatchedDir is true, it will set "Removed" flag to false
* otherwise, it will set "Exists" flag to true
*
* @param $nestedWatch - if true, bypass path check, and Watched to false
**/
public static function addDir($p_path, $p_type, $userAddedWatchedDir=true, $nestedWatch=false)
{
if(!is_dir($p_path)){
return array("code"=>2, "error"=>"'$p_path' is not a valid directory.");
@ -174,10 +196,15 @@ class Application_Model_MusicDir {
try {
/* isPathValid() checks if path is a substring or a superstring of an
* existing dir and if not, throws NestedDirectoryException */
self::isPathValid($p_path);
if($setRemovedFlag){
$dir->setRemovedFlag(false);
if(!$nestedWatch){
self::isPathValid($p_path);
}
if($userAddedWatchedDir){
$dir->setWatchedFlag(true);
}else{
if($nestedWatch){
$dir->setWatchedFlag(false);
}
$dir->setExistsFlag(true);
}
$dir->setDirectory($p_path);
@ -196,15 +223,15 @@ class Application_Model_MusicDir {
* 1. When watched dir was added
* 2. When some dir was watched, but it was unmounted somehow, but gets mounted again
*
* In case of 1, $setRemovedFlag should be true
* In case of 2, $setRemovedFlag should be false
* In case of 1, $userAddedWatchedDir should be true
* In case of 2, $userAddedWatchedDir should be false
*
* When $setRemovedFlag is true, it will set "Removed" flag to false
* When $userAddedWatchedDir is true, it will set "Watched" flag to true
* otherwise, it will set "Exists" flag to true
**/
public static function addWatchedDir($p_path, $setRemovedFlag=true)
public static function addWatchedDir($p_path, $userAddedWatchedDir=true, $nestedWatch=false)
{
$res = self::addDir($p_path, "watched", $setRemovedFlag);
$res = self::addDir($p_path, "watched", $userAddedWatchedDir, $nestedWatch);
if ($res['code'] == 0){
@ -266,7 +293,6 @@ class Application_Model_MusicDir {
$dir = CcMusicDirsQuery::create()
->filterByDirectory($p_path)
->findOne();
if($dir == NULL){
return null;
}
@ -280,9 +306,9 @@ class Application_Model_MusicDir {
* Search and returns watched dirs
*
* @param $exists search condition with exists flag
* @param $removed search condition with removed flag
* @param $watched search condition with watched flag
*/
public static function getWatchedDirs($exists=true, $removed=false)
public static function getWatchedDirs($exists=true, $watched=true)
{
$result = array();
@ -291,8 +317,8 @@ class Application_Model_MusicDir {
if($exists !== null){
$dirs = $dirs->filterByExists($exists);
}
if($removed !== null){
$dirs = $dirs->filterByRemoved($removed);
if($watched !== null){
$dirs = $dirs->filterByWatched($watched);
}
$dirs = $dirs->find();
@ -359,23 +385,22 @@ class Application_Model_MusicDir {
* 1. When watched dir was removed
* 2. When some dir was watched, but it was unmounted
*
* In case of 1, $setRemovedFlag should be true
* In case of 2, $setRemovedFlag should be false
* In case of 1, $userAddedWatchedDir should be true
* In case of 2, $userAddedWatchedDir should be false
*
* When $setRemovedFlag is true, it will set "Removed" flag to false
* When $userAddedWatchedDir is true, it will set "Watched" flag to false
* otherwise, it will set "Exists" flag to true
**/
public static function removeWatchedDir($p_dir, $setRemovedFlag=true){
public static function removeWatchedDir($p_dir, $userAddedWatchedDir=true){
$real_path = realpath($p_dir)."/";
if($real_path != "/"){
$p_dir = $real_path;
}
$dir = Application_Model_MusicDir::getDirByPath($p_dir);
if($dir == NULL){
return array("code"=>1,"error"=>"'$p_dir' doesn't exist in the watched list.");
}else{
$dir->remove($setRemovedFlag);
$dir->remove($userAddedWatchedDir);
$data = array();
$data["directory"] = $p_dir;
Application_Model_RabbitMq::SendMessageToMediaMonitor("remove_watch", $data);