Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

64 рядки
3.2KB

  1. param(
  2. [Parameter(Mandatory = $true)][int]$TemplateId,
  3. [string]$ConfigPath = (Join-Path (Split-Path -Parent $PSScriptRoot) 'config.ini'),
  4. [string]$OutputRoot = (Join-Path (Split-Path -Parent $PSScriptRoot) 'temp\previews')
  5. )
  6. $ErrorActionPreference = 'Stop'
  7. . (Join-Path $PSScriptRoot 'template-common.ps1')
  8. if ($TemplateId -lt 1) { throw 'TemplateId must be positive' }
  9. $context = Get-MailContext -ConfigPath $ConfigPath
  10. $connection = Open-MailDatabase $context
  11. try {
  12. $command = $connection.CreateCommand()
  13. $command.CommandText = 'SELECT [HtmlContent],[ContentHash],[ValidationStatus] FROM [TemplateVersion] WHERE [Id]=?'
  14. $null = $command.Parameters.Add('id', [System.Data.OleDb.OleDbType]::Integer)
  15. $command.Parameters[0].Value = $TemplateId
  16. $reader = $command.ExecuteReader()
  17. try {
  18. if (-not $reader.Read()) { throw "Template ID $TemplateId was not found" }
  19. $html = [string]$reader.GetValue(0)
  20. $hash = [string]$reader.GetValue(1)
  21. if ([string]$reader.GetValue(2) -ne 'validated' -or
  22. (Get-HashHex ([System.Text.Encoding]::UTF8.GetBytes($html))) -ne $hash) {
  23. throw 'Template validation or content hash failed'
  24. }
  25. }
  26. finally { $reader.Close() }
  27. $document = [xml]$html
  28. Assert-TemplateNode $document.DocumentElement
  29. $previewPath = Join-Path $OutputRoot ([guid]::NewGuid().ToString('N'))
  30. $null = New-Item -ItemType Directory -Path $previewPath -Force
  31. $command = $connection.CreateCommand()
  32. $command.CommandText = 'SELECT [OriginalRelativePath],[ContentType],[ContentHash],[SnapshotBytes],[ContentId] FROM [TemplateImage] WHERE [TemplateVersionId]=? ORDER BY [Id]'
  33. $null = $command.Parameters.Add('id', [System.Data.OleDb.OleDbType]::Integer)
  34. $command.Parameters[0].Value = $TemplateId
  35. $reader = $command.ExecuteReader()
  36. try {
  37. $index = 0
  38. while ($reader.Read()) {
  39. $index++
  40. $bytes = [byte[]]$reader.GetValue(3)
  41. if ((Get-HashHex $bytes) -ne [string]$reader.GetValue(2)) { throw 'Stored image hash failed' }
  42. $type = [string]$reader.GetValue(1)
  43. $ext = switch ($type) { 'image/png' { 'png' } 'image/jpeg' { 'jpg' } 'image/gif' { 'gif' } default { throw 'Unsupported stored image type' } }
  44. $name = "image-$index.$ext"
  45. [System.IO.File]::WriteAllBytes((Join-Path $previewPath $name), $bytes)
  46. $cid = 'cid:' + [string]$reader.GetValue(4)
  47. $found = $false
  48. foreach ($node in $document.GetElementsByTagName('img')) {
  49. if ($node.GetAttribute('src') -eq $cid) { $node.SetAttribute('src', $name); $found = $true }
  50. }
  51. if (-not $found) { throw 'Stored image is not referenced by the template' }
  52. }
  53. }
  54. finally { $reader.Close() }
  55. foreach ($node in $document.GetElementsByTagName('img')) {
  56. if ($node.GetAttribute('src').StartsWith('cid:')) { throw 'Template image snapshot is missing' }
  57. }
  58. $file = Join-Path $previewPath 'preview.html'
  59. [System.IO.File]::WriteAllText($file, $document.OuterXml, (New-Object System.Text.UTF8Encoding($false)))
  60. Write-Output $file
  61. }
  62. finally { $connection.Dispose() }

Powered by TurnKey Linux.