lib/pdfkit/middleware.rb in pdfkit-middleware-to-file-0.5.4 vs lib/pdfkit/middleware.rb in pdfkit-middleware-to-file-0.5.5
- old
+ new
@@ -1,15 +1,29 @@
class PDFKit
-
+
class Middleware
def initialize(app, options = {}, conditions = {})
@app = app
@options = options
@conditions = conditions
end
+ def upload(file)
+ host = PDFKit.configure.upload_options[:host]
+ user = PDFKit.configure.upload_options[:user]
+ folder = PDFKit.configure.upload_options[:folder]
+ password = PDFKit.configure.upload_options[:password]
+
+ file_name = File.basename( file )
+ full_path = File.absolute_path( file )
+
+ Net::SFTP.start(host, user, :password => password) do |sftp|
+ data = sftp.upload!( full_path, "#{folder}/#{file_name}" )
+ end
+ end
+
def call(env)
@request = Rack::Request.new(env)
@render_pdf = false
set_request_to_render_as_pdf(env) if render_as_pdf?
@@ -17,10 +31,15 @@
if rendering_pdf? && headers['Content-Type'] =~ /text\/html|application\/xhtml\+xml/
body = response.respond_to?(:body) ? response.body : response.join
body = body.join if body.is_a?(Array)
pdf = PDFKit.new(translate_paths(body, env), @options)
- file = pdf.to_file( headers["PDF-Full-File-Path"] )
+
+ if headers["PDF-Full-File-Path"].present?
+ file = pdf.to_file( headers["PDF-Full-File-Path"] )
+ upload(file) if PDFKit.configure.upload_options[:host].present?
+ end
+
body = pdf.to_pdf
response = [body]
# Do not cache PDFs
headers.delete('ETag')