diff --git a/plugins/diskspace/action.php b/plugins/diskspace/action.php index 23922e338..b8e25b1a1 100644 --- a/plugins/diskspace/action.php +++ b/plugins/diskspace/action.php @@ -1,28 +1,45 @@ linkExist && file_exists(rTorrentSettings::get()->directory) ) { - $partitionDirectory = rTorrentSettings::get()->directory; // Then we can show the disk space of the download directory + $partitionDirectory = array( rTorrentSettings::get()->directory ); // Then we can show the disk space of the download directory } else { - $partitionDirectory = &$topDirectory; // Else, we show $topDirectory by default as fallback + $partitionDirectory = array( $topDirectory); // Else, we show $topDirectory by default as fallback } } - $ret = array - ( + elseif( !is_array($partitionDirectory) ) + { + $partitionDirectory = array($partitionDirectory); + } + + $diskSpace = rDiskSpace::load($partitionDirectory); + + $current = $diskSpace->updateOldest(); + + $ret = array( "total" => 0, - "free" => 0 + "free" => 0, + "path" => "", + "updated" => 0, + "all" => array() ); - if( is_dir($partitionDirectory) ) + + if( $current ) { - $ret["total"] = disk_total_space($partitionDirectory); - $ret["free"] = disk_free_space($partitionDirectory); + $ret["total"] = $current["total"]; + $ret["free"] = $current["free"]; + $ret["path"] = $current["path"]; + $ret["updated"] = $current["updated"]; } + + $ret["all"] = $diskSpace->getAll(); CachedEcho::send(JSON::safeEncode($ret),"application/json"); diff --git a/plugins/diskspace/conf.php b/plugins/diskspace/conf.php index 1e0b36e81..1a7d45c06 100644 --- a/plugins/diskspace/conf.php +++ b/plugins/diskspace/conf.php @@ -4,4 +4,18 @@ $notifySpaceLimit = 512; // in Mb $partitionDirectory = null; // if null, then we will check rtorrent download directory (or $topDirectory if rtorrent is unavailable) // otherwise, set this to the absolute path for checked partition. +// To have multiple mountpoint checks add them in array like this +//$partitionDirectory = [ +// '/mnt/disk1', +// '/mnt/disk2', +// '/mnt/disk3' +//]; +// +//or like this +// +//$partitionDirectory = array( +// '/mnt/disk1', +// '/mnt/disk2', +// '/mnt/disk3' +//); $freeBytesInMeter = false; // show free space instead of % diff --git a/plugins/diskspace/disk.php b/plugins/diskspace/disk.php new file mode 100644 index 000000000..e78f72e06 --- /dev/null +++ b/plugins/diskspace/disk.php @@ -0,0 +1,147 @@ +get($obj) ) + { + $obj->initialize($paths); + $obj->store(); + } + else + { + $obj->sync($paths); + } + + return $obj; + } + + public function initialize( $paths ) + { + $this->disks = array(); + + foreach( $paths as $path ) + { + $this->disks[] = array( + 'path' => $path, + 'total' => 0, + 'free' => 0, + 'updated' => 0 + ); + } + } + + public function sync( $paths ) + { + $current = array(); + + foreach( $this->disks as $disk ) + $current[$disk['path']] = $disk; + + $newDisks = array(); + + foreach( $paths as $path ) + { + if( isset($current[$path]) ) + $newDisks[] = $current[$path]; + else + { + $newDisks[] = array( + 'path' => $path, + 'total' => 0, + 'free' => 0, + 'updated' => 0 + ); + } + } + + $this->disks = $newDisks; + $this->store(); + } + + public function store() + { + $cache = new rCache(); + return $cache->set($this); + } + + public function getOldestIndex() + { + $oldestIndex = 0; + $oldestTime = 9999999999; + + foreach( $this->disks as $index => $disk ) + { + if( $disk['updated'] < $oldestTime ) + { + $oldestTime = $disk['updated']; + $oldestIndex = $index; + } + } + + return $oldestIndex; + } + + public function updateOldest() + { + if( empty($this->disks) ) + return null; + + $index = $this->getOldestIndex(); + + $path = $this->disks[$index]['path']; + + if( is_dir($path) ) + { + $this->disks[$index]['total'] = intval(@disk_total_space($path)); + $this->disks[$index]['free'] = intval(@disk_free_space($path)); + } + else + { + $this->disks[$index]['total'] = 0; + $this->disks[$index]['free'] = 0; + } + + $this->disks[$index]['updated'] = time(); + + $this->store(); + + return $this->disks[$index]; + } + + public function getNewest() + { + if( empty($this->disks) ) + return null; + + $newest = null; + $newestTime = 0; + + foreach( $this->disks as $disk ) + { + if( $disk['updated'] >= $newestTime ) + { + $newestTime = $disk['updated']; + $newest = $disk; + } + } + + return $newest; + } + + public function getAll() + { + return $this->disks; + } +} diff --git a/plugins/diskspace/init.js b/plugins/diskspace/init.js index 011c65752..b19e5e8ac 100644 --- a/plugins/diskspace/init.js +++ b/plugins/diskspace/init.js @@ -1,7 +1,7 @@ plugin.loadLang(); plugin.loadMainCSS(); -plugin.setValue = function( full, free ) +plugin.setValue = function( full, free, all ) { var used = full-free; var percent = iv(full ? used/full*100 : 0); @@ -11,12 +11,33 @@ plugin.setValue = function( full, free ) visibility: !percent ? "hidden" : "visible" } ); if(plugin.freeBytesInMeter) $("#meter-disk-text").text(theConverter.bytes(free)); else $("#meter-disk-text").text(percent+'%'); - $("#meter-disk-pane").prop("title", - theUILang.diskUsage - .replace(/%USED%/, theConverter.bytes(used)) - .replace(/%TOTAL%/, theConverter.bytes(full)) - .replace(/%FREE%/, theConverter.bytes(free)) - ); + var tooltip = []; + if(all && all.length) + { + for(var i = 0; i < all.length; i++) + { + var disk = all[i]; + var dUsed = disk.total - disk.free; + var dPercent = disk.total ? Math.round(dUsed / disk.total * 100) : 0; + tooltip.push( + disk.path + "\n" + + theUILang.diskUsage + .replace(/%USED%/, theConverter.bytes(dUsed)) + .replace(/%TOTAL%/, theConverter.bytes(disk.total)) + .replace(/%FREE%/, theConverter.bytes(disk.free)) + ); + } + } + else + { + tooltip.push( + theUILang.diskUsage + .replace(/%USED%/, theConverter.bytes(used)) + .replace(/%TOTAL%/, theConverter.bytes(full)) + .replace(/%FREE%/, theConverter.bytes(free)) + ); + } + $("#meter-disk-pane").prop("title", tooltip.join("\n------------------\n")); if($.noty && plugin.allStuffLoaded) { @@ -68,7 +89,7 @@ plugin.init = function() cache: false, success : function(data) { - plugin.setValue( data.total, data.free ); + plugin.setValue( data.total, data.free, data.all ); }, complete : function(jqXHR, textStatus) { diff --git a/plugins/diskspace/init.php b/plugins/diskspace/init.php index 4022e0538..83d4491e5 100644 --- a/plugins/diskspace/init.php +++ b/plugins/diskspace/init.php @@ -14,10 +14,15 @@ $partitionDirectory = &$topDirectory; // Else, we show $topDirectory by default as fallback } } +$checkDirectory = $partitionDirectory; +if (is_array($partitionDirectory)) + $checkDirectory = reset($partitionDirectory); if(!function_exists('disk_total_space') || !function_exists('disk_free_space') || - (disk_total_space($partitionDirectory)===false) || (disk_free_space($partitionDirectory)===false)) + (disk_total_space($checkDirectory)===false) || (disk_free_space($checkDirectory)===false)) +{ $jResult .= "plugin.disable();"; +} else { $jResult.="plugin.interval = ".$diskUpdateInterval."; plugin.notifySpaceLimit = ".($notifySpaceLimit*1024*1024).";";