Skip to content

Commit

Permalink
Added control-upon-access of the default favicon.ico file
Browse files Browse the repository at this point in the history
  • Loading branch information
ssddanbrown committed Feb 9, 2023
1 parent da42fc7 commit f333db8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
4 changes: 2 additions & 2 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function notFound()
*/
public function favicon(FaviconHandler $favicons)
{
$favicons->restoreOriginalIfNotExists();
return response()->file($favicons->getPath());
$exists = $favicons->restoreOriginalIfNotExists();
return response()->file($exists ? $favicons->getPath() : $favicons->getOriginalPath());
}
}
28 changes: 20 additions & 8 deletions app/Uploads/FaviconHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,29 @@ public function saveForUploadedImage(UploadedFile $file): void

/**
* 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();
}

/**
Expand All @@ -64,6 +68,14 @@ public function getPath(): string
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:
Expand Down
12 changes: 12 additions & 0 deletions tests/PublicActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,18 @@ public function test_robots_effected_by_setting()
$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']);
Expand Down

0 comments on commit f333db8

Please sign in to comment.