Only serve a small set of supported image format statically
authorAlexander Ebert <ebert@woltlab.com>
Wed, 25 Sep 2024 09:39:48 +0000 (11:39 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Wed, 25 Sep 2024 09:39:48 +0000 (11:39 +0200)
wcfsetup/install/files/acp/update_com.woltlab.wcf_6.1.0_beta_2_migrateStaticFiles.php [new file with mode: 0644]
wcfsetup/install/files/lib/data/file/File.class.php

diff --git a/wcfsetup/install/files/acp/update_com.woltlab.wcf_6.1.0_beta_2_migrateStaticFiles.php b/wcfsetup/install/files/acp/update_com.woltlab.wcf_6.1.0_beta_2_migrateStaticFiles.php
new file mode 100644 (file)
index 0000000..aada69b
--- /dev/null
@@ -0,0 +1,80 @@
+<?php
+
+/**
+ * Migrates some previously static files to the private storage.
+ *
+ * @author Alexander Ebert
+ * @copyright 2001-2024 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since 6.1
+ */
+
+use wcf\data\file\FileEditor;
+use wcf\data\file\FileList;
+use wcf\system\WCF;
+use wcf\util\FileUtil;
+
+$moveTypes = [
+    'avif' => 'image/avif',
+    'mp3' => 'audio/mpeg',
+    'mp4' => 'video/mp4',
+    'pdf' => 'application/pdf',
+    'tiff' => 'image/tiff',
+    'txt' => 'text/plain',
+    'webm' => 'video/webm',
+];
+
+$fileList = new FileList();
+$fileList->getConditionBuilder()->add("file.mimeType IN (?)", [\array_values($moveTypes)]);
+$fileList->getConditionBuilder()->add("file.fileExtension <> ?", ["bin"]);
+$fileList->readObjects();
+
+$sql = "UPDATE  wcf1_file
+        SET     fileExtension = ?
+        WHERE   fileID = ?";
+$statement = WCF::getDB()->prepare($sql);
+
+$defunctFileIDs = [];
+foreach ($fileList->getObjects() as $file) {
+    $folderA = \substr($file->fileHash, 0, 2);
+    $folderB = \substr($file->fileHash, 2, 2);
+
+    $path = \WCF_DIR . \sprintf(
+        '_data/private/files/%s/%s/',
+        $folderA,
+        $folderB,
+    );
+
+    if (!\is_dir($path)) {
+        \mkdir($path, recursive: true);
+        FileUtil::makeWritable($path);
+    }
+
+    $filename = \sprintf(
+        '%d-%s.bin',
+        $file->fileID,
+        $file->fileHash,
+    );
+
+    $newPathname = $path . $filename;
+
+    $sourceFile = $file->getPathname();
+    if (\file_exists($sourceFile)) {
+        \rename(
+            $sourceFile,
+            $newPathname,
+        );
+    } else if (!\file_exists($newPathname)) {
+        $defunctFileIDs[] = $file->fileID;
+        continue;
+    }
+
+    $statement->execute([
+        'bin',
+        $file->fileID,
+    ]);
+}
+
+if ($defunctFileIDs !== []) {
+    FileEditor::deleteAll($defunctFileIDs);
+}
index f5b873f2719b27a0d1508a1220ccaaee8ea56b88..44e5e38281d8f66d6a758c09c1a75a37d92cd62b 100644 (file)
@@ -39,17 +39,10 @@ class File extends DatabaseObject implements ILinkableObject
      * @var array<string, string>
      */
     public const SAFE_FILE_EXTENSIONS = [
-        'avif' => 'image/avif',
         'gif' => 'image/gif',
         'jpg' => 'image/jpeg',
         'jpeg' => 'image/jpeg',
-        'mp3' => 'audio/mpeg',
-        'mp4' => 'video/mp4',
-        'pdf' => 'application/pdf',
         'png' => 'image/png',
-        'tiff' => 'image/tiff',
-        'txt' => 'text/plain',
-        'webm' => 'video/webm',
         'webp' => 'image/webp',
     ];