Add basic file processor for avatar
authorCyperghost <olaf_schmitz_1@t-online.de>
Wed, 6 Nov 2024 10:02:59 +0000 (11:02 +0100)
committerCyperghost <olaf_schmitz_1@t-online.de>
Wed, 6 Nov 2024 10:02:59 +0000 (11:02 +0100)
com.woltlab.wcf/objectType.xml
wcfsetup/install/files/lib/system/file/processor/UserAvatarFileProcessor.class.php [new file with mode: 0644]

index 9472239439d60fe5d12a6d8a4d9f13a4a1160e06..567b539407f1a1406b4d3684955afeebb94b133c 100644 (file)
                        <definitionname>com.woltlab.wcf.file</definitionname>
                        <classname>wcf\system\file\processor\AttachmentFileProcessor</classname>
                </type>
+               <type>
+                       <name>com.woltlab.wcf.user.avatar</name>
+                       <definitionname>com.woltlab.wcf.file</definitionname>
+                       <classname>wcf\system\file\processor\UserAvatarFileProcessor</classname>
+               </type>
                <!-- deprecated -->
                <type>
                        <name>com.woltlab.wcf.page.controller</name>
diff --git a/wcfsetup/install/files/lib/system/file/processor/UserAvatarFileProcessor.class.php b/wcfsetup/install/files/lib/system/file/processor/UserAvatarFileProcessor.class.php
new file mode 100644 (file)
index 0000000..0421ca2
--- /dev/null
@@ -0,0 +1,138 @@
+<?php
+
+namespace wcf\system\file\processor;
+
+use wcf\data\file\File;
+use wcf\data\file\thumbnail\FileThumbnail;
+use wcf\data\user\avatar\UserAvatar;
+use wcf\data\user\User;
+use wcf\system\cache\runtime\UserRuntimeCache;
+use wcf\system\WCF;
+
+/**
+ * @author      Olaf Braun
+ * @copyright   2001-2024 WoltLab GmbH
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since       6.2
+ */
+final class UserAvatarFileProcessor extends AbstractFileProcessor
+{
+    #[\Override]
+    public function getObjectTypeName(): string
+    {
+        return 'com.woltlab.wcf.user.avatar';
+    }
+
+    #[\Override]
+    public function getAllowedFileExtensions(array $context): array
+    {
+        return \explode("\n", WCF::getSession()->getPermission('user.profile.avatar.allowedFileExtensions'));
+    }
+
+    #[\Override]
+    public function canAdopt(File $file, array $context): bool
+    {
+        // TODO
+        return true;
+    }
+
+    #[\Override]
+    public function adopt(File $file, array $context): void
+    {
+        // TODO
+    }
+
+    #[\Override]
+    public function acceptUpload(string $filename, int $fileSize, array $context): FileProcessorPreflightResult
+    {
+        // TODO
+        return FileProcessorPreflightResult::Passed;
+    }
+
+    #[\Override]
+    public function canDelete(File $file): bool
+    {
+        // TODO
+        return true;
+    }
+
+    #[\Override]
+    public function canDownload(File $file): bool
+    {
+        // TODO
+        return true;
+    }
+
+    #[\Override]
+    public function getMaximumCount(array $context): ?int
+    {
+        // TODO
+        return null;
+    }
+
+    #[\Override]
+    public function getThumbnailFormats(): array
+    {
+        return [
+            // TODO did we need thumbnails for sizes less then 128x128?
+            // 96x96
+            // 64x64
+            // 48x48
+            // 32x32
+            new ThumbnailFormat('128', UserAvatar::AVATAR_SIZE, UserAvatar::AVATAR_SIZE, false),
+            new ThumbnailFormat('256', UserAvatar::AVATAR_SIZE_2X, UserAvatar::AVATAR_SIZE_2X, false),
+        ];
+    }
+
+    #[\Override]
+    public function adoptThumbnail(FileThumbnail $thumbnail): void
+    {
+        // TODO
+    }
+
+    #[\Override]
+    public function delete(array $fileIDs, array $thumbnailIDs): void
+    {
+        // TODO
+    }
+
+    #[\Override]
+    public function countExistingFiles(array $context): ?int
+    {
+        // TODO
+    }
+
+    #[\Override]
+    public function getMaximumSize(array $context): ?int
+    {
+        /**
+         * Reject the file if it is larger than 750 kB after resizing. A worst-case
+         * completely-random 128x128 PNG is around 35 kB and JPEG is around 50 kB.
+         *
+         * Animated GIFs can be much larger depending on the length of animation,
+         * 750 kB seems to be a reasonable upper bound for anything that can be
+         * considered reasonable with regard to "distraction" and mobile data
+         * volume.
+         */
+        return 750_000;
+    }
+
+    #[\Override]
+    public function getImageCropperConfiguration(): ?ImageCropperConfiguration
+    {
+        return ImageCropperConfiguration::createExact(
+            new ImageCropSize(UserAvatar::AVATAR_SIZE, UserAvatar::AVATAR_SIZE),
+            new ImageCropSize(UserAvatar::AVATAR_SIZE_2X, UserAvatar::AVATAR_SIZE_2X)
+        );
+    }
+
+    private function getUser(array $context): ?User
+    {
+        $userID = $context['objectID'] ?? null;
+        if ($userID === null) {
+            return null;
+        }
+
+        return UserRuntimeCache::getInstance()->getObject($userID);
+    }
+}