public/Set-RecipeImage.ps1

function Set-RecipeImage {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [int]$RecipeId,

        [Parameter(Mandatory)]
        [byte[]]$Bytes,

        [Parameter(Mandatory)]
        [string]$OriginalFileName
    )

    if (-not $Bytes -or $Bytes.Length -eq 0) {
        throw "Set-RecipeImage: Bytes were null or empty."
    }

    $folder = Get-RecipeImageFolder
    if (-not $folder) {
        throw "Set-RecipeImage: Image folder was null."
    }

    $ext = [System.IO.Path]::GetExtension($OriginalFileName)
    if (-not $ext) { $ext = ".jpg" }

    # Safe filename: Recipe_<id>.<ext>
    $fileName = "Recipe_{0}{1}" -f $RecipeId, $ext.ToLowerInvariant()
    $fullPath = Join-Path $folder $fileName

    [System.IO.File]::WriteAllBytes($fullPath, $Bytes)

    $safeFile = ConvertTo-SqlSafe $fileName

    Invoke-RecipeDbQuery -Query @"
UPDATE Recipes
SET ImageFileName = '$safeFile',
    ImageUpdatedAt = CURRENT_TIMESTAMP
WHERE RecipeId = $RecipeId;
"@
 | Out-Null

    $fullPath
}