]> BookStack Code Mirror - bookstack/commitdiff
Added control-upon-access of the default favicon.ico file 4032/head
authorDan Brown <redacted>
Thu, 9 Feb 2023 21:16:27 +0000 (21:16 +0000)
committerDan Brown <redacted>
Thu, 9 Feb 2023 21:16:27 +0000 (21:16 +0000)
app/Http/Controllers/HomeController.php
app/Uploads/FaviconHandler.php
tests/PublicActionTest.php

index 84651f6531fde170829ad3e0b68b3b31da10e0f9..a82710523846ca252ada0d2cf4db88a4650f6509 100644 (file)
@@ -136,7 +136,7 @@ class HomeController extends Controller
      */
     public function favicon(FaviconHandler $favicons)
     {
-        $favicons->restoreOriginalIfNotExists();
-        return response()->file($favicons->getPath());
+        $exists = $favicons->restoreOriginalIfNotExists();
+        return response()->file($exists ? $favicons->getPath() : $favicons->getOriginalPath());
     }
 }
index 3dc702ea62bb424fa983ffa2229352fbf1e399c6..c637356e068b479dca773409dcada15bbc2ad916 100644 (file)
@@ -35,25 +35,29 @@ class FaviconHandler
 
     /**
      * Restore the original favicon image.
+     * Returned boolean indicates if the copy occurred.
      */
-    public function restoreOriginal(): void
+    public function restoreOriginal(): bool
     {
-        $original = public_path('icon.ico');
-        if (!is_writeable($this->path)) {
-            return;
+        $permissionItem = file_exists($this->path) ? $this->path : dirname($this->path);
+        if (!is_writeable($permissionItem)) {
+            return false;
         }
 
-        copy($original, $this->path);
+        return copy($this->getOriginalPath(), $this->path);
     }
 
     /**
      * Restore the original favicon image if no favicon image is already in use.
+     * Returns a boolean to indicate if the file exists.
      */
-    public function restoreOriginalIfNotExists(): void
+    public function restoreOriginalIfNotExists(): bool
     {
-        if (!file_exists($this->path)) {
-            $this->restoreOriginal();
+        if (file_exists($this->path)) {
+            return true;
         }
+
+        return $this->restoreOriginal();
     }
 
     /**
@@ -64,6 +68,14 @@ class FaviconHandler
         return $this->path;
     }
 
+    /**
+     * Get the path of the original favicon copy.
+     */
+    public function getOriginalPath(): string
+    {
+        return public_path('icon.ico');
+    }
+
     /**
      * Convert PNG image data to ICO file format.
      * Built following the file format info from Wikipedia:
index afc7fcef36aafb4e2fd6fd0c92c8aee9c4102e69..e21afdf3322df7f9acc09eb7e997917cf2384d94 100644 (file)
@@ -155,6 +155,18 @@ class PublicActionTest extends TestCase
         $this->get('/robots.txt')->assertSee("User-agent: *\nDisallow: /");
     }
 
+    public function test_default_favicon_file_created_upon_access()
+    {
+        $faviconPath = public_path('favicon.ico');
+        if (file_exists($faviconPath)) {
+            unlink($faviconPath);
+        }
+
+        $this->assertFileDoesNotExist($faviconPath);
+        $this->get('/favicon.ico');
+        $this->assertFileExists($faviconPath);
+    }
+
     public function test_public_view_then_login_redirects_to_previous_content()
     {
         $this->setSettings(['app-public' => 'true']);