]> source.dussan.org Git - nextcloud-server.git/commitdiff
correctly update the collection when music files are moved around
authorRobin Appelman <icewind1991@gmail.com>
Sat, 24 Sep 2011 23:34:19 +0000 (01:34 +0200)
committerRobin Appelman <icewind1991@gmail.com>
Sat, 24 Sep 2011 23:34:55 +0000 (01:34 +0200)
apps/media/ajax/api.php
apps/media/lib_collection.php
apps/media/lib_media.php

index 31ff33bf03a4209173018421e05a1bd30765a7bc..c5909e4c78b30264d531e55f9e5d854124df4e9e 100644 (file)
@@ -134,7 +134,7 @@ if($arguments['action']){
        }
 }
 
-function findMusic($path='/'){
+function findMusic($path=''){
        $music=array();
        $dh=OC_Filesystem::opendir($path);
        if($dh){
index 273ea2494f8589a328220de93ac12619bec7e2f6..82c4afedd0a1c000f4ae2295316e3bca89e1d7d1 100644 (file)
@@ -251,10 +251,12 @@ class OC_MEDIA_COLLECTION{
                if($name=='' or $path==''){
                        return 0;
                }
-               $uid=$_SESSION['user_id'];
+               $uid=OC_User::getUser();
                //check if the song is already in the database
                $songId=self::getSongId($name,$artist,$album);
                if($songId!=0){
+                       $songInfo=self::getSong($songId);
+                       self::moveSong($songInfo['song_path'],$path);
                        return $songId;
                }else{
                        if(!isset(self::$queries['addsong'])){
@@ -357,13 +359,23 @@ class OC_MEDIA_COLLECTION{
         */
        public static function getSongByPath($path){
                $query=OC_DB::prepare("SELECT song_id FROM *PREFIX*media_songs WHERE song_path = ?");
-               $result=$query->execute(array($path))->fetchAll();
-               if(count($result)>0){
-                       return $result[0]['song_id'];
+               $result=$query->execute(array($path));
+               if($row=$result->fetchRow()){
+                       return $row['song_id'];
                }else{
                        return 0;
                }
        }
+       
+       /**
+        * set the path of a song
+        * @param string $oldPath
+        * @param string $newPath
+        */
+       public static function moveSong($oldPath,$newPath){
+               $query=OC_DB::prepare("UPDATE *PREFIX*media_songs SET song_path = ? WHERE song_path = ?");
+               $query->execute(array($newPath,$oldPath));
+       }
 }
 
 ?>
\ No newline at end of file
index 1d8321a774cf5de7ac1f4939b31b960c57843d9e..7a666be8c274f0a1ea7d69a56eb5616d414f978c 100644 (file)
@@ -30,6 +30,9 @@ OC_Hook::connect('OC_Filesystem','post_write','OC_MEDIA','updateFile');
 //listen for file deletions to clean the database if a song is deleted
 OC_Hook::connect('OC_Filesystem','delete','OC_MEDIA','deleteFile');
 
+//list for file moves to update the database
+OC_Hook::connect('OC_Filesystem','post_rename','OC_MEDIA','moveFile');
+
 class OC_MEDIA{
        /**
         * get the sha256 hash of the password needed for ampache
@@ -61,6 +64,7 @@ class OC_MEDIA{
                        $path=substr($path,1);
                }
                $path='/'.$path;
+               error_log("$path was updated");
                OC_MEDIA_SCANNER::scanFile($path);
        }
 
@@ -72,6 +76,11 @@ class OC_MEDIA{
                require_once 'lib_collection.php';
                OC_MEDIA_COLLECTION::deleteSongByPath($path);
        }
+
+       public static function moveFile($params){
+               require_once 'lib_collection.php';
+               OC_MEDIA_COLLECTION::moveSong($params['oldpath'],$params['newpath']);
+       }
 }
 
 class OC_MediaSearchProvider extends OC_Search_Provider{