Skip to content

Commit

Permalink
fix warnings; do not save config.command_prefix in agent
Browse files Browse the repository at this point in the history
  • Loading branch information
gutschilla committed Aug 24, 2016
1 parent 5c86e0b commit 24f2900
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
16 changes: 12 additions & 4 deletions lib/pdf_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ defmodule PdfGenerator do
PdfGenerator.PathAgent, [[
wkhtml_path: Application.get_env(:pdf_generator, :wkhtml_path),
pdftk_path: Application.get_env(:pdf_generator, :pdftk_path),
command_prefix: Application.get_env(:pdf_generator, :command_prefix),
]]
)
]
Expand Down Expand Up @@ -122,7 +121,7 @@ defmodule PdfGenerator do

executable = wkhtml_path
arguments = List.flatten( [ shell_params, html_file, pdf_file ] )
command_prefix = Keyword.get( options, :command_prefix ) || PdfGenerator.PathAgent.get |> Map.get( :command_prefix )
command_prefix = Keyword.get( options, :command_prefix ) || Application.get_env( :pdf_generator, :command_prefix )

# allow for xvfb-run wkhtmltopdf arg1 arg2
# or sudo wkhtmltopdf ...
Expand Down Expand Up @@ -157,8 +156,17 @@ defmodule PdfGenerator do
def encrypt_pdf( pdf_input_path, user_pw, owner_pw ) do
pdftk_path = PdfGenerator.PathAgent.get.pdftk_path

if owner_pw == nil, do: owner_pw = Misc.Random.string(16)
if user_pw == nil, do: user_pw = Misc.Random.string(16)
owner_pw =
case owner_pw do
nil -> Misc.Random.string(16)
_ -> owner_pw
end

user_pw =
case user_pw do
nil -> Misc.Random.string(16)
_ -> user_pw
end

pdf_output_file = Path.join System.tmp_dir, Misc.Random.string <> ".pdf"

Expand Down
16 changes: 7 additions & 9 deletions lib/pdf_generator_path_agent.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,25 @@ defmodule PdfGenerator.PathAgent do
defstruct wkhtml_path: nil, pdftk_path: nil

@moduledoc """
Will check system requirements on startup and keep
Will check system requirements on startup and keep
a path map as state in an Agent process.
"""

@name __MODULE__

def start_link( path_options ) do
Agent.start_link( __MODULE__, :init_opts, [ path_options ], name: @name )
end

def init_opts( paths_from_options ) do

# options override system default paths
options =
options =
[
wkhtml_path: System.find_executable( "wkhtmltopdf" ),
pdftk_path: System.find_executable( "pdftk" ),
# command_prefix: System.find_executable( "xvfb-run" )
]
pdftk_path: System.find_executable( "pdftk" )
]
++ paths_from_options
|> Enum.filter( fn { _, v } -> v != nil end )
|> Enum.filter( fn { _, v } -> v != nil end )

# at least, wkhtmltopdf executable sould be there
if Keyword.fetch!( options, :wkhtml_path ) == nil do
Expand All @@ -44,5 +42,5 @@ defmodule PdfGenerator.PathAgent do
def get do
Agent.get( @name, fn( data ) -> data end )
end

end
4 changes: 2 additions & 2 deletions test/pdf_generator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ defmodule PdfGeneratorTest do
file_info = File.stat! temp_filename
assert file_info.size > 0
pdf = File.read! temp_filename

# PDF header should be present
assert String.slice( pdf, 0, 6) == "%PDF-1"
end

test "command prefix with noop env" do
{:ok, temp_filename } = PdfGenerator.generate @html, [ command_prefix: "env" ]
{:ok, _temp_filename } = PdfGenerator.generate @html, [ command_prefix: "env" ]
end

end

0 comments on commit 24f2900

Please sign in to comment.