Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

65 lignes
3.2KB

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

Powered by TurnKey Linux.