]> BookStack Code Mirror - bookstack/commitdiff
Integrated favicon handler with correct files & actions
authorDan Brown <redacted>
Thu, 9 Feb 2023 13:24:43 +0000 (13:24 +0000)
committerDan Brown <redacted>
Thu, 9 Feb 2023 13:24:43 +0000 (13:24 +0000)
Format does not look 100% correct though, won't show in Firefox/gimp.

app/Settings/AppSettingsStore.php
app/Uploads/FaviconHandler.php
public/favicon.ico
public/icon.ico [new file with mode: 0644]
tests/Settings/SettingsTest.php

index d830df639c604de305893f14cc0c6ba9d89a507d..e6fc466baa2e20d67110aebdf5e9e12e8f4a7576 100644 (file)
@@ -51,6 +51,8 @@ class AppSettingsStore
                 $this->destroyExistingSettingImage('app-icon-' . $size);
                 setting()->remove('app-icon-' . $size);
             }
+
+            $this->faviconHandler->restoreOriginal();
         }
     }
 
index 78c9a899b1bf009d21cbe47475a2092ca94927c7..f61e7ae64b2c779e6d8caa7053db8579e824fac9 100644 (file)
@@ -17,19 +17,32 @@ class FaviconHandler
      */
     public function saveForUploadedImage(UploadedFile $file): void
     {
+        $targetPath = public_path('favicon.ico');
+        if (!is_writeable($targetPath)) {
+            return;
+        }
+
         $imageData = file_get_contents($file->getRealPath());
         $image = $this->imageTool->make($imageData);
         $image->resize(32, 32);
         $bmpData = $image->encode('bmp');
         $icoData = $this->bmpToIco($bmpData, 32, 32);
 
-        // TODO - Below are test paths
-        file_put_contents(public_path('uploads/test.ico'), $icoData);
-        file_put_contents(public_path('uploads/test.bmp'), $bmpData);
+        file_put_contents($targetPath, $icoData);
+    }
+
+    /**
+     * Restore the original favicon image.
+     */
+    public function restoreOriginal(): void
+    {
+        $targetPath = public_path('favicon.ico');
+        $original = public_path('icon.ico');
+        if (!is_writeable($targetPath)) {
+            return;
+        }
 
-        // TODO - Permission check for icon overwrite
-        // TODO - Write to correct location
-        // TODO - Handle deletion and restore of original icon on user icon clear
+        copy($original, $targetPath);
     }
 
     /**
index 41655ccba55f8dd5f250471eb2e8928b21a85992..e047657ccad8c38f318f2bda36f0809a9b0c878f 100644 (file)
Binary files a/public/favicon.ico and b/public/favicon.ico differ
diff --git a/public/icon.ico b/public/icon.ico
new file mode 100644 (file)
index 0000000..41655cc
Binary files /dev/null and b/public/icon.ico differ
index 30bb50f7ccf723dfebb0773ca6c7ba2579ef5ae7..fb952585a20be5a84cba82868ee93a64a26c23bb 100644 (file)
@@ -52,6 +52,10 @@ class SettingsTest extends TestCase
         $this->assertFalse(setting()->get('app-icon-128'));
         $this->assertFalse(setting()->get('app-icon-64'));
         $this->assertFalse(setting()->get('app-icon-32'));
+        $this->assertEquals(
+            file_get_contents(public_path('icon.ico')),
+            file_get_contents(public_path('favicon.ico')),
+        );
 
         $prevFileCount = count(glob(dirname($expectedPath) . DIRECTORY_SEPARATOR . '*.png'));
 
@@ -71,6 +75,11 @@ class SettingsTest extends TestCase
         $resp = $this->get('/');
         $this->withHtml($resp)->assertElementCount('link[sizes][href*="my-app-icon"]', 6);
 
+        $this->assertNotEquals(
+            file_get_contents(public_path('icon.ico')),
+            file_get_contents(public_path('favicon.ico')),
+        );
+
         $reset = $this->post('/settings/customization', ['app_icon_reset' => 'true']);
         $reset->assertRedirect('/settings/customization');
 
@@ -81,5 +90,10 @@ class SettingsTest extends TestCase
         $this->assertFalse(setting()->get('app-icon-128'));
         $this->assertFalse(setting()->get('app-icon-64'));
         $this->assertFalse(setting()->get('app-icon-32'));
+
+        $this->assertEquals(
+            file_get_contents(public_path('icon.ico')),
+            file_get_contents(public_path('favicon.ico')),
+        );
     }
 }