Browse Source

Take into account the limit for the volume of uploaded files

Visman 2 years ago
parent
commit
62b335542e

+ 13 - 0
app/Models/Attachment/Attachments.php

@@ -15,6 +15,7 @@ use ForkBB\Core\Image;
 use ForkBB\Models\Manager;
 use ForkBB\Models\Post\Post;
 use ForkBB\Models\PM\PPost;
+use ForkBB\Models\User\User;
 use PDO;
 use RuntimeException;
 
@@ -222,4 +223,16 @@ class Attachments extends Manager
 
         return;
     }
+
+    public function recalculate(User $user)
+    {
+        $vars = [
+            ':uid' => $user->id,
+        ];
+        $query = 'SELECT SUM(size_kb) FROM ::attachments WHERE uid=?i:uid';
+
+        $user->u_up_size_mb = (int) round($this->c->DB->query($query, $vars)->fetchColumn() / 1024);
+
+        $this->c->users->update($user); //???? оптимизировать?
+    }
 }

+ 1 - 0
app/Models/Pages/Admin/Install.php

@@ -1184,6 +1184,7 @@ class Install extends Admin
                 'last_report_id'   => ['INT(10) UNSIGNED', false, 0],
                 'ip_check_type'    => ['TINYINT UNSIGNED', false, 0],
                 'login_ip_cache'   => ['VARCHAR(255)', false, ''],
+                'u_up_size_mb'     => ['INT(10) UNSIGNED', false, 0],
             ],
             'PRIMARY KEY' => ['id'],
             'UNIQUE KEYS' => [

+ 10 - 0
app/Models/Pages/Admin/Update.php

@@ -715,4 +715,14 @@ class Update extends Admin
 
         return null;
     }
+
+    /**
+     * rev.60 to rev.61
+     */
+    protected function stageNumber60(array $args): ?int
+    {
+        $this->c->DB->addField('::users', 'u_up_size_mb', 'INT(10) UNSIGNED', false, 0);
+
+        return null;
+    }
 }

+ 6 - 0
app/Models/Pages/PostValidatorTrait.php

@@ -315,12 +315,14 @@ trait PostValidatorTrait
         }
 
         $result = "\n";
+        $calc   = false;
 
         foreach ($v->attachments as $file) {
             $data = $this->c->attachments->addFile($file);
 
             if (\is_array($data)) {
                 $name = $file->name();
+                $calc = true;
 
                 if ($data['image']) {
                     $result .= "[img]{$data['url']}[/img]\n"; // ={$name}
@@ -330,6 +332,10 @@ trait PostValidatorTrait
             }
         }
 
+        if ($calc) {
+            $this->c->attachments->recalculate($this->user);
+        }
+
         return $result;
     }
 }

+ 2 - 1
app/Models/Rules/Users.php

@@ -154,6 +154,7 @@ class Users extends Rules
             && 1 === $this->user->g_post_links // ???? может быть локальные ссылки разрешить в постах?
             && ! empty($this->user->g_up_ext)
             && $this->user->g_up_size_kb > 0
-            && $this->user->g_up_limit_mb > 0;
+            && $this->user->g_up_limit_mb > 0
+            && $this->user->u_up_size_mb < $this->user->g_up_limit_mb;
     }
 }