]> _ Git - fluidbook-toolbox.git/commitdiff
wip #5718 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 16 Feb 2023 09:41:19 +0000 (10:41 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 16 Feb 2023 09:41:19 +0000 (10:41 +0100)
app/Console/Kernel.php
app/Jobs/Maintenance/CleanFTP.php [new file with mode: 0644]
app/Models/File.php

index a461fe9061497e1f44226a5f42b661f0a7619776..02144204cf93318194643c4a875eb2e7cc8f810c 100644 (file)
@@ -26,6 +26,7 @@ class Kernel extends \Cubist\Backpack\Console\Kernel
     {
         $schedule->command('queue:prune-failed --hours=24')->daily()->at('03:50');
         $schedule->command('job:dispatch Maintenance\\\\CleanDownloads')->dailyAt('4:00');
+        $schedule->command('job:dispatch Maintenance\\\\CleanFTP')->dailyAt('4:30');
         // $schedule->command('backup:clean')->daily()->at('04:00');
         // $schedule->command('backup:run')->daily()->at('05:00');
 
diff --git a/app/Jobs/Maintenance/CleanFTP.php b/app/Jobs/Maintenance/CleanFTP.php
new file mode 100644 (file)
index 0000000..29cf98a
--- /dev/null
@@ -0,0 +1,15 @@
+<?php
+
+namespace App\Jobs\Maintenance;
+
+use App\Jobs\Base;
+use App\Models\File;
+use Cubist\Util\Files\Files;
+
+class CleanFTP extends Base
+{
+    public function handle()
+    {
+        File::clean();
+    }
+}
index 1df51800f7564dba91431f4a13f394f7370aa6d4..fa01bdd0dd02c91888115debe8d4927bd342c2dd 100644 (file)
@@ -36,6 +36,8 @@ class File extends ToolboxModel
 
     protected $_operations = [DownloadOperation::class];
 
+    public static $basePath = '/application/ftp/';
+
     public function setFields()
     {
         parent::setFields();
@@ -80,7 +82,7 @@ class File extends ToolboxModel
     protected function ___listFilesOfUser($id)
     {
 
-        $path = '/application/ftp/' . $id;
+        $path = self::$basePath . $id;
         if (!file_exists($path) || !is_dir($path)) {
             return [];
         }
@@ -109,7 +111,7 @@ class File extends ToolboxModel
 
             $date = new \DateTime();
             $date->setTimestamp($mtime);
-            $hash = Files::hashFileAttributes($pathname);
+            $hash = self::hash($pathname);
 
             $res[$hash] = [
                 'path' => $pathname,
@@ -129,6 +131,37 @@ class File extends ToolboxModel
         return $res;
     }
 
+    public static function clean()
+    {
+        $limit = time() - (92 * 3600 * 24);
+        $toDelete = File::whereNotIn('ext', ['apk', 'ipa'])->where('mtime', '<', $limit)->orderBy('mtime', 'ASC')->limit(10)->get();
+        foreach ($toDelete as $f) {
+            /** @var $f File */
+            echo "Delete " . $f->path . "\n";
+            $f->delete();
+        }
+    }
+
+    public function onDeleting(): bool
+    {
+        $res = parent::onDeleting();
+        unlink($this->path);
+        self::touchChangeFile();
+
+        return $res;
+    }
+
+    public static function touchChangeFile()
+    {
+        touch(self::$basePath . '__LAST_CHANGE');
+    }
+
+
+    public static function hash($pathname)
+    {
+        return sha1('!!//' . trim(str_replace(self::$basePath, '', $pathname), '/') . '||--');
+    }
+
     public function showPrimaryColumn()
     {
         return false;