From: Banana Date: Wed, 25 Dec 2019 10:45:34 +0000 (+0100) Subject: clean up the local storage directory X-Git-Tag: 2.2_2019-12-27~6 X-Git-Url: http://91.132.146.200/gitweb/?a=commitdiff_plain;h=452afd2c3f3282cbb6836de1160560066cb8402d;p=insipid.git clean up the local storage directory --- diff --git a/ChangeLog b/ChangeLog index c4e293b..678b383 100755 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,4 @@ -version 2.2alpha - Guardian of Ice - (tba) +version 2.2 - Guardian of Ice - (tba) * email import * code cleanups @@ -7,9 +7,10 @@ version 2.2alpha - Guardian of Ice - (tba) * authentication with an extra url now (index.php?m=auth) * management actions shown only if authenticated * small stats overview - * links can now be deleted... + * links can now be deleted * awaiting moderation links can new be moderated * Fixed an error in create sql file + * clean up the local storage via stats page (if authenticated) version 2.1alpha - Guardian of Fire - (2019-09-29) diff --git a/webroot/lib/management.class.php b/webroot/lib/management.class.php index 42f08b0..8188d9a 100644 --- a/webroot/lib/management.class.php +++ b/webroot/lib/management.class.php @@ -511,18 +511,37 @@ class Management { return $ret; } + /** + * get the used disk space for local image storage + * @return false|int + */ public function storageAmount() { $ret = 0; $_storageFolder = ABSOLUTE_PATH.'/'.LOCAL_STORAGE; if(file_exists($_storageFolder) && is_readable($_storageFolder)) { - $ret = Summoner::folderSize(); + $ret = Summoner::folderSize($_storageFolder); } return $ret; } + /** + * empties the local storage directory + * @return bool + */ + public function clearLocalStorage() { + $ret = false; + + $_storageFolder = ABSOLUTE_PATH.'/'.LOCAL_STORAGE; + if(file_exists($_storageFolder) && is_writable($_storageFolder)) { + $ret = Summoner::recursive_remove_directory($_storageFolder,true); + } + + return $ret; + } + /** * Load link by given hash. Do not use Link class directly. diff --git a/webroot/lib/summoner.class.php b/webroot/lib/summoner.class.php index df42dea..6496037 100644 --- a/webroot/lib/summoner.class.php +++ b/webroot/lib/summoner.class.php @@ -284,7 +284,7 @@ class Summoner { } /** - * get as much as possible solcial meta infos from given string + * get as much as possible social meta infos from given string * the string is usually a HTML source * @param string $string * @return array @@ -465,6 +465,11 @@ class Summoner { return $ret; } + /** + * retrieve the folder size with its children of given folder path + * @param $folder + * @return false|int + */ static function folderSize($folder) { $ret = 0; @@ -477,13 +482,82 @@ class Summoner { return $ret; } + /** + * Calculate the given byte size in more human readable format. + * @param $size + * @param string $unit + * @return string + */ static function humanFileSize($size,$unit="") { - if( (!$unit && $size >= 1<<30) || $unit == "GB") - return number_format($size/(1<<30),2)."GB"; - if( (!$unit && $size >= 1<<20) || $unit == "MB") - return number_format($size/(1<<20),2)."MB"; - if( (!$unit && $size >= 1<<10) || $unit == "KB") - return number_format($size/(1<<10),2)."KB"; - return number_format($size)." bytes"; + $ret = number_format($size)." bytes"; + + if((!$unit && $size >= 1<<30) || $unit == "GB") { + $ret = number_format($size / (1 << 30), 2)."GB"; + } + elseif((!$unit && $size >= 1<<20) || $unit == "MB") { + $ret = number_format($size / (1 << 20), 2) . "MB"; + } + elseif( (!$unit && $size >= 1<<10) || $unit == "KB") { + $ret = number_format($size / (1 << 10), 2) . "KB"; + } + + return $ret; } + + /** + * delete and/or empty a directory + * + * $empty = true => empty the directory but do not delete it + * + * @param string $directory + * @param boolean $empty + * @param int $fTime If not false remove files older then this value in sec. + * @return boolean + */ + static function recursive_remove_directory($directory,$empty=false,$fTime=0) { + if(substr($directory,-1) == '/') { + $directory = substr($directory,0,-1); + } + + if(!file_exists($directory) || !is_dir($directory)) { + return false; + } + elseif(!is_readable($directory)) { + return false; + } + else { + $handle = opendir($directory); + + // and scan through the items inside + while (false !== ($item = readdir($handle))) { + if($item[0] != '.') { + $path = $directory.'/'.$item; + + if(is_dir($path)) { + recursive_remove_directory($path); + } + else { + if($fTime !== false && is_int($fTime)) { + $ft = filemtime($path); + $offset = time()-$fTime; + if($ft <= $offset) { + unlink($path); + } + } + else { + unlink($path); + } + } + } + } + closedir($handle); + + if($empty === false) { + if(!rmdir($directory)) { + return false; + } + } + return true; + } + } } diff --git a/webroot/view/stats.inc.php b/webroot/view/stats.inc.php index 041ff41..7369d02 100644 --- a/webroot/view/stats.inc.php +++ b/webroot/view/stats.inc.php @@ -31,8 +31,21 @@ if(Summoner::simpleAuthCheck() === true) { $moderationAmount = $Management->moderationAmount(); } +if(isset($_POST['statsDeleteLocalStorage'])) { + + if($Management->clearLocalStorage() === true) { + + $TemplateData['refresh'] = 'index.php?p=stats'; + } + else { + $submitFeedback['message'] = 'Something went wrong while storage cleaning'; + $submitFeedback['status'] = 'error'; + } +} + $linkAmount = $Management->linkAmount(); $tagAmount = $Management->tagAmount(); $categoryAmount = $Management->categoryAmount(); $localStorageAmount = $Management->storageAmount(); +$localStorageAmount = Summoner::humanFileSize($localStorageAmount); diff --git a/webroot/view/stats.php b/webroot/view/stats.php index f62b99c..fa6e13a 100644 --- a/webroot/view/stats.php +++ b/webroot/view/stats.php @@ -53,30 +53,32 @@
-

Links

+

Links

# of Links:

View all

-

Tags

+

Tags

# of Tags:

View all

-

Categories

+

Categories

# of Categories:

View all

-

Moderation

+

Moderation

# Moderation needed:

View all

-

Local image storage

-

Diskspace used:

-

Delete all

+

Local image storage

+

Diskspace used:

+
+ +