Add `getPathname()` to simplify the file access
authorAlexander Ebert <ebert@woltlab.com>
Sun, 14 Apr 2024 11:10:05 +0000 (13:10 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Sat, 8 Jun 2024 10:19:38 +0000 (12:19 +0200)
wcfsetup/install/files/lib/action/FileDownloadAction.class.php
wcfsetup/install/files/lib/data/file/File.class.php
wcfsetup/install/files/lib/data/file/FileEditor.class.php
wcfsetup/install/files/lib/data/file/temporary/FileTemporary.class.php
wcfsetup/install/files/lib/system/file/processor/FileProcessor.class.php

index b7ded2a8fbfe7b341a9dff835fdd2239a62dcd8d..f2d1c7d66348ff7ec062319b5d9121eecbf5f600 100644 (file)
@@ -48,7 +48,7 @@ final class FileDownloadAction implements RequestHandlerInterface
             throw new PermissionDeniedException();
         }
 
-        $filename = $file->getPath() . $file->getSourceFilename();
+        $filename = $file->getPathname();
         $response = new Response(
             new Stream($filename),
         );
index 19fbc329955e899ea02e164eebbbcab1766c35cd..a0442b08974297b505579d10cca2e601232d1480 100644 (file)
@@ -30,6 +30,15 @@ class File extends DatabaseObject
     /** @var array<string, FileThumbnail> */
     private array $thumbnails = [];
 
+    public function getFilename(): string
+    {
+        return \sprintf(
+            '%d-%s.bin',
+            $this->fileID,
+            $this->fileHash,
+        );
+    }
+
     public function getPath(): string
     {
         $folderA = \substr($this->fileHash, 0, 2);
@@ -42,13 +51,9 @@ class File extends DatabaseObject
         );
     }
 
-    public function getSourceFilename(): string
+    public function getPathname(): string
     {
-        return \sprintf(
-            '%d-%s.bin',
-            $this->fileID,
-            $this->fileHash,
-        );
+        return $this->getPath() . $this->getFilename();
     }
 
     public function getLink(): string
index e0fafc292250f907745eb0e60a760c042ed92229..66be3cc768fd98de1ccbb64bd75487d80241e7a2 100644 (file)
@@ -25,7 +25,8 @@ class FileEditor extends DatabaseObjectEditor
 
     public static function createFromTemporary(FileTemporary $fileTemporary): File
     {
-        $mimeType = FileUtil::getMimeType($fileTemporary->getPath() . $fileTemporary->getFilename());
+        $pathname = $fileTemporary->getPathname();
+        $mimeType = FileUtil::getMimeType($pathname);
         $isImage = match ($mimeType) {
             'image/gif' => true,
             'image/jpeg' => true,
@@ -36,7 +37,7 @@ class FileEditor extends DatabaseObjectEditor
 
         $width = $height = null;
         if ($isImage) {
-            [$width, $height] = \getimagesize($fileTemporary->getPath() . $fileTemporary->getFilename());
+            [$width, $height] = \getimagesize($pathname);
         }
 
         $fileAction = new FileAction([], 'create', ['data' => [
@@ -57,8 +58,8 @@ class FileEditor extends DatabaseObjectEditor
         }
 
         \rename(
-            $fileTemporary->getPath() . $fileTemporary->getFilename(),
-            $filePath . $file->getSourceFilename()
+            $pathname,
+            $filePath . $file->getFilename()
         );
 
         return $file;
index 25fe09cb921a5bb92d8c1c4c7f05f94b15106695..a7778a47d914d6a13f75a6f9abb9b2858b452d86 100644 (file)
@@ -60,6 +60,11 @@ class FileTemporary extends DatabaseObject
         );
     }
 
+    public function getPathname(): string
+    {
+        return $this->getPath() . $this->getFilename();
+    }
+
     public function getContext(): array
     {
         return JSON::decode($this->context);
index 54431683770d0426ceb84d39099676689cd0dff0..f0ab87ecfe9dc4bc6ff21b76223bef9393f91921 100644 (file)
@@ -100,7 +100,7 @@ final class FileProcessor extends SingletonFactory
 
             if ($imageAdapter === null) {
                 $imageAdapter = ImageHandler::getInstance()->getAdapter();
-                $imageAdapter->loadFile($file->getPath() . $file->getSourceFilename());
+                $imageAdapter->loadFile($file->getPathname());
             }
 
             \assert($imageAdapter instanceof ImageAdapter);