_authorize( 'write', $ac->getId(), $sessid )) !== TRUE) return $res; if($ac->isAccessed()){ return PEAR::raiseError( 'LocStor.php: storeAudioClipOpen: is accessed' ); } $res = $ac->replace( $ac->getId(), $ac->name, '', $metadata, 'string' ); if(PEAR::isError($res)) return $res; }else{ // gunid doesn't exists - do insert $tmpid = uniqid(''); $parid = $this->_getHomeDirId($sessid); if(PEAR::isError($parid)) return $parid; if(($res = $this->_authorize('write', $parid, $sessid)) !== TRUE) return $res; $oid = $this->addObj($tmpid , 'File', $parid); if(PEAR::isError($oid)) return $oid; $ac =& StoredFile::insert( &$this, $oid, '', '', $metadata, 'string', $gunid, 'audioclip' ); if(PEAR::isError($ac)){ $res = $this->removeObj($oid); return $ac; } $res = $this->renameFile($oid, $ac->gunid, $sessid); if(PEAR::isError($res)) return $res; } $res = $ac->setState('incomplete'); if(PEAR::isError($res)) return $res; return $this->bsOpenPut($chsum, $ac->gunid); } /** * Store or replace existing audio clip * * @param sessid string * @param token string * @return string gunid or PEAR::error */ function storeAudioClipClose($sessid, $token) { $ac =& StoredFile::recallByToken(&$this, $token); if(PEAR::isError($ac)){ return $ac; } $fname = $this->bsClosePut($token); if(PEAR::isError($fname)){ return $fname; } $res = $ac->replaceRawMediaData($fname); if(PEAR::isError($res)){ return $res; } if(file_exists($fname)) @unlink($fname); $res = $ac->setState('ready'); if(PEAR::isError($res)) return $res; return $ac->gunid; } /* --------------------------------------------------------------- access */ /** * Make access to audio clip * * @param sessid string * @param gunid string * @return array with: seekable filehandle, access token */ function accessRawAudioData($sessid, $gunid) { $ac =& StoredFile::recallByGunid(&$this, $gunid); if(PEAR::isError($ac)) return $ac; if(($res = $this->_authorize('read', $ac->getId(), $sessid)) !== TRUE) return $res; return $ac->accessRawMediaData($sessid); } /** * Release access to audio clip * * @param sessid string * @param token string, access token * @return boolean or PEAR::error */ function releaseRawAudioData($sessid, $token) { $ac =& StoredFile::recallByToken(&$this, $token); if(PEAR::isError($ac)) return $ac; return $ac->releaseRawMediaData($token); } /* ------------------------------------------------------------- download */ /** * Create and return downloadable URL for audio file * * @param sessid string, session id * @param gunid string, global unique id * @return array with: downloadable URL, download token */ function downloadRawAudioDataOpen($sessid, $gunid) { $res = $this->existsAudioClip($sessid, $gunid); if(PEAR::isError($res)) return $res; $id = $this->_idFromGunid($gunid); if(is_null($id)){ return PEAR::raiseError( "LocStor::downloadRawAudioDataOpen: gunid not found ($gunid)" ); } if(($res = $this->_authorize('read', $id, $sessid)) !== TRUE) return $res; return $this->bsOpenDownload($id); } /** * Discard downloadable URL for audio file * * @param token string, download token * @return string, gunid */ function downloadRawAudioDataClose($token) { return $this->bsCloseDownload($token); } /** * Create and return downloadable URL for metadata * * @param sessid string, session id * @param gunid string, global unique id * @return array with: downloadable URL, download token */ function downloadMetadataOpen($sessid, $gunid) { $res = $this->existsAudioClip($sessid, $gunid); if(PEAR::isError($res)) return $res; $id = $this->_idFromGunid($gunid); if(is_null($id)){ return PEAR::raiseError( "LocStor::downloadMetadataOpen: gunid not found ($gunid)" ); } if(($res = $this->_authorize('read', $id, $sessid)) !== TRUE) return $res; return $this->bsOpenDownload($id, 'metadata'); } /** * Discard downloadable URL for metadata * * @param token string, download token * @return string, gunid */ function downloadMetadataClose($token) { return $this->bsCloseDownload($token, 'metadata'); } /** * Return metadata as XML * * @param sessid string * @param gunid string * @return string or PEAR::error */ function getAudioClip($sessid, $gunid) { $ac =& StoredFile::recallByGunid(&$this, $gunid); if(PEAR::isError($ac)) return $ac; if(($res = $this->_authorize('read', $ac->getId(), $sessid)) !== TRUE) return $res; $md = $this->getMdata($ac->getId(), $sessid); if(PEAR::isError($md)) return $md; return $md; } /* --------------------------------------------------------------- search */ /** * Search in metadata database * * @param sessid string * @param criteria string * @return array or PEAR::error * @see GreenBox::localSearch */ function searchMetadata($sessid, $criteria) { $res = $this->localSearch($criteria, $sessid); return $res; } /* ----------------------------------------------------------------- etc. */ /** * Check if audio clip exists * * @param sessid string * @param gunid string * @return boolean * @see GreenBox */ function existsAudioClip($sessid, $gunid) { $ex = $this->existsFile($sessid, $gunid, 'audioclip'); if(!$ex) return FALSE; $ac =& StoredFile::recallByGunid(&$this, $gunid); if(PEAR::isError($ac)){ return $ac; } return $ac->exists(); } /** * Check if file exists in the storage * * @param sessid string * @param gunid string * @param ftype string, internal file type * @return boolean * @see GreenBox */ function existsFile($sessid, $gunid, $ftype=NULL) { $ac =& StoredFile::recallByGunid(&$this, $gunid); if(PEAR::isError($ac)){ // catch some exceptions switch($ac->getCode()){ case GBERR_FILENEX: case GBERR_FOBJNEX: return FALSE; break; default: return $ac; } } if(!is_null($ftype) && ($ac->_getType() != $ftype)) return FALSE; if(($res = $this->_authorize('read', $ac->getId(), $sessid)) !== TRUE) return $res; return TRUE; } /** * Delete existing audio clip * * @param sessid string * @param gunid string * @return boolean or PEAR::error */ function deleteAudioClip($sessid, $gunid) { $ac =& StoredFile::recallByGunid(&$this, $gunid); if(PEAR::isError($ac)) return $ac; if(($res = $this->_authorize('write', $ac->getId(), $sessid)) !== TRUE) return $res; if($ac->isAccessed()){ return PEAR::raiseError( 'LocStor.php: deleteAudioClip: is accessed' ); } $res = $this->deleteFile($ac->getId(), $sessid); if(PEAR::isError($res)) return $res; return TRUE; } /** * Update existing audio clip metadata * * @param sessid string * @param gunid string * @param metadata string, metadata XML string * @return boolean or PEAR::error */ function updateAudioClipMetadata($sessid, $gunid, $metadata) { $ac =& StoredFile::recallByGunid(&$this, $gunid); if(PEAR::isError($ac)) return $ac; if(($res = $this->_authorize('write', $ac->getId(), $sessid)) !== TRUE) return $res; return $ac->replaceMetaData($metadata, 'string'); } /** * Reset storageServer for debugging. * * @param input string */ function resetStorage($input='') { $this->deleteData(); $rootHD = $this->getObjId('root', $this->storId); $this->login('root', $this->config['tmpRootPass']); $s = $this->sessid; include"../tests/sampleData.php"; $res = array(); foreach($sampleData as $k=>$it){ list($media, $meta) = $it; $r = $this->putFile($rootHD, "file".($k+1), $media, $meta, $s); if(PEAR::isError($r)){ return $r; } $res[] = $this->_gunidFromId($r); } $this->logout($this->sessid); return $res; } /*====================================================== playlist methods */ /** * Create a new Playlist metafile. * * @param sessid string, session ID * @param playlistId string, playlist global unique ID * @return string, playlist global unique ID */ function createPlaylist($sessid, $playlistId) { $ex = $this->existsPlaylist($sessid, $playlistId); if(PEAR::isError($ex)){ return $ex; } if($ex){ return PEAR::raiseError( 'LocStor.php: createPlaylist: already exists' ); } $tmpid = uniqid(''); $parid = $this->_getHomeDirId($sessid); if(PEAR::isError($parid)) return $parid; if(($res = $this->_authorize('write', $parid, $sessid)) !== TRUE) return $res; $oid = $this->addObj($tmpid , 'File', $parid); if(PEAR::isError($oid)) return $oid; $ac =& StoredFile::insert(&$this, $oid, '', '', '', 'string', $playlistId, 'playlist' ); if(PEAR::isError($ac)){ $res = $this->removeObj($oid); return $ac; } $res = $this->renameFile($oid, $ac->gunid, $sessid); if(PEAR::isError($res)) return $res; $res = $ac->setState('ready'); if(PEAR::isError($res)) return $res; $res = $ac->setMime('application/smil'); if(PEAR::isError($res)) return $res; return $playlistId; } /** * Open a Playlist metafile for editing. * Open readable URL and mark file as beeing edited. * * @param sessid string, session ID * @param playlistId string, playlist global unique ID * @return struct {url:readable URL for HTTP GET, token:access token} */ function editPlaylist($sessid, $playlistId) { $ex = $this->existsPlaylist($sessid, $playlistId); if(PEAR::isError($ex)){ return $ex; } if(!$ex){ return PEAR::raiseError( 'LocStor.php: editPlaylist: playlist not exists' ); } if($this->_isEdited($playlistId)){ return PEAR::raiseError( 'LocStor.php: editPlaylist: playlist already edited' ); } $ac =& StoredFile::recallByGunid(&$this, $playlistId); if(PEAR::isError($ac)){ return $ac; } $id = $ac->getId(); $res = $this->bsOpenDownload($id, 'metadata'); if(PEAR::isError($res)){ return $res; } $this->_setEditFlag($playlistId, TRUE); return $res; } /** * Store a new Playlist metafile in place of the old one. * * @param sessid string, session ID * @param playlistToken string, playlist access token * @param newPlaylist string, new playlist as XML string * @return boolean */ function savePlaylist($sessid, $playlistToken, $newPlaylist) { $playlistId = $this->bsCloseDownload($playlistToken, $part='metadata'); $ac =& StoredFile::recallByGunid(&$this, $playlistId); if(PEAR::isError($ac)){ return $ac; } $res = $ac->replaceMetaData($newPlaylist, $mdataLoc='string'); if(PEAR::isError($res)){ return $res; } $this->_setEditFlag($playlistId, FALSE); return TRUE; } /** * Delete a Playlist metafile. * * @param sessid string, session ID * @param playlistId string, playlist global unique ID * @return boolean */ function deletePlaylist($sessid, $playlistId) { $ex = $this->existsPlaylist($sessid, $playlistId); if(PEAR::isError($ex)){ return $ex; } if(!$ex){ return PEAR::raiseError( 'LocStor.php: deletePlaylist: playlist not exists' ); } $ac =& StoredFile::recallByGunid(&$this, $playlistId); if(PEAR::isError($ac)) return $ac; if(($res = $this->_authorize('write', $ac->getId(), $sessid)) !== TRUE) return $res; if($this->_isEdited($playlistId)){ return PEAR::raiseError( 'LocStor.php: deletePlaylist: playlist is edited' ); } $res = $this->deleteFile($ac->getId(), $sessid); if(PEAR::isError($res)) return $res; return TRUE; } /** * Access (read) a Playlist metafile. * * @param sessid string, session ID * @param playlistId string, playlist global unique ID * @return struct {url:readable URL for HTTP GET, token:access token */ function accessPlaylist($sessid, $playlistId) { $ex = $this->existsPlaylist($sessid, $playlistId); if(PEAR::isError($ex)){ return $ex; } if(!$ex){ return PEAR::raiseError( 'LocStor.php: accessPlaylist: playlist not found' ); } $id = $this->_idFromGunid($playlistId); if(($res = $this->_authorize('read', $id, $sessid)) !== TRUE) return $res; return $this->bsOpenDownload($id, 'metadata'); } /** * Release the resources obtained earlier by accessPlaylist(). * * @param sessid string, session ID * @param playlistToken string, playlist access token * @return string, playlist ID */ function releasePlaylist($sessid, $playlistToken) { return $this->bsCloseDownload($playlistToken, 'metadata'); } /** * Check whether a Playlist metafile with the given playlist ID exists. * * @param sessid string, session ID * @param playlistId string, playlist global unique ID * @return boolean */ function existsPlaylist($sessid, $playlistId) { return $this->existsFile($sessid, $playlistId, 'playlist'); } /** * Check whether a Playlist metafile with the given playlist ID * is available for editing, i.e., exists and is not marked as * beeing edited. * * @param sessid string, session ID * @param playlistId string, playlist global unique ID * @return boolean */ function playlistIsAvailable($sessid, $playlistId) { $ex = $this->existsPlaylist($sessid, $playlistId); if(PEAR::isError($ex)){ return $ex; } if(!$ex){ return PEAR::raiseError( 'LocStor.php: playlistIsAvailable: playlist not exists' ); } return !$this->_isEdited($playlistId); } /* ---------------------------------------------------- "private" methods */ /** * Check if playlist is marked as edited * * @param playlistId string, playlist global unique ID * @return boolean */ function _isEdited($playlistId) { $state = StoredFile::_getState($playlistId); if($state == 'edited'){ return TRUE; } return FALSE; } /** * Set edit flag * * @param playlistId string, playlist global unique ID * @param val boolean, set/clear of edit flag * @return boolean, previous state */ function _setEditFlag($playlistId, $val=TRUE) { $ac =& StoredFile::recallByGunid(&$this, $playlistId); $state = $ac->_getState(); if($val){ $ac->setState('edited'); } else{ $ac->setState('ready'); } return ($state == 'edited'); } } ?>