diff --git a/airtime_mvc/application/modules/rest/Bootstrap.php b/airtime_mvc/application/modules/rest/Bootstrap.php index e7017ba16..31691ca96 100644 --- a/airtime_mvc/application/modules/rest/Bootstrap.php +++ b/airtime_mvc/application/modules/rest/Bootstrap.php @@ -23,5 +23,15 @@ class Rest_Bootstrap extends Zend_Application_Module_Bootstrap ) ); $router->addRoute('download', $downloadRoute); + + $clearLibraryRoute = new Zend_Controller_Router_Route( + 'rest/media/clear', + array( + 'controller' => 'media', + 'action' => 'clear', + 'module' => 'rest' + ) + ); + $router->addRoute('clear', $clearLibraryRoute); } -} \ No newline at end of file +} diff --git a/airtime_mvc/application/modules/rest/controllers/MediaController.php b/airtime_mvc/application/modules/rest/controllers/MediaController.php index ecd538a12..65f30b7e9 100644 --- a/airtime_mvc/application/modules/rest/controllers/MediaController.php +++ b/airtime_mvc/application/modules/rest/controllers/MediaController.php @@ -80,6 +80,42 @@ class Rest_MediaController extends Zend_Rest_Controller $this->fileNotFoundResponse(); } } + + public function clearAction() + { + //TODO:: make this not accessible via public api?? + if (!$this->verifyAuth(true, true)) + { + return; + } + + //set file_exists flag to false for every file + $con = Propel::getConnection(CcFilesPeer::DATABASE_NAME); + $selectCriteria = new Criteria(); + $selectCriteria->add(CcFilesPeer::FILE_EXISTS, true); + $updateCriteria = new Criteria(); + $updateCriteria->add(CcFilesPeer::FILE_EXISTS, false); + BasePeer::doUpdate($selectCriteria, $updateCriteria, $con); + + $path = isset($_SERVER['AIRTIME_BASE']) ? $_SERVER['AIRTIME_BASE']."/srv/airtime/stor/imported/*" : "/srv/airtime/stor/imported/*"; + exec("rm -rf $path"); + + //update disk_usage value in cc_pref + $musicDir = CcMusicDirsQuery::create() + ->filterByType('stor') + ->filterByExists(true) + ->findOne(); + $storPath = $musicDir->getDirectory(); + + $freeSpace = disk_free_space($storPath); + $totalSpace = disk_total_space($storPath); + + Application_Model_Preference::setDiskUsage($totalSpace - $freeSpace); + + $this->getResponse() + ->setHttpResponseCode(200) + ->appendBody("Library has been cleared"); + } public function getAction() { @@ -136,7 +172,21 @@ class Rest_MediaController extends Zend_Rest_Controller $file->save(); return; } else { + /* If full_path is set, the post request came from ftp. + * Users are allowed to upload folders via ftp. If this is the case + * we need to include the folder name with the file name, otherwise + * files won't get removed from the organize folder. + */ + if (isset($whiteList["full_path"])) { + $fullPath = $whiteList["full_path"]; + $basePath = isset($_SERVER['AIRTIME_BASE']) ? $_SERVER['AIRTIME_BASE']."/srv/airtime/stor/organize/" : "/srv/airtime/stor/organize/"; + //$relativePath is the folder name(if one) + track name, that was uploaded via ftp + $relativePath = substr($fullPath, strlen($basePath)-1); + } else { + $relativePath = $_FILES["file"]["name"]; + } + $file->fromArray($whiteList); $file->setDbOwnerId($this->getOwnerId()); $now = new DateTime("now", new DateTimeZone("UTC")); @@ -146,9 +196,9 @@ class Rest_MediaController extends Zend_Rest_Controller $file->save(); $callbackUrl = $this->getRequest()->getScheme() . '://' . $this->getRequest()->getHttpHost() . $this->getRequest()->getRequestUri() . "/" . $file->getPrimaryKey(); - - $this->processUploadedFile($callbackUrl, $_FILES["file"]["name"], $this->getOwnerId()); - + + $this->processUploadedFile($callbackUrl, $relativePath, $this->getOwnerId()); + $this->getResponse() ->setHttpResponseCode(201) ->appendBody(json_encode($this->sanitizeResponse($file))); @@ -201,6 +251,10 @@ class Rest_MediaController extends Zend_Rest_Controller $now = new DateTime("now", new DateTimeZone("UTC")); $file->setDbMtime($now); $file->save(); + + $this->removeEmptySubFolders( + isset($_SERVER['AIRTIME_BASE']) ? $_SERVER['AIRTIME_BASE']."/srv/airtime/stor/organize/" : "/srv/airtime/stor/organize/"); + $this->getResponse() ->setHttpResponseCode(200) ->appendBody(json_encode($this->sanitizeResponse($file))); @@ -365,14 +419,11 @@ class Rest_MediaController extends Zend_Rest_Controller Logging::error($e->getMessage()); return; } - - Logging::info($newTempFilePath); - //Logging::info("Old temp file path: " . $tempFilePath); //Dispatch a message to airtime_analyzer through RabbitMQ, //notifying it that there's a new upload to process! Application_Model_RabbitMq::SendMessageToAnalyzer($newTempFilePath, - $importedStorageDirectory, $originalFilename, + $importedStorageDirectory, basename($originalFilename), $callbackUrl, $apiKey); } @@ -430,5 +481,10 @@ class Rest_MediaController extends Zend_Rest_Controller return $response; } + private function removeEmptySubFolders($path) + { + exec("find $path -empty -type d -delete"); + } + } diff --git a/airtime_mvc/application/modules/rest/views/scripts/media/clear.phtml b/airtime_mvc/application/modules/rest/views/scripts/media/clear.phtml new file mode 100644 index 000000000..e69de29bb diff --git a/python_apps/airtime_analyzer/tools/ftp-upload-hook.sh b/python_apps/airtime_analyzer/tools/ftp-upload-hook.sh index e456d8be9..f5be38183 100755 --- a/python_apps/airtime_analyzer/tools/ftp-upload-hook.sh +++ b/python_apps/airtime_analyzer/tools/ftp-upload-hook.sh @@ -28,7 +28,7 @@ post_file() { api_key=$(awk -F "= " '/api_key/ {print $2}' $instance_conf_path) - until curl --max-time 30 $url -u $api_key":" -X POST -F "file=@${file_path}" -F "name=${filename}" + until curl --max-time 30 $url -u $api_key":" -X POST -F "file=@${file_path}" -F "full_path=${file_path}" do retry_count=$[$retry_count+1] if [ $retry_count -ge $max_retry ]; then