Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

73 řádky
3.8KB

  1. param(
  2. [Parameter(Mandatory = $true)][string]$Path,
  3. [string]$ConfigPath = (Join-Path (Split-Path -Parent $PSScriptRoot) 'config.ini')
  4. )
  5. $ErrorActionPreference = 'Stop'
  6. . (Join-Path $PSScriptRoot 'template-common.ps1')
  7. $context = Get-MailContext -ConfigPath $ConfigPath
  8. $limits = Get-TemplateLimits $context
  9. $sourcePath = [System.IO.Path]::GetFullPath($Path)
  10. if (-not (Test-Path -LiteralPath $sourcePath -PathType Leaf)) { throw "Template not found: $sourcePath" }
  11. if (-not $sourcePath.StartsWith($limits.Root + [System.IO.Path]::DirectorySeparatorChar,
  12. [StringComparison]::OrdinalIgnoreCase)) { throw 'Template must be inside TEMPLATE_ROOT' }
  13. $templateName = [System.IO.Path]::GetFileNameWithoutExtension($sourcePath)
  14. if ($templateName.Length -gt 200) { throw 'Template file name is too long' }
  15. $document = Read-SafeTemplate $sourcePath
  16. $images = Get-TemplateImages $document $sourcePath $limits
  17. $html = $document.OuterXml
  18. $htmlBytes = [System.Text.Encoding]::UTF8.GetBytes($html)
  19. $total = $htmlBytes.Length
  20. foreach ($image in $images) { $total += $image.Bytes.Length }
  21. if ($total -gt $limits.MessageLimit) { throw 'Template and images exceed TEMPLATE_MESSAGE_MAX_BYTES' }
  22. $connection = Open-MailDatabase $context
  23. $transaction = $null
  24. try {
  25. $transaction = $connection.BeginTransaction()
  26. $next = $connection.CreateCommand()
  27. $next.Transaction = $transaction
  28. $next.CommandText = 'SELECT MAX([VersionNumber]) FROM [TemplateVersion] WHERE [TemplateName]=?'
  29. Add-TextParameter $next 'name' 200 $templateName
  30. $maximum = $next.ExecuteScalar()
  31. $version = if ($maximum -is [DBNull]) { 1 } else { [int]$maximum + 1 }
  32. $insert = $connection.CreateCommand()
  33. $insert.Transaction = $transaction
  34. $insert.CommandText = 'INSERT INTO [TemplateVersion] ([TemplateName],[VersionNumber],[HtmlContent],[ContentHash],[ImportedAtUtc],[ValidationStatus]) VALUES (?,?,?,?,?,?)'
  35. Add-TextParameter $insert 'name' 200 $templateName
  36. $null = $insert.Parameters.Add('version', [System.Data.OleDb.OleDbType]::Integer)
  37. $insert.Parameters[1].Value = $version
  38. $null = $insert.Parameters.Add('html', [System.Data.OleDb.OleDbType]::LongVarWChar)
  39. $insert.Parameters[2].Value = $html
  40. Add-TextParameter $insert 'hash' 64 (Get-HashHex $htmlBytes)
  41. $null = $insert.Parameters.Add('imported', [System.Data.OleDb.OleDbType]::Date)
  42. $insert.Parameters[4].Value = [DateTime]::UtcNow
  43. Add-TextParameter $insert 'status' 30 'validated'
  44. $null = $insert.ExecuteNonQuery()
  45. $identity = $connection.CreateCommand()
  46. $identity.Transaction = $transaction
  47. $identity.CommandText = 'SELECT @@IDENTITY'
  48. $templateId = [int]$identity.ExecuteScalar()
  49. foreach ($image in $images) {
  50. $command = $connection.CreateCommand()
  51. $command.Transaction = $transaction
  52. $command.CommandText = 'INSERT INTO [TemplateImage] ([TemplateVersionId],[OriginalRelativePath],[ContentType],[ContentHash],[SnapshotBytes],[ContentId]) VALUES (?,?,?,?,?,?)'
  53. $null = $command.Parameters.Add('template', [System.Data.OleDb.OleDbType]::Integer)
  54. $command.Parameters[0].Value = $templateId
  55. Add-TextParameter $command 'path' 255 $image.Path
  56. Add-TextParameter $command 'type' 40 $image.Type
  57. Add-TextParameter $command 'hash' 64 $image.Hash
  58. $null = $command.Parameters.Add('bytes', [System.Data.OleDb.OleDbType]::LongVarBinary, $image.Bytes.Length)
  59. $command.Parameters[4].Value = $image.Bytes
  60. Add-TextParameter $command 'cid' 200 $image.Cid
  61. $null = $command.ExecuteNonQuery()
  62. }
  63. $transaction.Commit()
  64. $transaction = $null
  65. Write-Output "Imported template ID $templateId, version $version, $($images.Count) inline image(s)."
  66. }
  67. catch {
  68. if ($transaction) { $transaction.Rollback() }
  69. throw
  70. }
  71. finally { $connection.Dispose() }

Powered by TurnKey Linux.