From 481e953cde5abbd749d2bde1cfd7948053efd960 Mon Sep 17 00:00:00 2001 From: Diogo Peralta Cordeiro Date: Thu, 22 Jul 2021 20:57:03 +0100 Subject: [PATCH] [Media] File quota should be triggered by the Core --- plugins/FileQuota/FileQuota.php | 11 +++++------ src/Entity/Attachment.php | 13 +++++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/plugins/FileQuota/FileQuota.php b/plugins/FileQuota/FileQuota.php index 7810e5c06e..c78416eaaf 100644 --- a/plugins/FileQuota/FileQuota.php +++ b/plugins/FileQuota/FileQuota.php @@ -45,7 +45,7 @@ class FileQuota extends Plugin * quotas. Handles per file, per user and per user-month quotas. * Throws on quota violations */ - public function onEnforceQuota(int $filesize) + public function onEnforceQuota(int $user_id, int $filesize) { $file_quota = Common::config('attachments', 'file_quota'); if ($filesize > $file_quota) { @@ -54,7 +54,6 @@ class FileQuota extends Plugin 'Try to upload a smaller version.', ['quota' => $file_quota, 'size' => $filesize])); } - $user = Common::user(); $query = <<getId() . 'file-quota'; - $user_total = Cache::get($cache_key_user_total, fn () => DB::dql($query, ['actor_id' => $user->getId()])[0]['total']); + $cache_key_user_total = 'user-' . $user_id . 'file-quota'; + $user_total = Cache::get($cache_key_user_total, fn () => DB::dql($query, ['actor_id' => $user_id])[0]['total']); Cache::set($cache_key_user_total, $user_total + $filesize); if ($user_total + $filesize > $user_quota) { @@ -80,8 +79,8 @@ END; $monthly_quota = Common::config('attachments', 'monthly_quota'); if ($monthly_quota != false) { - $cache_key_user_monthly = 'user-' . $user->getId() . 'monthly-file-quota'; - $monthly_total = Cache::get($cache_key_user_monthly, fn () => DB::dql($query, ['actor_id' => $user->getId()])[0]['total']); + $cache_key_user_monthly = 'user-' . $user_id . 'monthly-file-quota'; + $monthly_total = Cache::get($cache_key_user_monthly, fn () => DB::dql($query, ['actor_id' => $user_id])[0]['total']); Cache::set($cache_key_user_monthly, $monthly_total + $filesize); if ($monthly_total + $filesize > $monthly_quota) { diff --git a/src/Entity/Attachment.php b/src/Entity/Attachment.php index 43d92af1d5..a67cb8f973 100644 --- a/src/Entity/Attachment.php +++ b/src/Entity/Attachment.php @@ -23,6 +23,7 @@ namespace App\Entity; use App\Core\DB\DB; use App\Core\Entity; +use App\Core\GSFile; use App\Util\Common; use DateTimeInterface; @@ -126,6 +127,18 @@ class Attachment extends Entity return $this->mimetype; } + public function getMimetypeMajor(): ?string + { + $mime = $this->getMimetype(); + return is_null($mime) ? $mime : GSFile::mimetypeMajor($mime); + } + + public function getMimetypeMinor(): ?string + { + $mime = $this->getMimetype(); + return is_null($mime) ? $mime : GSFile::mimetypeMinor($mime); + } + public function setTitle(?string $title): self { $this->title = $title;