Add rank image upload migration script
authorjoshuaruesweg <ruesweg@woltlab.com>
Thu, 7 Jan 2021 16:19:32 +0000 (17:19 +0100)
committerjoshuaruesweg <ruesweg@woltlab.com>
Mon, 11 Jan 2021 11:46:03 +0000 (12:46 +0100)
com.woltlab.wcf/package.xml
wcfsetup/install/files/acp/update_com.woltlab.wcf_5.4_migrate_rank_images.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/background/job/DownloadRankImageJob.class.php [new file with mode: 0644]

index e228e351f408077625b41fd5768630456e4161fa..f0c44823e75c5aef05ea027391062b5b1c9f23f8 100644 (file)
@@ -101,5 +101,8 @@ tar cvf com.woltlab.wcf/files_pre.tar -C wcfsetup/install/files/ \
                
                <!-- Migration of multi-factor authentication. -->
                <instruction type="script" run="standalone">acp/update_com.woltlab.wcf_5.4_migrate_multifactor.php</instruction>
+               
+               <!-- Migration of the rank images -->
+               <instruction type="script" run="standalone">acp/update_com.woltlab.wcf_5.4_migrate_rank_images.php</instruction>
        </instructions>
 </package>
diff --git a/wcfsetup/install/files/acp/update_com.woltlab.wcf_5.4_migrate_rank_images.php b/wcfsetup/install/files/acp/update_com.woltlab.wcf_5.4_migrate_rank_images.php
new file mode 100644 (file)
index 0000000..c466b5a
--- /dev/null
@@ -0,0 +1,44 @@
+<?php
+
+use wcf\data\user\rank\UserRank;
+use wcf\data\user\rank\UserRankEditor;
+use wcf\data\user\rank\UserRankList;
+use wcf\system\background\BackgroundQueueHandler;
+use wcf\system\background\job\DownloadRankImageJob;
+use wcf\util\Url;
+
+/**
+ * Downloads the rank image files to the new internal upload system.
+ *
+ * @author     Joshua Ruesweg
+ * @copyright  2001-2021 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    WoltLabSuite\Core
+ */
+$rankList = new UserRankList();
+$rankList->readObjects();
+
+foreach ($rankList as $rank) {
+       if (!empty($rank->rankImage)) {
+               if (Url::is($rank->rankImage)) {
+                       BackgroundQueueHandler::getInstance()->enqueueIn(new DownloadRankImageJob($rank), 60);
+                       continue;
+               }
+               
+               $rankEditor = new UserRankEditor($rank);
+               
+               if (file_exists(WCF_DIR . $rank->rankImage)) {
+                       $newImageName = WCF_DIR . UserRank::RANK_IMAGE_DIR . $rank->rankID .'-'. basename($rank->rankImage);
+                       copy(WCF_DIR . $rank->rankImage, $newImageName);
+                       
+                       $rankEditor->update([
+                               'rankImage' => basename($newImageName)
+                       ]);
+               }
+               else {
+                       $rankEditor->update([
+                               'rankImage' => ""
+                       ]);
+               }
+       }
+}
diff --git a/wcfsetup/install/files/lib/system/background/job/DownloadRankImageJob.class.php b/wcfsetup/install/files/lib/system/background/job/DownloadRankImageJob.class.php
new file mode 100644 (file)
index 0000000..00e4238
--- /dev/null
@@ -0,0 +1,73 @@
+<?php
+namespace wcf\system\background\job;
+use GuzzleHttp\Psr7\Request;
+use wcf\data\user\rank\UserRank;
+use wcf\data\user\rank\UserRankEditor;
+use wcf\system\io\HttpFactory;
+use wcf\util\Url;
+
+/**
+ * Downloads the rank images and stores it locally within the rank image path.
+ *
+ * @author     Joshua Ruesweg
+ * @copyright  2001-2021 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    WoltLabSuite\Core\System\Background\Job
+ * @since      5.4
+ * @deprecated 5.4 - This background job is used for the upgrade from 5.3 to 5.4 and will be removed with 5.5.
+ */
+class DownloadRankImageJob extends AbstractBackgroundJob {
+       /**
+        * @inheritDoc
+        */
+       const MAX_FAILURES = 5;
+       
+       /**
+        * @var int
+        */
+       protected $userRankID;
+       
+       public function __construct(UserRank $userRank) {
+               $this->userRankID = $userRank->rankID;
+       }
+       
+       /**
+        * @return      int     every 10 minutes
+        */
+       public function retryAfter() {
+               return 10 * 60;
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       public function perform() {
+               $rank = new UserRank($this->userRankID);
+               if (!$rank->rankID) return;
+               if (empty($rank->rankImage)) return;
+               if (!Url::is($rank->rankImage)) return;
+               
+               $rankEditor = new UserRankEditor($rank);
+               
+               $extension = pathinfo(Url::parse($rank->rankImage)['path'], PATHINFO_EXTENSION);
+               if (in_array($extension, ['gif','png','jpg','jpeg','svg','webp'])) {
+                       $http = HttpFactory::makeClient([
+                               'timeout' => 10,
+                       ]);
+                       
+                       $imageDest = WCF_DIR . UserRank::RANK_IMAGE_DIR . $rank->rankID .'-rankImage.'. $extension;
+                       $http->send(new Request('GET', $rank->rankImage), [
+                               'sink' => $imageDest,
+                       ]);
+                       
+                       $rankEditor->update([
+                               'rankImage' => basename($imageDest)
+                       ]);
+               }
+               else {
+                       $rankEditor->update([
+                               'rankImage' => ""
+                       ]);
+               }
+       }
+}