Skip to content

Commit

Permalink
Added default favicon creation upon access.
Browse files Browse the repository at this point in the history
  • Loading branch information
ssddanbrown committed Feb 9, 2023
1 parent 48f1934 commit da42fc7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
12 changes: 12 additions & 0 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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;

Expand Down Expand Up @@ -127,4 +128,15 @@ public function notFound()
{
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());
}
}
31 changes: 25 additions & 6 deletions app/Uploads/FaviconHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@

class FaviconHandler
{
protected string $path;

public function __construct(
protected ImageManager $imageTool
) {
$this->path = public_path('favicon.ico');
}

/**
* Save the given UploadedFile instance as the application favicon.
*/
public function saveForUploadedImage(UploadedFile $file): void
{
$targetPath = public_path('favicon.ico');
if (!is_writeable($targetPath)) {
if (!is_writeable($this->path)) {
return;
}

Expand All @@ -28,21 +30,38 @@ public function saveForUploadedImage(UploadedFile $file): void
$bmpData = $image->encode('png');
$icoData = $this->pngToIco($bmpData, 32, 32);

file_put_contents($targetPath, $icoData);
file_put_contents($this->path, $icoData);
}

/**
* Restore the original favicon image.
*/
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;
}

/**
Expand Down
Binary file removed public/favicon.ico
Binary file not shown.
1 change: 1 addition & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

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 () {
Expand Down

0 comments on commit da42fc7

Please sign in to comment.