]> BookStack Code Mirror - bookstack/commitdiff
Added default favicon creation upon access.
authorDan Brown <redacted>
Thu, 9 Feb 2023 20:57:35 +0000 (20:57 +0000)
committerDan Brown <redacted>
Thu, 9 Feb 2023 20:57:35 +0000 (20:57 +0000)
app/Http/Controllers/HomeController.php
app/Uploads/FaviconHandler.php
public/favicon.ico [deleted file]
routes/web.php

index c3c8d1066d75dd664cad9750a45f955905c651dc..84651f6531fde170829ad3e0b68b3b31da10e0f9 100644 (file)
@@ -10,6 +10,7 @@ use BookStack\Entities\Queries\TopFavourites;
 use BookStack\Entities\Repos\BookRepo;
 use BookStack\Entities\Repos\BookshelfRepo;
 use BookStack\Entities\Tools\PageContent;
+use BookStack\Uploads\FaviconHandler;
 use BookStack\Util\SimpleListOptions;
 use Illuminate\Http\Request;
 
@@ -127,4 +128,15 @@ class HomeController extends Controller
     {
         return response()->view('errors.404', [], 404);
     }
+
+    /**
+     * Serve the application favicon.
+     * Ensures a 'favicon.ico' file exists at the web root location (if writable) to be served
+     * directly by the webserver in the future.
+     */
+    public function favicon(FaviconHandler $favicons)
+    {
+        $favicons->restoreOriginalIfNotExists();
+        return response()->file($favicons->getPath());
+    }
 }
index 2e756c587962e00c6809cab4d5f72e329dc91e0e..3dc702ea62bb424fa983ffa2229352fbf1e399c6 100644 (file)
@@ -7,9 +7,12 @@ use Intervention\Image\ImageManager;
 
 class FaviconHandler
 {
+    protected string $path;
+
     public function __construct(
         protected ImageManager $imageTool
     ) {
+        $this->path = public_path('favicon.ico');
     }
 
     /**
@@ -17,8 +20,7 @@ class FaviconHandler
      */
     public function saveForUploadedImage(UploadedFile $file): void
     {
-        $targetPath = public_path('favicon.ico');
-        if (!is_writeable($targetPath)) {
+        if (!is_writeable($this->path)) {
             return;
         }
 
@@ -28,7 +30,7 @@ class FaviconHandler
         $bmpData = $image->encode('png');
         $icoData = $this->pngToIco($bmpData, 32, 32);
 
-        file_put_contents($targetPath, $icoData);
+        file_put_contents($this->path, $icoData);
     }
 
     /**
@@ -36,13 +38,30 @@ class FaviconHandler
      */
     public function restoreOriginal(): void
     {
-        $targetPath = public_path('favicon.ico');
         $original = public_path('icon.ico');
-        if (!is_writeable($targetPath)) {
+        if (!is_writeable($this->path)) {
             return;
         }
 
-        copy($original, $targetPath);
+        copy($original, $this->path);
+    }
+
+    /**
+     * Restore the original favicon image if no favicon image is already in use.
+     */
+    public function restoreOriginalIfNotExists(): void
+    {
+        if (!file_exists($this->path)) {
+            $this->restoreOriginal();
+        }
+    }
+
+    /**
+     * Get the path to the favicon file.
+     */
+    public function getPath(): string
+    {
+        return $this->path;
     }
 
     /**
diff --git a/public/favicon.ico b/public/favicon.ico
deleted file mode 100644 (file)
index 41655cc..0000000
Binary files a/public/favicon.ico and /dev/null differ
index 95b4ae535236b879e9c9aa83ac2e486251a6d318..937dc0c6c49e685352681bc895bd057d80e60088 100644 (file)
@@ -40,6 +40,7 @@ use Illuminate\View\Middleware\ShareErrorsFromSession;
 
 Route::get('/status', [StatusController::class, 'show']);
 Route::get('/robots.txt', [HomeController::class, 'robots']);
+Route::get('/favicon.ico', [HomeController::class, 'favicon']);
 
 // Authenticated routes...
 Route::middleware('auth')->group(function () {