transDir = $config['transDir']; } /* ======================================================= upload methods */ /** * Open file upload * * @param sessid string - session id * @param trid string - transport id * @param type string - media|metadata|search * @return array(url string) or error */ function uploadOpen($sessid, $trid, $type) { $file = "{$this->transDir}/$trid"; if(!$fp = fopen($file, 'w')) return PEAR::raiseError( "Archive::uploadOpen: unable to create blank file" ); fclose($fp); $host = $this->config['archiveUrlHost']; $port = $this->config['archiveUrlPort']; $path = $this->config['archiveUrlPath']; $url = "http://$host:$port$path/trans/".basename($file); return array('url'=>$url); } /** * Check uploaded file * * @param sessid string - session id * @param url string * @return array(md5h string, size int, url string) */ function uploadCheck($sessid, $url) { $file = "{$this->transDir}/".basename($url); $md5h = $this->_md5sum($file); $size = filesize($file); return array('md5h'=>$md5h, 'size'=>$size, 'url'=>$url); } /** * Close file upload * * @param sessid string - session id * @param url string * @param type string - media|metadata|search * @param gunid string - global unique id * @return boolean or error */ function uploadClose($sessid, $url, $type, $gunid) { $file = "{$this->transDir}/".basename($url); $res = $this->processUploaded($sessid, $file, $type, $gunid); return $res; } /** * Process uploaded file - insert to the storage * * @param sessid string - session id * @param file string - absolute local pathname * @param type string - media|metadata|search * @param gunid string - global unique id * @return boolean or error */ function processUploaded($sessid, $file, $type, $gunid='X') { switch($type){ case 'media': if(!file_exists($file)) break; $res = $this->storeAudioClip($sessid, $gunid, $file, ''); if(PEAR::isError($res)) return $res; @unlink($file); break; case 'metadata': case 'mdata': if(!file_exists($file)) break; $res = $this->updateAudioClipMetadata($sessid, $gunid, $file); if(PEAR::isError($res)){ // catch valid exception if($res->getCode() == GBERR_FOBJNEX){ $res2 = $this->storeAudioClip($sessid, $gunid, '', $file); if(PEAR::isError($res2)) return $res2; }else return $res; } @unlink($file); break; case 'search': return PEAR::raiseError("Archive::processUploaded: search not implemented"); /* rename($file, $file."_"); $criteria = unserialize(file_get_contents($file_)); $res = $this->searchMetadata($sessid, $criteria); $fh = fopen($file, "w"); fwrite($fh, serialize($res)); fclose($fh); @unlink($file."_"); */ break; default: return PEAR::raiseError("Archive::processUploaded: unknown type ($type)"); break; } return TRUE; } /* ===================================================== download methods */ /** * Open file download * * @param sessid string - session id * @param type string media|metadata|search * @param par string - depends on type */ function downloadOpen($sessid, $type, $par) { switch($type){ case 'media': case 'metadata': $gunid = $par; $res = $this->prepareForTransport('', $gunid, $sessid); if(PEAR::isError($res)) return $res; list($mediaFile, $mdataFile, $gunid) = $res; default: } switch($type){ case 'media': $fname = $mediaFile; break; case 'metadata': $fname = $mdataFile; break; default: } $file = "{$this->transDir}/$fname"; $host = $this->config['archiveUrlHost']; $port = $this->config['archiveUrlPort']; $path = $this->config['archiveUrlPath']; $url = "http://$host:$port$path/trans/$fname"; $md5h = $this->_md5sum($file); return array('url'=>$url, 'md5h'=>$md5h, 'fname'=>$fname); } /** * Close file download * * @param sessid string - session id * @param url string * @return boolean */ function downloadClose($sessid, $url) { $file = "{$this->transDir}/".basename($url); @unlink($file); return TRUE; } /* ==================================================== auxiliary methods */ /** * Returns md5 hash of external file * * @param fpath string - local path to file * @return string */ function _md5sum($fpath) { $md5h = `md5sum $fpath`; $arr = split(' ', $md5h); return $arr[0]; } } ?>