From ab142ab52d67dd1edd51c64e73d250290ce6a224 Mon Sep 17 00:00:00 2001 From: Diogo Peralta Cordeiro Date: Thu, 19 Aug 2021 19:18:10 +0100 Subject: [PATCH] [FileQuota] Update plugin to respect the new entities --- components/Posting/Posting.php | 8 +++- plugins/FileQuota/FileQuota.php | 77 ++++++++++++++++++++++----------- 2 files changed, 59 insertions(+), 26 deletions(-) diff --git a/components/Posting/Posting.php b/components/Posting/Posting.php index 26f197f87a..fb4833d378 100644 --- a/components/Posting/Posting.php +++ b/components/Posting/Posting.php @@ -26,6 +26,7 @@ use App\Core\DB\DB; use App\Core\Event; use App\Core\Form; use App\Core\GSFile; +use App\Util\Exception\ClientException; use function App\Core\I18n\_m; use App\Core\Modules\Component; use App\Entity\Attachment; @@ -119,7 +120,12 @@ END; $processed_attachments = []; foreach ($attachments as $f) { // where $f is a Symfony\Component\HttpFoundation\File\UploadedFile $filesize = $f->getSize(); - Event::handle('EnforceQuota', [$actor_id, $filesize]); + $max_file_size = Common::config('attachments', 'file_quota'); + if ($max_file_size < $filesize) { + throw new ClientException(_m('No file may be larger than {quota} bytes and the file you sent was {size} bytes. ' . + 'Try to upload a smaller version.', ['quota' => $max_file_size, 'size' => $filesize])); + } + Event::handle('EnforceUserFileQuota', [$filesize, $actor_id]); $processed_attachments[] = [GSFile::sanitizeAndStoreFileAsAttachment($f), $f->getClientOriginalName()]; } diff --git a/plugins/FileQuota/FileQuota.php b/plugins/FileQuota/FileQuota.php index 8a906f8b81..58b3c728e8 100644 --- a/plugins/FileQuota/FileQuota.php +++ b/plugins/FileQuota/FileQuota.php @@ -24,9 +24,11 @@ namespace Plugin\FileQuota; use App\Core\Cache; use App\Core\DB\DB; use App\Core\Event; +use function App\Core\I18n\_m; use App\Core\Modules\Plugin; use App\Util\Common; use App\Util\Exception\ClientException; +use App\Util\Exception\ServerException; /** * Check attachment file size quotas @@ -34,55 +36,57 @@ use App\Util\Exception\ClientException; * @package GNUsocial * @ccategory Attachment * - * @authir Hugo Sales - * + * @author Hugo Sales * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later */ class FileQuota extends Plugin { + public function version(): string + { + return '1.0.0'; + } + /** - * Check file size to ensure it repects configured file size + * Check file size to ensure it respects configured file size * quotas. Handles per file, per user and per user-month quotas. * Throws on quota violations + * + * @param int $filesize + * @param int $user_id + * + * @throws ClientException + * @throws ServerException + * + * @return bool */ - public function onEnforceQuota(int $user_id, int $filesize): bool + public function onEnforceUserFileQuota(int $filesize, int $user_id): bool { - return Event::stop; - /* - $file_quota = Common::config('attachments', 'file_quota'); - if ($filesize > $file_quota) { - // TRANS: Message given if an upload is larger than the configured maximum. - throw new ClientException(_m('No file may be larger than {quota} bytes and the file you sent was {size} bytes. ' . - 'Try to upload a smaller version.', ['quota' => $file_quota, 'size' => $filesize])); - } - $query = << DB::dql($query, ['actor_id' => $user_id])[0]['total']); Cache::set($cache_key_user_total, $user_total + $filesize); - if ($user_total + $filesize > $user_quota) { + if ($user_total + $filesize > $max_user_quota) { // TRANS: Message given if an upload would exceed user quota. - throw new ClientException(_m('A file this large would exceed your user quota of {quota} bytes.', ['quota' => $user_quota])); + throw new ClientException(_m('A file this large would exceed your user quota of {quota} bytes.', ['quota' => $max_user_quota])); } } $query .= ' AND MONTH(at.modified) = MONTH(CURRENT_DATE())' - . ' AND YEAR(at.modified) = YEAR(CURRENT_DATE())'; + . ' AND YEAR(at.modified) = YEAR(CURRENT_DATE())'; $monthly_quota = Common::config('attachments', 'monthly_quota'); - if ($monthly_quota != false) { - $cache_key_user_monthly = 'user-' . $user_id . 'monthly-file-quota'; + if ($monthly_quota !== false) { // If not disabled + $cache_key_user_monthly = "FileQuota-monthly-user-{$user_id}"; $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); @@ -90,6 +94,29 @@ END; // TRANS: Message given if an upload would exceed user quota. throw new ClientException(_m('A file this large would exceed your monthly quota of {quota} bytes.', ['quota' => $monthly_quota])); } - }*/ + } + + return Event::next; + } + + /** + * Event raised when GNU social polls the plugin for information about it. + * Adds this plugin's version information to $versions array + * + * @param &$versions array inherited from parent + * + * @return bool true hook value + */ + public function onPluginVersion(array &$versions): bool + { + $versions[] = [ + 'name' => 'FileQuota', + 'version' => $this->version(), + 'author' => 'Hugo Sales', + 'homepage' => GNUSOCIAL_PROJECT_URL, + 'description' => // TRANS: Plugin description. + _m('Plugin to manage user quotas.'), + ]; + return Event::next; } }