Skip to content

Commit

Permalink
Integrated favicon handler with correct files & actions
Browse files Browse the repository at this point in the history
Format does not look 100% correct though, won't show in Firefox/gimp.
  • Loading branch information
ssddanbrown committed Feb 9, 2023
1 parent 420f89a commit 1a18964
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
2 changes: 2 additions & 0 deletions app/Settings/AppSettingsStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ protected function updateAppIcon(Request $request): void
$this->destroyExistingSettingImage('app-icon-' . $size);
setting()->remove('app-icon-' . $size);
}

$this->faviconHandler->restoreOriginal();
}
}

Expand Down
25 changes: 19 additions & 6 deletions app/Uploads/FaviconHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,32 @@ public function __construct(
*/
public function saveForUploadedImage(UploadedFile $file): void
{
$targetPath = public_path('favicon.ico');
if (!is_writeable($targetPath)) {
return;
}

$imageData = file_get_contents($file->getRealPath());
$image = $this->imageTool->make($imageData);
$image->resize(32, 32);
$bmpData = $image->encode('bmp');
$icoData = $this->bmpToIco($bmpData, 32, 32);

// TODO - Below are test paths
file_put_contents(public_path('uploads/test.ico'), $icoData);
file_put_contents(public_path('uploads/test.bmp'), $bmpData);
file_put_contents($targetPath, $icoData);
}

/**
* Restore the original favicon image.
*/
public function restoreOriginal(): void
{
$targetPath = public_path('favicon.ico');
$original = public_path('icon.ico');
if (!is_writeable($targetPath)) {
return;
}

// TODO - Permission check for icon overwrite
// TODO - Write to correct location
// TODO - Handle deletion and restore of original icon on user icon clear
copy($original, $targetPath);
}

/**
Expand Down
Binary file modified public/favicon.ico
Binary file not shown.
Binary file added public/icon.ico
Binary file not shown.
14 changes: 14 additions & 0 deletions tests/Settings/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public function test_updating_and_removing_app_icon()
$this->assertFalse(setting()->get('app-icon-128'));
$this->assertFalse(setting()->get('app-icon-64'));
$this->assertFalse(setting()->get('app-icon-32'));
$this->assertEquals(
file_get_contents(public_path('icon.ico')),
file_get_contents(public_path('favicon.ico')),
);

$prevFileCount = count(glob(dirname($expectedPath) . DIRECTORY_SEPARATOR . '*.png'));

Expand All @@ -71,6 +75,11 @@ public function test_updating_and_removing_app_icon()
$resp = $this->get('/');
$this->withHtml($resp)->assertElementCount('link[sizes][href*="my-app-icon"]', 6);

$this->assertNotEquals(
file_get_contents(public_path('icon.ico')),
file_get_contents(public_path('favicon.ico')),
);

$reset = $this->post('/settings/customization', ['app_icon_reset' => 'true']);
$reset->assertRedirect('/settings/customization');

Expand All @@ -81,5 +90,10 @@ public function test_updating_and_removing_app_icon()
$this->assertFalse(setting()->get('app-icon-128'));
$this->assertFalse(setting()->get('app-icon-64'));
$this->assertFalse(setting()->get('app-icon-32'));

$this->assertEquals(
file_get_contents(public_path('icon.ico')),
file_get_contents(public_path('favicon.ico')),
);
}
}

0 comments on commit 1a18964

Please sign in to comment.