nextcloud/apps/media/ajax/api.php

168 lines
5.0 KiB
PHP
Raw Normal View History

2011-07-22 08:30:52 +04:00
<?php
/**
* ownCloud - media plugin
*
* @author Robin Appelman
* @copyright 2010 Robin Appelman icewind1991@gmail.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
header('Content-type: text/html; charset=UTF-8') ;
//no apps
$RUNTIME_NOAPPS=true;
require_once('../../../lib/base.php');
require_once('../lib_collection.php');
require_once('../lib_scanner.php');
error_reporting(E_ALL); //no script error reporting because of getID3
$arguments=$_POST;
2011-07-22 08:30:52 +04:00
if(!isset($_POST['action']) and isset($_GET['action'])){
$arguments=$_GET;
}
foreach($arguments as &$argument){
$argument=stripslashes($argument);
}
global $CONFIG_DATADIRECTORY;
@ob_clean();
2011-07-22 08:30:52 +04:00
if(!isset($arguments['artist'])){
$arguments['artist']=0;
}
if(!isset($arguments['album'])){
$arguments['album']=0;
}
if(!isset($arguments['search'])){
$arguments['search']='';
}
2011-07-29 23:36:03 +04:00
OC_MEDIA_COLLECTION::$uid=OC_User::getUser();
2011-07-22 08:30:52 +04:00
if($arguments['action']){
switch($arguments['action']){
case 'delete':
unset($_SESSION['collection']);
2011-07-22 08:30:52 +04:00
$path=$arguments['path'];
OC_MEDIA_COLLECTION::deleteSongByPath($path);
2011-07-29 23:36:03 +04:00
$paths=explode(PATH_SEPARATOR,OC_Preferences::getValue(OC_User::getUser(),'media','paths',''));
2011-07-22 08:30:52 +04:00
if(array_search($path,$paths)!==false){
unset($paths[array_search($path,$paths)]);
2011-07-29 23:36:03 +04:00
OC_Preferences::setValue(OC_User::getUser(),'media','paths',implode(PATH_SEPARATOR,$paths));
2011-07-22 08:30:52 +04:00
}
case 'get_collection':
if(!isset($_SESSION['collection'])){
$artists=OC_MEDIA_COLLECTION::getArtists();
foreach($artists as &$artist){
$artist['albums']=OC_MEDIA_COLLECTION::getAlbums($artist['artist_id']);
foreach($artist['albums'] as &$album){
$album['songs']=OC_MEDIA_COLLECTION::getSongs($artist['artist_id'],$album['album_id']);
}
2011-07-22 08:30:52 +04:00
}
$_SESSION['collection']=json_encode($artists);
2011-07-22 08:30:52 +04:00
}
echo $_SESSION['collection'];
2011-07-22 08:30:52 +04:00
break;
case 'scan':
unset($_SESSION['collection']);
OC_DB::beginTransaction();
2011-07-22 08:30:52 +04:00
set_time_limit(0); //recursive scan can take a while
$path=$arguments['path'];
echo OC_MEDIA_SCANNER::scanFolder($path);
OC_DB::commit();
2011-07-22 08:30:52 +04:00
flush();
break;
case 'scanFile':
unset($_SESSION['collection']);
2011-07-22 08:30:52 +04:00
echo (OC_MEDIA_SCANNER::scanFile($arguments['path']))?'true':'false';
break;
case 'get_artists':
echo json_encode(OC_MEDIA_COLLECTION::getArtists($arguments['search']));
break;
case 'get_albums':
echo json_encode(OC_MEDIA_COLLECTION::getAlbums($arguments['artist'],$arguments['search']));
break;
case 'get_songs':
echo json_encode(OC_MEDIA_COLLECTION::getSongs($arguments['artist'],$arguments['album'],$arguments['search']));
break;
case 'get_path_info':
2011-08-02 01:53:01 +04:00
if(OC_Filesystem::file_exists($arguments['path'])){
$songId=OC_MEDIA_COLLECTION::getSongByPath($arguments['path']);
if($songId==0){
unset($_SESSION['collection']);
$songId= OC_MEDIA_SCANNER::scanFile($arguments['path']);
}
if($songId>0){
$song=OC_MEDIA_COLLECTION::getSong($songId);
$song['artist']=OC_MEDIA_COLLECTION::getArtistName($song['song_artist']);
$song['album']=OC_MEDIA_COLLECTION::getAlbumName($song['song_album']);
echo json_encode($song);
}
}
break;
2011-07-22 08:30:52 +04:00
case 'play':
ob_end_clean();
2011-07-29 23:36:03 +04:00
$ftype=OC_Filesystem::getMimeType( $arguments['path'] );
2011-07-22 08:30:52 +04:00
$songId=OC_MEDIA_COLLECTION::getSongByPath($arguments['path']);
OC_MEDIA_COLLECTION::registerPlay($songId);
2011-07-22 08:30:52 +04:00
header('Content-Type:'.$ftype);
2011-08-02 19:02:27 +04:00
// calc an offset of 24 hours
$offset = 3600 * 24;
// calc the string in GMT not localtime and add the offset
$expire = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
//output the HTTP header
2011-08-16 16:34:42 +04:00
header($expire);
2011-08-02 19:02:27 +04:00
header('Cache-Control: max-age=3600, must-revalidate');
2011-07-22 08:30:52 +04:00
header('Pragma: public');
header('Accept-Ranges: bytes');
2011-07-29 23:36:03 +04:00
header('Content-Length: '.OC_Filesystem::filesize($arguments['path']));
2011-08-02 19:02:27 +04:00
$gmt_mtime = gmdate('D, d M Y H:i:s', OC_Filesystem::filemtime($arguments['path']) ) . ' GMT';
header("Last-Modified: " . $gmt_mtime );
2011-07-22 08:30:52 +04:00
2011-07-29 23:36:03 +04:00
OC_Filesystem::readfile($arguments['path']);
2011-07-22 08:30:52 +04:00
exit;
2011-08-02 01:53:01 +04:00
case 'find_music':
echo json_encode(findMusic());
exit;
}
}
function findMusic($path='/'){
$music=array();
$dh=OC_Filesystem::opendir($path);
if($dh){
while($filename=readdir($dh)){
if($filename[0]!='.'){
$file=$path.'/'.$filename;
if(OC_Filesystem::is_dir($file)){
$music=array_merge($music,findMusic($file));
}else{
if(OC_MEDIA_SCANNER::isMusic($filename)){
$music[]=$file;
}
}
}
}
2011-07-22 08:30:52 +04:00
}
2011-08-02 01:53:01 +04:00
return $music;
2011-07-22 08:30:52 +04:00
}
?>