Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions plugins/diskspace/action.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
<?php
require_once( '../../php/util.php' );
require_once( '../../php/settings.php' );
require_once( dirname(__FILE__)."/disk.php" );
eval( FileUtil::getPluginConf( 'diskspace' ) );

if( is_null($partitionDirectory) )
if( !isset($partitionDirectory) || is_null($partitionDirectory) )
{
// If we run locally && we the download directory seems to exists
if ( User::isLocalMode() && rTorrentSettings::get()->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");
14 changes: 14 additions & 0 deletions plugins/diskspace/conf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 %
147 changes: 147 additions & 0 deletions plugins/diskspace/disk.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

require_once( dirname(__FILE__)."/../../php/cache.php" );

class rDiskSpace
{
public $hash = "diskspace.dat";
public $modified = false;

public $disks = array();

static public function load( $paths )
{
$obj = new rDiskSpace();
$cache = new rCache();

if( !$cache->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;
}
}
37 changes: 29 additions & 8 deletions plugins/diskspace/init.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down
7 changes: 6 additions & 1 deletion plugins/diskspace/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).";";
Expand Down