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;
{
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());
+ }
}
class FaviconHandler
{
+ protected string $path;
+
public function __construct(
protected ImageManager $imageTool
) {
+ $this->path = public_path('favicon.ico');
}
/**
*/
public function saveForUploadedImage(UploadedFile $file): void
{
- $targetPath = public_path('favicon.ico');
- if (!is_writeable($targetPath)) {
+ if (!is_writeable($this->path)) {
return;
}
$bmpData = $image->encode('png');
$icoData = $this->pngToIco($bmpData, 32, 32);
- file_put_contents($targetPath, $icoData);
+ file_put_contents($this->path, $icoData);
}
/**
*/
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;
}
/**
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 () {