New-HTML -FavIcon
xlrod opened this issue · 9 comments
Hello,
I've added the -FavIcon
parameter to New-HTML
to allow for favicons for the dashboards.
$Table = Get-Process | Select-Object -First 5
New-HTML -TitleText "Test-Dashboard" -FavIcon "$PSScriptRoot\138544-interaction-assets\png\file-2.png" -FilePath "$PSScriptRoot\Test-Dashboard.html" {
New-HTMLTable -DataTable $Table
}
Is there any type of validation I should add aside from an if statement with a Test-Path
?
if(Test-Path -Path $FavIcon){
New-HTMLTag -Tag 'link' -Attributes @{rel = 'icon'; href = "$FavIcon"; type = 'image/x-icon'}
}
I was thinking of doing the Test-Path
with -Filter
for allowed favicon formats.
Hello,
Well, it needs to support:
- standard "filesystem" files which then needs to be converted to binary format
- https:// link
I wonder how to give people a choice to convert https links to binary format as well. If at all should be considered.
Otherwise, your implementation makes sense
How is this going?
I'm going to be testing your suggestions this week.
I saw you already had a function to convert an image to binary, so I will try that first.
I've added a some of your functions/code to the validation.
The parameter FavIcon
is still a string, that expects the path of the image, (Eg. "C:\users\xlrod\image\icon.png") and the check is as follows:
$Extension = $FavIcon -replace ".*(?=\.)"
if ($Extension -in @('.png', '.jpg', 'jpeg', '.svg', '.ico')) {
if(Test-Path -Path $FavIcon){
$FavIcon = Get-Item -Path $FavIcon
$FavIconImageBinary = Convert-ImageToBinary -ImageFile $FavIcon
New-HTMLTag -Tag 'link' -Attributes @{rel = 'icon'; href = "$FavIconImageBinary"; type = 'image/x-icon'}
}
else{
Write-Warning -Message "The path to the FavIcon image could not be resolved."
}
}
else{
Write-Warning -Message "File extension `'$Extension`' is not supported as a FavIcon image."
}
This results in this, if the image is valid.
If the path has a valid extension, but could not be found.
If the path does not have a valid extension.
*Edit: Added a second line to display which file extensions are accepted.
what about linked .ico (https?)
Will look into this.
Btw you should consider using [Uri] instead of [string]. It acts as a string when working with paths but gives you lots of options.
[uri] $Test = 'https://evotec.xyz/myfile.txt'
[Uri] $Test1 = 'C:\Users\przemyslaw.klys\OneDrive - Evotec\Support\GitHub\PSSharedGoods\PSSharedGoods.psd1'
$Test | fl
$Test1 | fl
Also for extension:
[System.IO.Path]::GetExtension($Test)
System.IO.Path has lots of other useful options.
Nice! Would this work for the https links?
Changed the parameters to Uri [Uri] $FavIcon
(works great!)
[Uri]$FavIcon = "https://icolink.com/components/com_djclassifieds/images/item/1/1990_ico-amazonians-green-coin_thb.jpg"
$Extension = [System.IO.Path]::GetExtension($FavIcon)
if ($Extension -in @('.png', '.jpg', 'jpeg', '.svg', '.ico')) {
switch ($FavIcon.Scheme) {
"file" {
if (Test-Path -Path $FavIcon.OriginalString) {
$FavIcon = Get-Item -Path $FavIcon.OriginalString
$FavIconImageBinary = Convert-ImageToBinary -ImageFile $FavIcon
New-HTMLTag -Tag 'link' -Attributes @{rel = 'icon'; href = "$FavIconImageBinary"; type = 'image/x-icon' }
}
else {
Write-Warning -Message "The path to the FavIcon image could not be resolved."
}
}
"https" {
$FavIcon = $FavIcon.OriginalString
New-HTMLTag -Tag 'link' -Attributes @{rel = 'icon'; href = "$FavIcon"; type = 'image/x-icon' }
}
default {
Write-Warning -Message "The path to the FavIcon image could not be resolved."
}
}
}
else {
Write-Warning -Message "File extension `'$Extension`' is not supported as a FavIcon image.`nPlease use images with these extensions: '.png', '.jpg', 'jpeg', '.svg', '.ico'"
}
It seems to work ;)