Skip to content

Commit c62b8fa

Browse files
committed
Use instance variables for escaped paths.
1 parent ed55530 commit c62b8fa

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

lib/libreconv.rb

+10-8
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,25 @@ def initialize(source, target, soffice_command = nil, convert_to = nil)
2121
determine_soffice_command
2222
@source_type = check_source_type
2323

24+
# If the URL contains GET params, the '&' could break when being used
25+
# as an argument to soffice. Wrap it in single quotes to escape it.
26+
# Then strip them from the target temp file name
27+
@escaped_source = @source_type == 1 ? @source : "'#{@source}'"
28+
@escaped_source_path = @source_type == 1 ? @source : URI.parse(@source).path
29+
2430
unless @soffice_command && File.exists?(@soffice_command)
2531
raise IOError, "Can't find Libreoffice or Openoffice executable."
2632
end
2733
end
2834

2935
def convert
30-
orig_stdout = $stdout.clone
31-
$stdout.reopen File.new('/dev/null', 'w')
3236
Dir.mktmpdir { |target_path|
33-
# If the URL contains GET params, the '&' could break when being used
34-
# as an argument to soffice. Wrap it in single quotes to escape it.
35-
escaped_source = @source_type == 1 ? @source : "'#{@source}'"
36-
pid = Spoon.spawnp(@soffice_command, "--headless", "--convert-to", @convert_to, source, "--outdir", target_path)
37+
orig_stdout = $stdout.clone
38+
$stdout.reopen File.new('/dev/null', 'w')
39+
pid = Spoon.spawnp(@soffice_command, "--headless", "--convert-to", @convert_to, @escaped_source, "--outdir", target_path)
3740
Process.waitpid(pid)
3841
$stdout.reopen orig_stdout
39-
escaped_source_path = @source_type == 1 ? @source : URI.parse(@source).path # Strip any GET params that the URL may have
40-
target_tmp_file = "#{target_path}/#{File.basename(escaped_source_path, ".*")}.#{File.basename(@convert_to, ":*")}"
42+
target_tmp_file = "#{target_path}/#{File.basename(@escaped_source_path, ".*")}.#{File.basename(@convert_to, ":*")}"
4143
FileUtils.cp target_tmp_file, @target
4244
}
4345
end

0 commit comments

Comments
 (0)