]> 91.132.146.200 Git - insipid.git/commitdiff
clean up the local storage directory
authorBanana <banana@mirage>
Wed, 25 Dec 2019 10:45:34 +0000 (11:45 +0100)
committerBanana <banana@mirage>
Wed, 25 Dec 2019 10:45:34 +0000 (11:45 +0100)
ChangeLog
webroot/lib/management.class.php
webroot/lib/summoner.class.php
webroot/view/stats.inc.php
webroot/view/stats.php

index c4e293b02cef32816c1a24d396229dc6ad63270c..678b3833a62b9a9b5255f2b69f3450f65de75e69 100755 (executable)
--- 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)
 
index 42f08b024eff85c2234dfa7a7de775a05aaa2579..8188d9ace937d8e8fd9871ee08e3da0d620a1fc2 100644 (file)
@@ -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.
index df42deab6f76ec1ccffec5cd979f0ee6f7c3f538..6496037455019af3fef51ab32f443c73e117e290 100644 (file)
@@ -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;
+        }
+    }
 }
index 041ff412ea9d6d880716e17f529bb11365a43dee..7369d02ceeafd6b76f9b587a2774098beba2b6ad 100644 (file)
@@ -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);
 
index f62b99ce6f41f236a285fff5e320ced8afd736f1..fa6e13ae3607b5f01611680da9a4682521fc419c 100644 (file)
 <section class="section">
        <div class="columns is-multiline">
                <div class="column is-one-quarter">
-                       <h3 class="is-size-3">Links</h3>
+                       <h4 class="is-size-4">Links</h4>
                        <p># of Links: <?php echo $linkAmount; ?></p>
                        <p><a href="index.php?p=overview&m=all">View all</a></p>
                </div>
                <div class="column is-one-quarter">
-                       <h3 class="is-size-3">Tags</h3>
+                       <h4 class="is-size-4">Tags</h4>
                        <p># of Tags: <?php echo $tagAmount; ?></p>
                        <p><a href="index.php?p=overview&m=tag">View all</a></p>
                </div>
                <div class="column is-one-quarter">
-                       <h3 class="is-size-3">Categories</h3>
+                       <h4 class="is-size-4">Categories</h4>
                        <p># of Categories: <?php echo $categoryAmount; ?></p>
                        <p><a href="index.php?p=overview&m=category">View all</a></p>
                </div>
                <?php if($_displayEditButton === true) { ?>
                <div class="column is-one-quarter">
-                       <h3 class="is-size-3">Moderation</h3>
+                       <h4 class="is-size-4">Moderation</h4>
                        <p># Moderation needed: <?php echo $moderationAmount; ?></p>
                        <p><a href="index.php?p=overview&m=awm">View all</a></p>
                </div>
                <div class="column is-one-quarter">
-                       <h3 class="is-size-3">Local image storage</h3>
-                       <p>Diskspace used: <?php echo $moderationAmount; ?></p>
-                       <p><a href="index.php?p=overview&m=category">Delete all</a></p>
+                       <h4 class="is-size-4">Local image storage</h4>
+                       <p>Diskspace used: <?php echo $localStorageAmount; ?></p>
+            <form method="post">
+                <input type="submit" class="button is-info is-small" value="Delete all" name="statsDeleteLocalStorage">
+            </form>
                </div>
                <?php } ?>
        </div>