2010-12-07 20:19:27 +01:00
< ? php
2012-02-24 15:07:04 +01:00
require_once 'formatters/LengthFormatter.php' ;
2012-02-24 18:22:07 +01:00
require_once 'formatters/SamplerateFormatter.php' ;
require_once 'formatters/BitrateFormatter.php' ;
2012-02-24 15:07:04 +01:00
2010-12-07 20:19:27 +01:00
/**
2011-09-22 18:24:17 +02:00
* Application_Model_StoredFile class
2010-12-07 20:19:27 +01:00
*
2011-01-05 18:19:58 +01:00
* @ package Airtime
2010-12-07 20:19:27 +01:00
* @ subpackage StorageServer
* @ copyright 2010 Sourcefabric O . P . S .
* @ license http :// www . gnu . org / licenses / gpl . txt
* @ see MetaData
*/
2011-09-22 18:24:17 +02:00
class Application_Model_StoredFile {
2010-12-07 20:19:27 +01:00
/**
2011-06-08 10:15:35 +02:00
* @ holds propel database object
*/
private $_file ;
2010-12-07 20:19:27 +01:00
/**
2011-06-08 10:15:35 +02:00
* array of db metadata -> propel
2010-12-07 20:19:27 +01:00
*/
2011-06-08 10:15:35 +02:00
private $_dbMD = array (
" track_title " => " DbTrackTitle " ,
" artist_name " => " DbArtistName " ,
" album_title " => " DbAlbumTitle " ,
" genre " => " DbGenre " ,
" mood " => " DbMood " ,
" track_number " => " DbTrackNumber " ,
" bpm " => " DbBpm " ,
" label " => " DbLabel " ,
" composer " => " DbComposer " ,
" encoded_by " => " DbEncodedBy " ,
" conductor " => " DbConductor " ,
" year " => " DbYear " ,
" info_url " => " DbInfoUrl " ,
" isrc_number " => " DbIsrcNumber " ,
" copyright " => " DbCopyright " ,
" length " => " DbLength " ,
" bit_rate " => " DbBitRate " ,
" sample_rate " => " DbSampleRate " ,
" mime " => " DbMime " ,
2011-06-14 18:52:43 +02:00
" md5 " => " DbMd5 " ,
2011-08-03 18:32:59 +02:00
" ftype " => " DbFtype " ,
2012-05-01 18:39:56 +02:00
" language " => " DbLanguage " ,
2012-07-06 04:37:40 +02:00
" replay_gain " => " DbReplayGain " ,
2012-05-01 18:39:56 +02:00
" directory " => " DbDirectory "
2011-06-08 10:15:35 +02:00
);
2011-06-09 11:50:03 +02:00
public function __construct ()
2010-12-07 20:19:27 +01:00
{
2011-06-08 10:15:35 +02:00
2010-12-07 20:19:27 +01:00
}
2011-06-08 11:59:48 +02:00
public function getId ()
{
2011-06-08 10:15:35 +02:00
return $this -> _file -> getDbId ();
}
2010-12-07 20:19:27 +01:00
2011-06-09 11:50:03 +02:00
public function getGunId () {
return $this -> _file -> getDbGunid ();
}
2011-06-08 11:59:48 +02:00
public function getFormat ()
{
return $this -> _file -> getDbFtype ();
}
2011-06-20 20:10:33 +02:00
public function getPropelOrm (){
return $this -> _file ;
}
2011-06-08 11:59:48 +02:00
public function setFormat ( $p_format )
{
$this -> _file -> setDbFtype ( $p_format );
}
2010-12-07 20:19:27 +01:00
/**
2011-06-08 10:15:35 +02:00
* Set multiple metadata values using defined metadata constants .
2010-12-07 20:19:27 +01:00
*
2011-06-08 10:15:35 +02:00
* @ param array $p_md
* example : $p_md [ 'MDATA_KEY_URL' ] = 'http://www.fake.com'
2010-12-07 20:19:27 +01:00
*/
2012-02-28 19:47:57 +01:00
public function setMetadata ( $p_md = null )
2010-12-07 20:19:27 +01:00
{
2011-06-08 10:15:35 +02:00
if ( is_null ( $p_md )) {
$this -> setDbColMetadata ();
}
else {
$dbMd = array ();
2012-06-04 17:43:41 +02:00
if ( isset ( $p_md [ " MDATA_KEY_YEAR " ])){
// We need to make sure to clean this value before inserting into database.
// If value is outside of range [-2^31, 2^31-1] then postgresl will throw error
// when trying to retrieve this value. We could make sure number is within these bounds,
// but simplest is to do substring to 4 digits (both values are garbage, but at least our
// new garbage value won't cause errors). If the value is 2012-01-01, then substring to
2012-07-06 04:37:40 +02:00
// first 4 digits is an OK result.
2012-06-04 17:43:41 +02:00
// CC-3771
$year = $p_md [ " MDATA_KEY_YEAR " ];
if ( strlen ( $year ) > 4 ){
$year = substr ( $year , 0 , 4 );
}
if ( ! is_numeric ( $year )){
$year = 0 ;
}
$p_md [ " MDATA_KEY_YEAR " ] = $year ;
}
2011-06-08 10:15:35 +02:00
foreach ( $p_md as $mdConst => $mdValue ) {
2012-05-15 21:17:19 +02:00
if ( defined ( $mdConst )){
$dbMd [ constant ( $mdConst )] = $mdValue ;
}
2011-06-08 10:15:35 +02:00
}
$this -> setDbColMetadata ( $dbMd );
2010-12-07 20:19:27 +01:00
}
2012-01-17 11:18:17 +01:00
2012-01-10 20:45:43 +01:00
$this -> _file -> setDbMtime ( new DateTime ( " now " ), new DateTimeZone ( " UTC " ));
$this -> _file -> save ();
2010-12-07 20:19:27 +01:00
}
2011-06-08 10:15:35 +02:00
/**
* Set multiple metadata values using database columns as indexes .
*
* @ param array $p_md
* example : $p_md [ 'url' ] = 'http://www.fake.com'
*/
2012-02-28 19:47:57 +01:00
public function setDbColMetadata ( $p_md = null )
2010-12-07 20:19:27 +01:00
{
2011-06-08 10:15:35 +02:00
if ( is_null ( $p_md )) {
foreach ( $this -> _dbMD as $dbColumn => $propelColumn ) {
$method = " set $propelColumn " ;
$this -> _file -> $method ( null );
}
}
else {
foreach ( $p_md as $dbColumn => $mdValue ) {
2011-06-15 09:19:41 +02:00
//don't blank out name, defaults to original filename on first insertion to database.
2011-06-15 11:12:57 +02:00
if ( $dbColumn == " track_title " && ( is_null ( $mdValue ) || $mdValue == " " )) {
2011-06-15 09:19:41 +02:00
continue ;
}
2011-06-21 10:24:02 +02:00
if ( isset ( $this -> _dbMD [ $dbColumn ])) {
$propelColumn = $this -> _dbMD [ $dbColumn ];
$method = " set $propelColumn " ;
2012-07-06 04:37:40 +02:00
Logging :: log ( $method );
2011-06-21 10:24:02 +02:00
$this -> _file -> $method ( $mdValue );
}
2011-06-08 10:15:35 +02:00
}
2010-12-07 20:19:27 +01:00
}
2011-06-08 10:15:35 +02:00
2012-01-10 20:45:43 +01:00
$this -> _file -> setDbMtime ( new DateTime ( " now " ), new DateTimeZone ( " UTC " ));
2011-06-08 10:15:35 +02:00
$this -> _file -> save ();
2010-12-07 20:19:27 +01:00
}
/**
* Set metadata element value
*
* @ param string $category
2011-06-08 10:15:35 +02:00
* Metadata element by metadata constant
2010-12-07 20:19:27 +01:00
* @ param string $value
* value to store , if NULL then delete record
*/
public function setMetadataValue ( $p_category , $p_value )
{
2011-07-15 23:35:16 +02:00
// constant() was used because it gets quoted constant name value from
// api_client.py. This is the wrapper funtion
2011-06-08 10:15:35 +02:00
$this -> setDbColMetadataValue ( constant ( $p_category ), $p_value );
2010-12-07 20:19:27 +01:00
}
/**
2011-06-08 10:15:35 +02:00
* Set metadata element value
2010-12-07 20:19:27 +01:00
*
2011-06-08 10:15:35 +02:00
* @ param string $category
* Metadata element by db column
* @ param string $value
* value to store , if NULL then delete record
2010-12-07 20:19:27 +01:00
*/
2011-06-08 10:15:35 +02:00
public function setDbColMetadataValue ( $p_category , $p_value )
2010-12-07 20:19:27 +01:00
{
2011-06-15 09:19:41 +02:00
//don't blank out name, defaults to original filename on first insertion to database.
if ( $p_category == " track_title " && ( is_null ( $p_value ) || $p_value == " " )) {
return ;
}
2011-07-15 23:35:16 +02:00
if ( isset ( $this -> _dbMD [ $p_category ])) {
2011-06-21 10:24:02 +02:00
$propelColumn = $this -> _dbMD [ $p_category ];
$method = " set $propelColumn " ;
$this -> _file -> $method ( $p_value );
$this -> _file -> save ();
}
2010-12-07 20:19:27 +01:00
}
/**
2011-06-08 10:15:35 +02:00
* Get one metadata value .
2010-12-07 20:19:27 +01:00
*
2011-06-08 10:15:35 +02:00
* @ param string $p_category ( MDATA_KEY_URL )
* @ return string
2010-12-07 20:19:27 +01:00
*/
2011-06-08 10:15:35 +02:00
public function getMetadataValue ( $p_category )
2010-12-07 20:19:27 +01:00
{
2011-07-15 23:35:16 +02:00
// constant() was used because it gets quoted constant name value from
// api_client.py. This is the wrapper funtion
return $this -> getDbColMetadataValue ( constant ( $p_category ));
2010-12-07 20:19:27 +01:00
}
2011-06-08 10:15:35 +02:00
/**
2010-12-07 20:19:27 +01:00
* Get one metadata value .
*
2011-06-08 10:15:35 +02:00
* @ param string $p_category ( url )
2010-12-07 20:19:27 +01:00
* @ return string
*/
2011-06-08 10:15:35 +02:00
public function getDbColMetadataValue ( $p_category )
{
$propelColumn = $this -> _dbMD [ $p_category ];
$method = " get $propelColumn " ;
return $this -> _file -> $method ();
}
/**
* Get metadata as array , indexed by the column names in the database .
*
* @ return array
*/
public function getDbColMetadata ()
2010-12-07 20:19:27 +01:00
{
2011-06-08 10:15:35 +02:00
$md = array ();
foreach ( $this -> _dbMD as $dbColumn => $propelColumn ) {
$method = " get $propelColumn " ;
$md [ $dbColumn ] = $this -> _file -> $method ();
2010-12-07 23:29:28 +01:00
}
2011-06-08 10:15:35 +02:00
return $md ;
2010-12-07 20:19:27 +01:00
}
2011-06-15 14:20:14 +02:00
/**
* Get metadata as array , indexed by the constant names .
*
* @ return array
*/
public function getMetadata ()
{
$c = get_defined_constants ( true );
$md = array ();
2012-05-04 18:02:42 +02:00
/* Create a copy of dbMD here and create a " filepath " key inside of
* it . The reason we do this here , instead of creating this key inside
* dbMD is because " filepath " isn 't really metadata, and we don' t want
* filepath updated everytime the metadata changes . Also it needs extra
* processing before we can write it to the database ( needs to be split
* into base and relative path )
* */
$dbmd_copy = $this -> _dbMD ;
$dbmd_copy [ " filepath " ] = " DbFilepath " ;
2011-06-15 14:20:14 +02:00
foreach ( $c [ 'user' ] as $constant => $value ) {
if ( preg_match ( '/^MDATA_KEY/' , $constant )) {
2012-05-04 18:02:42 +02:00
if ( isset ( $dbmd_copy [ $value ])) {
2012-05-04 18:47:29 +02:00
$propelColumn = $dbmd_copy [ $value ];
$method = " get $propelColumn " ;
$md [ $constant ] = $this -> _file -> $method ();
2011-06-15 14:20:14 +02:00
}
}
}
return $md ;
}
2010-12-07 20:19:27 +01:00
/**
* Set state of virtual file
*
* @ param string $p_state
* 'empty' | 'incomplete' | 'ready' | 'edited'
* @ param int $p_editedby
* user id | 'NULL' for clear editedBy field
2012-04-05 08:41:51 +02:00
* @ return TRUE
2010-12-07 20:19:27 +01:00
*/
public function setState ( $p_state , $p_editedby = NULL )
{
2012-04-01 21:51:03 +02:00
global $CC_CONFIG ;
$con = Propel :: getConnection ();
2010-12-07 20:19:27 +01:00
$escapedState = pg_escape_string ( $p_state );
$eb = ( ! is_null ( $p_editedby ) ? " , editedBy= $p_editedby " : '' );
$sql = " UPDATE " . $CC_CONFIG [ 'filesTable' ]
2012-04-01 21:51:03 +02:00
. " SET state=' $escapedState ' $eb , mtime=now() "
. " WHERE gunid=' { $this -> gunid } ' " ;
$res = $con -> exec ( $sql );
2010-12-07 20:19:27 +01:00
$this -> state = $p_state ;
$this -> editedby = $p_editedby ;
return TRUE ;
}
/**
* Returns an array of playlist objects that this file is a part of .
* @ return array
*/
public function getPlaylists () {
2012-04-01 21:51:03 +02:00
global $CC_CONFIG ;
$con = Propel :: getConnection ();
2010-12-07 20:19:27 +01:00
$sql = " SELECT playlist_id "
2012-04-01 21:51:03 +02:00
. " FROM " . $CC_CONFIG [ 'playistTable' ]
. " WHERE file_id=' { $this -> id } ' " ;
$ids = $con -> query ( $sql ) -> fetchAll ();
2010-12-07 20:19:27 +01:00
$playlists = array ();
if ( is_array ( $ids ) && count ( $ids ) > 0 ) {
2010-12-07 23:29:28 +01:00
foreach ( $ids as $id ) {
2011-09-22 18:24:17 +02:00
$playlists [] = Application_Model_Playlist :: Recall ( $id );
2010-12-07 23:29:28 +01:00
}
2010-12-07 20:19:27 +01:00
}
return $playlists ;
}
/**
2011-06-08 11:59:48 +02:00
* Delete stored virtual file
2010-12-07 20:19:27 +01:00
*
2011-06-08 11:59:48 +02:00
* @ param boolean $p_deleteFile
*
2010-12-07 20:19:27 +01:00
*/
2012-01-31 22:12:11 +01:00
public function delete ( $deleteFromPlaylist = false )
2010-12-07 20:19:27 +01:00
{
2011-07-19 11:00:32 +02:00
2012-02-06 11:07:10 +01:00
$filepath = $this -> getFilePath ();
2011-06-08 11:59:48 +02:00
// Check if the file is scheduled to be played in the future
2011-09-23 22:50:00 +02:00
if ( Application_Model_Schedule :: IsFileScheduledInTheFuture ( $this -> getId ())) {
2012-02-05 18:19:22 +01:00
throw new DeleteScheduledFileException ();
2011-06-08 11:59:48 +02:00
}
2011-07-18 15:28:17 +02:00
2012-06-28 19:28:45 +02:00
$music_dir = Application_Model_MusicDir :: getDirByPK ( $this -> _file -> getDbDirectory ());
$type = $music_dir -> getType ();
if ( file_exists ( $filepath ) && $type == " stor " ) {
2012-02-05 18:19:22 +01:00
$data = array ( " filepath " => $filepath , " delete " => 1 );
Application_Model_RabbitMq :: SendMessageToMediaMonitor ( " file_delete " , $data );
2010-12-07 20:19:27 +01:00
}
2012-02-06 11:07:10 +01:00
if ( $deleteFromPlaylist ){
Application_Model_Playlist :: DeleteFileFromAllPlaylists ( $this -> getId ());
2011-06-08 10:15:35 +02:00
}
2012-04-12 22:17:19 +02:00
// set file_exists falg to false
$this -> _file -> setDbFileExists ( false );
$this -> _file -> save ();
}
/**
* This function is for when media monitor detects deletion of file
* and trying to update airtime side
*
* @ param boolean $p_deleteFile
*
*/
public function deleteByMediaMonitor ( $deleteFromPlaylist = false )
{
$filepath = $this -> getFilePath ();
2010-12-07 20:19:27 +01:00
2012-04-12 22:17:19 +02:00
if ( $deleteFromPlaylist ){
Application_Model_Playlist :: DeleteFileFromAllPlaylists ( $this -> getId ());
}
2012-02-05 18:19:22 +01:00
// set file_exists falg to false
$this -> _file -> setDbFileExists ( false );
$this -> _file -> save ();
2010-12-07 20:19:27 +01:00
}
/**
2011-06-08 11:59:48 +02:00
* Return suitable extension .
2010-12-07 20:19:27 +01:00
*
* @ return string
2011-06-08 11:59:48 +02:00
* file extension without a dot
2010-12-07 20:19:27 +01:00
*/
2011-06-08 11:59:48 +02:00
public function getFileExtension ()
2010-12-07 20:19:27 +01:00
{
2011-06-08 11:59:48 +02:00
$mime = $this -> _file -> getDbMime ();
2010-12-07 20:19:27 +01:00
2011-08-25 22:32:57 +02:00
if ( $mime == " audio/vorbis " || $mime == " application/ogg " ) {
2011-06-08 11:59:48 +02:00
return " ogg " ;
}
2011-08-25 22:32:57 +02:00
else if ( $mime == " audio/mp3 " || $mime == " audio/mpeg " ) {
2011-06-08 11:59:48 +02:00
return " mp3 " ;
}
2011-06-08 10:15:35 +02:00
}
2010-12-07 20:19:27 +01:00
/**
* Get real filename of raw media data
*
* @ return string
*/
2011-06-08 11:59:48 +02:00
public function getFilePath ()
2012-01-17 11:18:17 +01:00
{
2011-09-22 18:24:17 +02:00
$music_dir = Application_Model_MusicDir :: getDirByPK ( $this -> _file -> getDbDirectory ());
2011-12-22 23:21:18 +01:00
$directory = $music_dir -> getDirectory ();
2012-01-17 11:18:17 +01:00
2011-06-21 10:24:02 +02:00
$filepath = $this -> _file -> getDbFilepath ();
2011-12-22 23:21:18 +01:00
return $directory . $filepath ;
2011-06-21 10:24:02 +02:00
}
2012-06-15 22:48:51 +02:00
2011-06-21 10:24:02 +02:00
/**
* Set real filename of raw media data
*
* @ return string
*/
public function setFilePath ( $p_filepath )
{
2011-09-22 18:24:17 +02:00
$path_info = Application_Model_MusicDir :: splitFilePath ( $p_filepath );
2012-01-17 11:18:17 +01:00
2011-06-21 10:24:02 +02:00
if ( is_null ( $path_info )) {
return - 1 ;
}
2011-09-22 18:24:17 +02:00
$musicDir = Application_Model_MusicDir :: getDirByPath ( $path_info [ 0 ]);
2011-06-21 10:24:02 +02:00
$this -> _file -> setDbDirectory ( $musicDir -> getId ());
$this -> _file -> setDbFilepath ( $path_info [ 1 ]);
$this -> _file -> save ();
2010-12-07 20:19:27 +01:00
}
/**
2011-10-14 00:07:53 +02:00
* Get the URL to access this file using the server name / address that
* this PHP script was invoked through .
2010-12-07 20:19:27 +01:00
*/
public function getFileUrl ()
2011-09-22 21:31:21 +02:00
{
2011-09-09 17:45:19 +02:00
$serverName = $_SERVER [ 'SERVER_NAME' ];
$serverPort = $_SERVER [ 'SERVER_PORT' ];
2011-12-01 11:16:29 +01:00
2011-10-17 13:36:16 +02:00
return $this -> constructGetFileUrl ( $serverName , $serverPort );
2011-10-14 00:07:53 +02:00
}
2011-12-01 11:16:29 +01:00
2011-10-14 00:07:53 +02:00
/**
* Get the URL to access this file using the server name / address that
2011-11-14 22:08:45 +01:00
* is specified in the airtime . conf config file . If either of these is
* not specified , then use values provided by the $_SERVER global variable .
2011-10-14 00:07:53 +02:00
*/
public function getFileUrlUsingConfigAddress (){
global $CC_CONFIG ;
2011-12-01 11:16:29 +01:00
2011-11-14 22:08:45 +01:00
if ( isset ( $CC_CONFIG [ 'baseUrl' ])){
$serverName = $CC_CONFIG [ 'baseUrl' ];
} else {
$serverName = $_SERVER [ 'SERVER_NAME' ];
}
2011-12-01 11:16:29 +01:00
2011-11-14 22:08:45 +01:00
if ( isset ( $CC_CONFIG [ 'basePort' ])){
$serverPort = $CC_CONFIG [ 'basePort' ];
} else {
$serverPort = $_SERVER [ 'SERVER_PORT' ];
}
2011-10-17 13:36:16 +02:00
return $this -> constructGetFileUrl ( $serverName , $serverPort );
2011-10-14 00:07:53 +02:00
}
2011-12-01 11:16:29 +01:00
2011-10-14 00:07:53 +02:00
private function constructGetFileUrl ( $p_serverName , $p_serverPort ){
2012-03-07 18:27:32 +01:00
Logging :: log ( " getting media! - 2 " );
2011-10-14 00:07:53 +02:00
return " http:// $p_serverName : $p_serverPort /api/get-media/file/ " . $this -> getGunId () . " . " . $this -> getFileExtension ();
2011-06-09 11:50:03 +02:00
}
2011-06-22 22:50:58 +02:00
/**
* Sometimes we want a relative URL and not a full URL . See bug
* http :// dev . sourcefabric . org / browse / CC - 2403
*/
2011-06-24 22:04:57 +02:00
public function getRelativeFileUrl ( $baseUrl )
2011-06-22 22:50:58 +02:00
{
2012-02-25 00:24:24 +01:00
Logging :: log ( " getting media! " );
2011-06-24 22:04:57 +02:00
return $baseUrl . " /api/get-media/file/ " . $this -> getGunId () . " . " . $this -> getFileExtension ();
2011-06-22 22:50:58 +02:00
}
2011-06-09 11:50:03 +02:00
public static function Insert ( $md = null )
{
$file = new CcFiles ();
$file -> setDbGunid ( md5 ( uniqid ( " " , true )));
2012-01-10 20:45:43 +01:00
$file -> setDbUtime ( new DateTime ( " now " ), new DateTimeZone ( " UTC " ));
2012-01-09 04:31:18 +01:00
$file -> setDbMtime ( new DateTime ( " now " ), new DateTimeZone ( " UTC " ));
2011-06-09 11:50:03 +02:00
2011-09-22 18:24:17 +02:00
$storedFile = new Application_Model_StoredFile ();
2011-06-09 12:57:30 +02:00
$storedFile -> _file = $file ;
2011-06-21 10:24:02 +02:00
if ( isset ( $md [ 'MDATA_KEY_FILEPATH' ])) {
2012-01-17 11:18:17 +01:00
// removed "//" in the path. Always use '/' for path separator
2012-01-13 20:17:39 +01:00
$filepath = str_replace ( " // " , " / " , $md [ 'MDATA_KEY_FILEPATH' ]);
$res = $storedFile -> setFilePath ( $filepath );
2011-06-21 10:24:02 +02:00
if ( $res === - 1 ) {
return null ;
}
}
else {
return null ;
}
2011-06-09 11:50:03 +02:00
if ( isset ( $md )) {
2011-06-09 12:57:30 +02:00
$storedFile -> setMetadata ( $md );
2012-05-04 18:02:42 +02:00
}
2011-06-09 11:50:03 +02:00
return $storedFile ;
2010-12-07 20:19:27 +01:00
}
/**
2011-06-08 10:15:35 +02:00
* Fetch instance of StoreFile object .< br >
* Should be supplied with only ONE parameter , all the rest should
* be NULL .
2010-12-07 20:19:27 +01:00
*
2011-06-08 10:15:35 +02:00
* @ param int $p_id
* local id
* @ param string $p_gunid
* global unique id of file
* @ param string $p_md5sum
* MD5 sum of the file
2012-04-12 23:37:14 +02:00
* @ param boolean $exist
* When this is true , it check against only files with file_exist is 'true'
2011-09-22 18:24:17 +02:00
* @ return Application_Model_StoredFile | NULL
2011-06-08 10:15:35 +02:00
* Return NULL if the object doesnt exist in the DB .
2010-12-07 20:19:27 +01:00
*/
2012-04-12 23:37:14 +02:00
public static function Recall ( $p_id = null , $p_gunid = null , $p_md5sum = null , $p_filepath = null , $exist = false )
2011-06-08 10:15:35 +02:00
{
if ( isset ( $p_id )) {
2011-06-09 11:50:03 +02:00
$file = CcFilesQuery :: create () -> findPK ( intval ( $p_id ));
2011-06-08 10:15:35 +02:00
}
else if ( isset ( $p_gunid )) {
2011-06-09 11:50:03 +02:00
$file = CcFilesQuery :: create ()
2011-06-08 18:24:17 +02:00
-> filterByDbGunid ( $p_gunid )
-> findOne ();
2011-06-08 10:15:35 +02:00
}
else if ( isset ( $p_md5sum )) {
2012-04-12 23:37:14 +02:00
if ( $exist ){
$file = CcFilesQuery :: create ()
-> filterByDbMd5 ( $p_md5sum )
-> filterByDbFileExists ( true )
-> findOne ();
} else {
$file = CcFilesQuery :: create ()
2011-06-08 18:24:17 +02:00
-> filterByDbMd5 ( $p_md5sum )
-> findOne ();
2012-04-12 23:37:14 +02:00
}
2011-06-08 10:15:35 +02:00
}
else if ( isset ( $p_filepath )) {
2011-09-22 18:24:17 +02:00
$path_info = Application_Model_MusicDir :: splitFilePath ( $p_filepath );
2011-07-25 16:21:42 +02:00
2011-06-21 10:24:02 +02:00
if ( is_null ( $path_info )) {
return null ;
}
2011-09-22 18:24:17 +02:00
$music_dir = Application_Model_MusicDir :: getDirByPath ( $path_info [ 0 ]);
2011-06-21 10:24:02 +02:00
2011-06-09 11:50:03 +02:00
$file = CcFilesQuery :: create ()
2011-06-21 10:24:02 +02:00
-> filterByDbDirectory ( $music_dir -> getId ())
-> filterByDbFilepath ( $path_info [ 1 ])
2011-06-08 18:24:17 +02:00
-> findOne ();
2011-06-08 10:15:35 +02:00
}
else {
return null ;
}
2011-06-09 12:57:30 +02:00
if ( isset ( $file )) {
2011-09-22 18:24:17 +02:00
$storedFile = new Application_Model_StoredFile ();
2011-06-09 12:57:30 +02:00
$storedFile -> _file = $file ;
2011-06-09 11:50:03 +02:00
2011-06-09 12:57:30 +02:00
return $storedFile ;
}
2011-06-10 16:43:30 +02:00
else {
2011-06-09 12:57:30 +02:00
return null ;
}
2011-06-08 10:15:35 +02:00
}
2011-06-28 15:04:03 +02:00
2011-06-27 17:23:48 +02:00
public function getName (){
$info = pathinfo ( $this -> getFilePath ());
return $info [ 'filename' ];
}
2011-06-08 10:15:35 +02:00
/**
* Create instance of StoreFile object and recall existing file
* by gunid .
*
* @ param string $p_gunid
* global unique id of file
2011-09-22 18:24:17 +02:00
* @ return Application_Model_StoredFile | NULL
2011-06-08 10:15:35 +02:00
*/
public static function RecallByGunid ( $p_gunid )
2010-12-07 20:19:27 +01:00
{
2011-09-22 18:24:17 +02:00
return Application_Model_StoredFile :: Recall ( null , $p_gunid );
2010-12-07 20:19:27 +01:00
}
/**
2011-09-22 18:24:17 +02:00
* Fetch the Application_Model_StoredFile by looking up the MD5 value .
2010-12-07 20:19:27 +01:00
*
2011-06-08 10:15:35 +02:00
* @ param string $p_md5sum
2011-09-22 18:24:17 +02:00
* @ return Application_Model_StoredFile | NULL
2010-12-07 20:19:27 +01:00
*/
2012-04-12 23:37:14 +02:00
public static function RecallByMd5 ( $p_md5sum , $exist = false )
2010-12-07 20:19:27 +01:00
{
2012-04-12 23:37:14 +02:00
return Application_Model_StoredFile :: Recall ( null , null , $p_md5sum , null , $exist );
2010-12-07 20:19:27 +01:00
}
2011-06-08 10:15:35 +02:00
/**
2011-09-22 18:24:17 +02:00
* Fetch the Application_Model_StoredFile by looking up its filepath .
2011-06-08 10:15:35 +02:00
*
* @ param string $p_filepath path of file stored in Airtime .
2011-09-22 18:24:17 +02:00
* @ return Application_Model_StoredFile | NULL
2011-06-08 10:15:35 +02:00
*/
public static function RecallByFilepath ( $p_filepath )
{
2011-09-22 18:24:17 +02:00
return Application_Model_StoredFile :: Recall ( null , null , null , $p_filepath );
2011-06-08 10:15:35 +02:00
}
2011-12-01 11:16:29 +01:00
2011-09-27 20:49:03 +02:00
public static function RecallByPartialFilepath ( $partial_path ){
$path_info = Application_Model_MusicDir :: splitFilePath ( $partial_path );
if ( is_null ( $path_info )) {
return null ;
}
$music_dir = Application_Model_MusicDir :: getDirByPath ( $path_info [ 0 ]);
$files = CcFilesQuery :: create ()
-> filterByDbDirectory ( $music_dir -> getId ())
-> filterByDbFilepath ( " $path_info[1] % " )
-> find ();
$res = array ();
foreach ( $files as $file ){
$storedFile = new Application_Model_StoredFile ();
$storedFile -> _file = $file ;
$res [] = $storedFile ;
}
return $res ;
}
2011-12-01 11:16:29 +01:00
2012-03-12 15:32:24 +01:00
public static function searchLibraryFiles ( $datatables ) {
2012-04-01 21:51:03 +02:00
2012-03-12 15:32:24 +01:00
$con = Propel :: getConnection ( CcFilesPeer :: DATABASE_NAME );
2010-12-07 23:29:28 +01:00
2012-02-22 18:38:33 +01:00
$displayColumns = array ( " id " , " track_title " , " artist_name " , " album_title " , " genre " , " length " ,
" year " , " utime " , " mtime " , " ftype " , " track_number " , " mood " , " bpm " , " composer " , " info_url " ,
2012-03-06 00:59:31 +01:00
" bit_rate " , " sample_rate " , " isrc_number " , " encoded_by " , " label " , " copyright " , " mime " ,
" language " , " gunid " , " filepath "
2012-02-22 18:38:33 +01:00
);
$plSelect = array ();
$fileSelect = array ();
foreach ( $displayColumns as $key ) {
if ( $key === " id " ) {
$plSelect [] = " PL.id AS " . $key ;
$fileSelect [] = $key ;
2012-02-24 00:55:20 +01:00
}
else if ( $key === " track_title " ) {
2012-02-22 18:38:33 +01:00
$plSelect [] = " name AS " . $key ;
$fileSelect [] = $key ;
2012-02-24 00:55:20 +01:00
}
else if ( $key === " ftype " ) {
$plSelect [] = " 'playlist'::varchar AS " . $key ;
2012-02-22 18:38:33 +01:00
$fileSelect [] = $key ;
2012-02-24 00:55:20 +01:00
}
else if ( $key === " artist_name " ) {
2012-02-22 18:38:33 +01:00
$plSelect [] = " login AS " . $key ;
$fileSelect [] = $key ;
2012-02-24 00:55:20 +01:00
}
2012-02-24 16:05:01 +01:00
//same columns in each table.
else if ( in_array ( $key , array ( " length " , " utime " , " mtime " ))) {
2012-02-22 18:38:33 +01:00
$plSelect [] = $key ;
$fileSelect [] = $key ;
2012-02-24 00:55:20 +01:00
}
2012-02-24 16:05:01 +01:00
else if ( $key === " year " ) {
2012-02-27 14:59:16 +01:00
2012-02-24 16:05:01 +01:00
$plSelect [] = " EXTRACT(YEAR FROM utime)::varchar AS " . $key ;
2012-05-10 17:01:22 +02:00
$fileSelect [] = " year AS " . $key ;
2012-02-24 00:55:20 +01:00
}
//need to cast certain data as ints for the union to search on.
2012-02-27 12:07:46 +01:00
else if ( in_array ( $key , array ( " track_number " , " bit_rate " , " sample_rate " ))){
2012-02-24 00:55:20 +01:00
$plSelect [] = " NULL::int AS " . $key ;
$fileSelect [] = $key ;
}
else {
$plSelect [] = " NULL::text AS " . $key ;
2012-02-22 18:38:33 +01:00
$fileSelect [] = $key ;
2010-12-30 00:43:17 +01:00
}
}
2012-02-22 18:38:33 +01:00
$plSelect = " SELECT " . join ( " , " , $plSelect );
$fileSelect = " SELECT " . join ( " , " , $fileSelect );
2012-02-24 00:55:20 +01:00
$type = intval ( $datatables [ " type " ]);
$plTable = " ( { $plSelect } FROM cc_playlist AS PL LEFT JOIN cc_subjs AS sub ON (sub.id = PL.creator_id)) " ;
$fileTable = " ( { $fileSelect } FROM cc_files AS FILES WHERE file_exists = 'TRUE') " ;
$unionTable = " ( { $plTable } UNION { $fileTable } ) AS RESULTS " ;
2012-01-17 11:18:17 +01:00
2012-02-24 00:55:20 +01:00
//choose which table we need to select data from.
switch ( $type ) {
case 0 :
$fromTable = $unionTable ;
break ;
case 1 :
$fromTable = $fileTable . " AS File " ; //need an alias for the table if it's standalone.
break ;
case 2 :
$fromTable = $plTable . " AS Playlist " ; //need an alias for the table if it's standalone.
break ;
default :
$fromTable = $unionTable ;
}
2012-04-01 21:51:03 +02:00
2012-03-12 15:32:24 +01:00
$results = Application_Model_Datatables :: findEntries ( $con , $displayColumns , $fromTable , $datatables );
2012-01-17 11:18:17 +01:00
2012-03-01 23:56:16 +01:00
//Used by the audio preview functionality in the library.
2012-02-20 11:41:44 +01:00
foreach ( $results [ 'aaData' ] as & $row ) {
2012-02-01 18:47:08 +01:00
$row [ 'id' ] = intval ( $row [ 'id' ]);
2012-02-24 15:07:04 +01:00
$formatter = new LengthFormatter ( $row [ 'length' ]);
$row [ 'length' ] = $formatter -> format ();
2012-02-01 23:33:20 +01:00
2012-02-24 18:22:07 +01:00
if ( $row [ 'ftype' ] === " audioclip " ) {
$formatter = new SamplerateFormatter ( $row [ 'sample_rate' ]);
$row [ 'sample_rate' ] = $formatter -> format ();
$formatter = new BitrateFormatter ( $row [ 'bit_rate' ]);
$row [ 'bit_rate' ] = $formatter -> format ();
}
2012-01-13 20:17:39 +01:00
// add checkbox row
$row [ 'checkbox' ] = " <input type='checkbox' name='cb_ " . $row [ 'id' ] . " '> " ;
2012-01-17 11:18:17 +01:00
$type = substr ( $row [ 'ftype' ], 0 , 2 );
2012-02-01 23:33:20 +01:00
$row [ 'tr_id' ] = " { $type } _ { $row [ 'id' ] } " ;
2012-01-17 11:18:17 +01:00
//TODO url like this to work on both playlist/showbuilder screens.
//datatable stuff really needs to be pulled out and generalized within the project
//access to zend view methods to access url helpers is needed.
2012-02-11 00:37:10 +01:00
2012-03-06 00:59:31 +01:00
if ( $type == " au " ){ //&& isset( $audioResults )) {
$row [ 'audioFile' ] = $row [ 'gunid' ] . " . " . pathinfo ( $row [ 'filepath' ], PATHINFO_EXTENSION );
2012-04-30 22:20:23 +02:00
$row [ 'image' ] = '<img title="Track preview" src="/css/images/icon_audioclip.png">' ;
2012-01-17 11:18:17 +01:00
}
else {
2012-05-15 21:10:56 +02:00
$row [ 'image' ] = '<img title="Playlist preview" src="/css/images/icon_playlist.png">' ;
2012-01-17 11:18:17 +01:00
}
2012-01-13 20:17:39 +01:00
}
2012-01-17 11:18:17 +01:00
2012-01-13 20:17:39 +01:00
return $results ;
2012-01-09 04:31:18 +01:00
}
2012-03-07 18:27:32 +01:00
2011-06-08 10:15:35 +02:00
public static function uploadFile ( $p_targetDir )
2011-06-06 16:18:03 +02:00
{
2011-03-25 04:07:13 +01:00
// HTTP headers for no cache etc
2012-02-28 19:47:57 +01:00
header ( 'Content-type: text/plain; charset=UTF-8' );
header ( " Expires: Mon, 26 Jul 1997 05:00:00 GMT " );
header ( " Last-Modified: " . gmdate ( " D, d M Y H:i:s " ) . " GMT " );
header ( " Cache-Control: no-store, no-cache, must-revalidate " );
header ( " Cache-Control: post-check=0, pre-check=0 " , false );
header ( " Pragma: no-cache " );
2012-03-07 18:27:32 +01:00
2012-02-28 19:47:57 +01:00
// Settings
$cleanupTargetDir = false ; // Remove old files
$maxFileAge = 60 * 60 ; // Temp file age in seconds
2012-03-07 18:27:32 +01:00
2012-02-28 19:47:57 +01:00
// 5 minutes execution time
@ set_time_limit ( 5 * 60 );
// usleep(5000);
2012-03-07 18:27:32 +01:00
2012-02-28 19:47:57 +01:00
// Get parameters
$chunk = isset ( $_REQUEST [ " chunk " ]) ? $_REQUEST [ " chunk " ] : 0 ;
$chunks = isset ( $_REQUEST [ " chunks " ]) ? $_REQUEST [ " chunks " ] : 0 ;
$fileName = isset ( $_REQUEST [ " name " ]) ? $_REQUEST [ " name " ] : '' ;
2011-09-22 23:30:05 +02:00
Logging :: log ( __FILE__ . " :uploadFile(): filename= $fileName to $p_targetDir " );
2012-02-28 19:47:57 +01:00
// Clean the fileName for security reasons
2011-07-21 12:12:37 +02:00
//this needs fixing for songs not in ascii.
2012-02-28 19:47:57 +01:00
//$fileName = preg_replace('/[^\w\._]+/', '', $fileName);
2012-03-07 18:27:32 +01:00
2012-02-28 19:47:57 +01:00
// Create target dir
if ( ! file_exists ( $p_targetDir ))
@ mkdir ( $p_targetDir );
2012-03-07 18:27:32 +01:00
2012-02-28 19:47:57 +01:00
// Remove old temp files
if ( is_dir ( $p_targetDir ) && ( $dir = opendir ( $p_targetDir ))) {
while (( $file = readdir ( $dir )) !== false ) {
$filePath = $p_targetDir . DIRECTORY_SEPARATOR . $file ;
2012-03-07 18:27:32 +01:00
2012-02-28 19:47:57 +01:00
// Remove temp files if they are older than the max age
if ( preg_match ( '/\.tmp$/' , $file ) && ( filemtime ( $filePath ) < time () - $maxFileAge ))
@ unlink ( $filePath );
}
2012-03-07 18:27:32 +01:00
2012-02-28 19:47:57 +01:00
closedir ( $dir );
} else
die ( '{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}' );
2012-03-07 18:27:32 +01:00
2012-02-28 19:47:57 +01:00
// Look for the content type header
if ( isset ( $_SERVER [ " HTTP_CONTENT_TYPE " ]))
$contentType = $_SERVER [ " HTTP_CONTENT_TYPE " ];
2012-03-07 18:27:32 +01:00
2012-02-28 19:47:57 +01:00
if ( isset ( $_SERVER [ " CONTENT_TYPE " ]))
$contentType = $_SERVER [ " CONTENT_TYPE " ];
2011-03-25 04:07:13 +01:00
2011-11-23 20:12:14 +01:00
// create temp file name (CC-3086)
2011-12-14 21:58:26 +01:00
// we are not using mktemp command anymore.
// plupload support unique_name feature.
$tempFilePath = $p_targetDir . DIRECTORY_SEPARATOR . $fileName ;
2011-12-01 11:16:29 +01:00
2012-02-28 19:47:57 +01:00
if ( strpos ( $contentType , " multipart " ) !== false ) {
if ( isset ( $_FILES [ 'file' ][ 'tmp_name' ]) && is_uploaded_file ( $_FILES [ 'file' ][ 'tmp_name' ])) {
// Open temp file
$out = fopen ( $tempFilePath , $chunk == 0 ? " wb " : " ab " );
if ( $out ) {
// Read binary input stream and append it to temp file
$in = fopen ( $_FILES [ 'file' ][ 'tmp_name' ], " rb " );
2012-03-07 18:27:32 +01:00
2012-02-28 19:47:57 +01:00
if ( $in ) {
while ( $buff = fread ( $in , 4096 ))
fwrite ( $out , $buff );
} else
die ( '{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}' );
2012-03-07 18:27:32 +01:00
2012-02-28 19:47:57 +01:00
fclose ( $out );
unlink ( $_FILES [ 'file' ][ 'tmp_name' ]);
} else
die ( '{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}' );
} else
die ( '{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}' );
} else {
// Open temp file
$out = fopen ( $tempFilePath , $chunk == 0 ? " wb " : " ab " );
if ( $out ) {
// Read binary input stream and append it to temp file
$in = fopen ( " php://input " , " rb " );
2012-03-07 18:27:32 +01:00
2012-02-28 19:47:57 +01:00
if ( $in ) {
while ( $buff = fread ( $in , 4096 ))
fwrite ( $out , $buff );
} else
die ( '{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}' );
2012-03-07 18:27:32 +01:00
2012-02-28 19:47:57 +01:00
fclose ( $out );
} else
die ( '{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}' );
}
2012-03-07 18:27:32 +01:00
2012-02-28 19:47:57 +01:00
return $tempFilePath ;
2011-07-29 22:45:13 +02:00
}
2011-08-03 18:32:59 +02:00
2012-02-09 18:45:39 +01:00
/**
* Check , using disk_free_space , the space available in the $destination_folder folder to see if it has
* enough space to move the $audio_file into and report back to the user if not .
**/
2012-07-06 04:37:40 +02:00
public static function isEnoughDiskSpaceToCopy ( $destination_folder , $audio_file ){
2012-02-17 11:50:49 +01:00
//check to see if we have enough space in the /organize directory to copy the file
$freeSpace = disk_free_space ( $destination_folder );
$fileSize = filesize ( $audio_file );
2012-04-01 21:51:03 +02:00
2012-07-06 04:37:40 +02:00
return $freeSpace >= $fileSize ;
2012-02-09 18:45:39 +01:00
}
2012-02-16 19:46:14 +01:00
2011-11-23 20:12:14 +01:00
public static function copyFileToStor ( $p_targetDir , $fileName , $tempname ){
$audio_file = $p_targetDir . DIRECTORY_SEPARATOR . $tempname ;
2011-08-30 21:35:03 +02:00
Logging :: log ( 'copyFileToStor: moving file ' . $audio_file );
2011-07-29 22:45:13 +02:00
$md5 = md5_file ( $audio_file );
2012-04-12 23:37:14 +02:00
$duplicate = Application_Model_StoredFile :: RecallByMd5 ( $md5 , true );
2012-04-01 21:51:03 +02:00
2012-03-27 01:00:40 +02:00
$result = null ;
2011-07-29 22:45:13 +02:00
if ( $duplicate ) {
if ( file_exists ( $duplicate -> getFilePath ())) {
$duplicateName = $duplicate -> getMetadataValue ( 'MDATA_KEY_TITLE' );
2012-02-15 00:12:29 +01:00
$result = array ( " code " => 106 , " message " => " An identical audioclip named ' $duplicateName ' already exists on the server. " );
2011-07-29 22:45:13 +02:00
}
}
2011-08-03 18:32:59 +02:00
2012-03-27 01:00:40 +02:00
if ( ! isset ( $result )){ //The file has no duplicate, so proceed to copy.
2012-02-28 19:47:57 +01:00
$storDir = Application_Model_MusicDir :: getStorDir ();
$stor = $storDir -> getDirectory ();
2012-04-12 18:22:20 +02:00
// check if "organize" dir exists and if not create one
if ( ! file_exists ( $stor . " /organize " )){
2012-04-13 21:09:59 +02:00
if ( ! mkdir ( $stor . " /organize " , 0777 )){
$result = array ( " code " => 109 , " message " => " Failed to create 'organize' directory. " );
return $result ;
}
2012-04-12 18:22:20 +02:00
}
2012-07-06 04:37:40 +02:00
if ( chmod ( $audio_file , 0644 ) === false ){
Logging :: log ( " Warning: couldn't change permissions of $audio_file to 0644 " );
}
2012-02-28 19:47:57 +01:00
//check to see if there is enough space in $stor to continue.
2012-07-06 04:37:40 +02:00
if ( self :: isEnoughDiskSpaceToCopy ( $stor , $audio_file )){
2012-04-24 22:15:22 +02:00
$audio_stor = Application_Common_OsPath :: join ( $stor , " organize " , $fileName );
2012-02-28 19:47:57 +01:00
2012-07-06 04:37:40 +02:00
if ( self :: liquidsoapFilePlayabilityTest ( $audio_file )){
Logging :: log ( " copyFileToStor: moving file $audio_file to $audio_stor " );
2012-06-04 23:01:43 +02:00
//Martin K.: changed to rename: Much less load + quicker since this is an atomic operation
2012-07-06 04:37:40 +02:00
if ( @ rename ( $audio_file , $audio_stor ) === false ) {
2012-06-04 23:01:43 +02:00
#something went wrong likely there wasn't enough space in the audio_stor to move the file too.
#warn the user that the file wasn't uploaded and they should check if there is enough disk space.
2012-07-06 04:37:40 +02:00
unlink ( $audio_file ); //remove the file after failed rename
$result = array ( " code " => 108 , " message " => " The file was not uploaded, this error can occur if the computer hard drive does not have enough disk space. " );
}
} else {
$result = array ( " code " => 110 , " message " => " This file appears to be corrupted and will not be added to media library. " );
2012-02-28 19:47:57 +01:00
}
2012-07-06 04:37:40 +02:00
} else {
$result = array ( " code " => 107 , " message " => " The file was not uploaded, there is " . $freeSpace . " MB of disk space left and the file you are uploading has a size of " . $fileSize . " MB. " );
2012-03-27 01:00:40 +02:00
}
2012-02-28 19:47:57 +01:00
}
2012-03-27 01:00:40 +02:00
return $result ;
2011-03-25 04:07:13 +01:00
}
2012-07-06 04:37:40 +02:00
/*
* Pass the file through Liquidsoap and test if it is readable . Return True if readable , and False otherwise .
*/
public static function liquidsoapFilePlayabilityTest ( $audio_file ){
$LIQUIDSOAP_ERRORS = array ( 'TagLib: MPEG::Properties::read() -- Could not find a valid last MPEG frame in the stream.' );
// Ask Liquidsoap if file is playable
$command = sprintf ( " /usr/bin/airtime-liquidsoap -c 'output.dummy(audio_to_stereo(single( \" %s \" )))' 2>&1 " , $audio_file );
exec ( $command , $output , $rv );
$isError = count ( $output ) > 0 && in_array ( $output [ 0 ], $LIQUIDSOAP_ERRORS );
return ( $rv == 0 && ! $isError );
}
2011-07-22 12:54:42 +02:00
public static function getFileCount ()
{
2012-04-01 21:51:03 +02:00
global $CC_CONFIG ;
$con = Propel :: getConnection ();
2012-04-03 21:53:55 +02:00
$sql = " SELECT count(*) as cnt FROM " . $CC_CONFIG [ " filesTable " ] . " WHERE file_exists " ;
2012-04-01 21:51:03 +02:00
return $con -> query ( $sql ) -> fetchColumn ( 0 );
2011-07-22 12:54:42 +02:00
}
2012-01-13 20:17:39 +01:00
/**
2012-01-17 11:18:17 +01:00
*
2012-01-13 20:17:39 +01:00
* Enter description here ...
* @ param $dir_id - if this is not provided , it returns all files with full path constructed .
* @ param $propelObj - if this is true , it returns array of proepl obj
*/
2012-04-01 21:51:03 +02:00
public static function listAllFiles ( $dir_id = null , $propelObj = false )
{
$con = Propel :: getConnection ();
2012-01-17 11:18:17 +01:00
2012-04-01 21:51:03 +02:00
if ( $propelObj ) {
2012-01-13 20:17:39 +01:00
$sql = " SELECT m.directory || f.filepath as fp "
. " FROM CC_MUSIC_DIRS m "
. " LEFT JOIN CC_FILES f "
. " ON m.id = f.directory WHERE m.id = $dir_id and f.file_exists = 'TRUE' " ;
2012-04-01 21:51:03 +02:00
} else {
2012-01-13 20:17:39 +01:00
$sql = " SELECT filepath as fp "
. " FROM CC_FILES "
. " WHERE directory = $dir_id and file_exists = 'TRUE' " ;
}
2012-04-01 21:51:03 +02:00
$rows = $con -> query ( $sql ) -> fetchAll ();
2011-07-04 20:29:09 +02:00
$results = array ();
2012-04-01 21:51:03 +02:00
foreach ( $rows as $row ) {
if ( $propelObj ) {
2012-01-13 20:17:39 +01:00
$results [] = Application_Model_StoredFile :: RecallByFilepath ( $row [ " fp " ]);
2012-04-01 21:51:03 +02:00
} else {
2012-01-13 20:17:39 +01:00
$results [] = $row [ " fp " ];
}
2011-07-04 20:29:09 +02:00
}
return $results ;
}
2012-05-07 17:24:40 +02:00
/* Gets number of tracks uploaded to
* Soundcloud in the last 24 hours
*/
public static function getSoundCloudUploads ()
{
2012-05-07 18:12:08 +02:00
try {
$con = Propel :: getConnection ();
2012-05-07 17:24:40 +02:00
2012-05-07 18:12:08 +02:00
$sql = " SELECT soundcloud_id as id, soundcloud_upload_time "
. " FROM CC_FILES "
. " WHERE (id != -2 and id != -3) and "
. " (soundcloud_upload_time >= (now() - (INTERVAL '1 day'))) " ;
2012-05-07 17:24:40 +02:00
2012-05-07 18:12:08 +02:00
$rows = $con -> query ( $sql ) -> fetchAll ();
return count ( $rows );
} catch ( Exception $e ) {
header ( 'HTTP/1.0 503 Service Unavailable' );
Logging :: log ( " Could not connect to database. " );
exit ;
}
2012-05-07 17:24:40 +02:00
}
2011-09-22 21:31:21 +02:00
2011-10-22 17:34:04 +02:00
public function setSoundCloudLinkToFile ( $link_to_file )
{
$this -> _file -> setDbSoundCloudLinkToFile ( $link_to_file )
-> save ();
}
2011-12-01 11:16:29 +01:00
2011-10-22 17:34:04 +02:00
public function getSoundCloudLinkToFile (){
return $this -> _file -> getDbSoundCloudLinkToFile ();
}
2011-12-01 11:16:29 +01:00
2011-09-22 17:47:24 +02:00
public function setSoundCloudFileId ( $p_soundcloud_id )
{
$this -> _file -> setDbSoundCloudId ( $p_soundcloud_id )
-> save ();
}
2011-09-22 21:31:21 +02:00
2011-09-22 17:47:24 +02:00
public function getSoundCloudId (){
return $this -> _file -> getDbSoundCloudId ();
}
2011-09-22 21:31:21 +02:00
2011-09-22 17:47:24 +02:00
public function setSoundCloudErrorCode ( $code ){
$this -> _file -> setDbSoundCloudErrorCode ( $code )
-> save ();
}
2011-09-22 21:31:21 +02:00
2011-09-22 17:47:24 +02:00
public function getSoundCloudErrorCode (){
return $this -> _file -> getDbSoundCloudErrorCode ();
}
2011-09-22 21:31:21 +02:00
2011-09-22 17:47:24 +02:00
public function setSoundCloudErrorMsg ( $msg ){
$this -> _file -> setDbSoundCloudErrorMsg ( $msg )
-> save ();
}
2011-09-22 21:31:21 +02:00
2011-09-22 17:47:24 +02:00
public function getSoundCloudErrorMsg (){
return $this -> _file -> getDbSoundCloudErrorMsg ();
}
2012-06-15 22:48:51 +02:00
public function getDirectory (){
return $this -> _file -> getDbDirectory ();
}
2012-01-17 11:18:17 +01:00
2012-01-13 20:17:39 +01:00
public function setFileExistsFlag ( $flag ){
$this -> _file -> setDbFileExists ( $flag )
-> save ();
}
2012-05-07 17:24:40 +02:00
public function setSoundCloudUploadTime ( $time ){
$this -> _file -> setDbSoundCloundUploadTime ( $time )
-> save ();
}
2012-01-17 11:18:17 +01:00
2012-01-13 20:17:39 +01:00
public function getFileExistsFlag (){
return $this -> _file -> getDbFileExists ();
}
2011-09-22 21:31:21 +02:00
2011-09-22 17:47:24 +02:00
public function uploadToSoundCloud ()
{
global $CC_CONFIG ;
2011-12-01 11:16:29 +01:00
2011-09-22 17:47:24 +02:00
$file = $this -> _file ;
if ( is_null ( $file )) {
return " File does not exist " ;
}
2011-09-29 23:10:17 +02:00
if ( Application_Model_Preference :: GetUploadToSoundcloudOption ())
2011-09-22 17:47:24 +02:00
{
for ( $i = 0 ; $i < $CC_CONFIG [ 'soundcloud-connection-retries' ]; $i ++ ) {
$description = $file -> getDbTrackTitle ();
2011-10-22 17:34:04 +02:00
$tag = array ();
2011-09-22 17:47:24 +02:00
$genre = $file -> getDbGenre ();
$release = $file -> getDbYear ();
try {
2011-10-04 00:02:01 +02:00
$soundcloud = new Application_Model_Soundcloud ();
2011-10-22 17:34:04 +02:00
$soundcloud_res = $soundcloud -> uploadTrack ( $this -> getFilePath (), $this -> getName (), $description , $tag , $release , $genre );
$this -> setSoundCloudFileId ( $soundcloud_res [ 'id' ]);
$this -> setSoundCloudLinkToFile ( $soundcloud_res [ 'permalink_url' ]);
2012-05-07 17:24:40 +02:00
$this -> setSoundCloudUploadTime ( new DateTime ( " now " ), new DateTimeZone ( " UTC " ));
2011-09-22 17:47:24 +02:00
break ;
}
catch ( Services_Soundcloud_Invalid_Http_Response_Code_Exception $e ) {
$code = $e -> getHttpCode ();
$msg = $e -> getHttpBody ();
$temp = explode ( '"error":' , $msg );
$msg = trim ( $temp [ 1 ], '"}' );
$this -> setSoundCloudErrorCode ( $code );
$this -> setSoundCloudErrorMsg ( $msg );
// setting sc id to -3 which indicates error
2011-09-29 20:47:07 +02:00
$this -> setSoundCloudFileId ( SOUNDCLOUD_ERROR );
2011-09-22 17:47:24 +02:00
if ( ! in_array ( $code , array ( 0 , 100 ))) {
break ;
}
}
sleep ( $CC_CONFIG [ 'soundcloud-connection-wait' ]);
}
}
}
2010-12-07 23:29:28 +01:00
}
2011-02-22 18:22:31 +01:00
2012-02-05 18:19:22 +01:00
class DeleteScheduledFileException extends Exception {}
2012-02-16 19:46:14 +01:00
class FileDoesNotExistException extends Exception {}