Skip to content

Commit

Permalink
Update storage.php
Browse files Browse the repository at this point in the history
Add error checking, prevents division by zero errors and handle issues where disk space information cannot be retrieved
  • Loading branch information
curtishall authored Jan 24, 2025
1 parent da83544 commit 651bdbf
Showing 1 changed file with 42 additions and 20 deletions.
62 changes: 42 additions & 20 deletions www/ajax/storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,51 @@ public function __construct(){
$this->chAccess('admin');
}

public function getData()
{
$this->setView('ajax.storage');

$locations = data::query("SELECT * FROM Storage");
foreach(array_keys($locations) as $id) {
$path = database::escapeString($locations[$id]['path']);
$max_thresh = database::escapeString($locations[$id]['max_thresh']);
$available_space = (disk_free_space($path) - disk_total_space($path) * (1.0 - $max_thresh/100) ) >> 20;
if ($available_space < 0)
$available_space = 0;
$query = "SELECT ({$available_space}) * period / size AS result FROM (".
"SELECT SUM(CASE WHEN end < start THEN 0 ELSE end - start END".
") period, SUM(size) DIV 1048576 size".
" FROM Media WHERE filepath LIKE '{$path}%') q";
$result = data::query($query);
$locations[$id]['record_time'] = $result[0]['result'];
$locations[$id]['used_percent'] = ceil((disk_total_space($path) - disk_free_space($path)) / disk_total_space($path) * 100);
}
public function getData()
{
$this->setView('ajax.storage');
$storage_check = $this->ctl('storagecheck');

$this->view->locations = $locations;
$locations = data::query("SELECT * FROM Storage");
foreach(array_keys($locations) as $id) {
$path = database::escapeString($locations[$id]['path']);
$max_thresh = database::escapeString($locations[$id]['max_thresh']);

// Check if we can access the directory
$dir_status = $storage_check->change_directory_permissions($path);

// Get disk space info with error checking
$free_space = disk_free_space($path);
$total_space = disk_total_space($path);

if ($free_space === false || $total_space === false) {
// Handle error case
$locations[$id]['record_time'] = 0;
$locations[$id]['used_percent'] = 0;
continue;
}

$available_space = ($free_space - $total_space * (1.0 - $max_thresh/100) ) >> 20;
if ($available_space < 0)
$available_space = 0;

$query = "SELECT ({$available_space}) * period / size AS result FROM (".
"SELECT SUM(CASE WHEN end < start THEN 0 ELSE end - start END".
") period, SUM(size) DIV 1048576 size".
" FROM Media WHERE filepath LIKE '{$path}%') q";
$result = data::query($query);
$locations[$id]['record_time'] = $result[0]['result'];

if ($total_space > 0) {
$locations[$id]['used_percent'] = ceil(($total_space - $free_space) / $total_space * 100);
} else {
$locations[$id]['used_percent'] = 0;
}
}

$this->view->locations = $locations;
}

public function postData()
{
$storage_check = $this->ctl('storagecheck');
Expand Down

0 comments on commit 651bdbf

Please sign in to comment.