diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/request_controller.rb | 41 | ||||
-rw-r--r-- | app/helpers/link_to_helper.rb | 4 | ||||
-rw-r--r-- | app/models/incoming_message.rb | 33 | ||||
-rw-r--r-- | app/views/request/_bubble.rhtml | 13 | ||||
-rw-r--r-- | app/views/request/_view_html_prefix.rhtml | 14 | ||||
-rw-r--r-- | app/views/request/_view_html_stylesheet.rhtml | 1 |
6 files changed, 94 insertions, 12 deletions
diff --git a/app/controllers/request_controller.rb b/app/controllers/request_controller.rb index 0a195e30a..2789fed97 100644 --- a/app/controllers/request_controller.rb +++ b/app/controllers/request_controller.rb @@ -4,7 +4,7 @@ # Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: request_controller.rb,v 1.123 2008-10-17 16:51:40 francis Exp $ +# $Id: request_controller.rb,v 1.124 2008-10-17 20:32:42 francis Exp $ class RequestController < ApplicationController @@ -391,24 +391,51 @@ class RequestController < ApplicationController # Download an attachment caches_page :get_attachment def get_attachment + get_attachment_internal + + response.content_type = 'application/octet-stream' + if !@attachment.content_type.nil? + response.content_type = @attachment.content_type + end + render :text => @attachment.body + end + + def get_attachment_as_html + get_attachment_internal + html = @attachment.body_as_html + + view_html_stylesheet = render_to_string :partial => "request/view_html_stylesheet" + html.sub!("<head>", "<head>" + view_html_stylesheet) + + view_html_prefix = render_to_string :partial => "request/view_html_prefix" + html.sub!("<!--Section Begins-->", view_html_prefix + "<!--Section Begins-->") + + html.sub!("<!--Section Begins-->", '<!--Section Begins--><div class="view_html_content">') + html.sub!("<!--Section Ends->", '</div><!--Section Begins-->') + + # Mask any more emails that have now been exposed (e.g. in PDFs - ones in + # .doc will have been got in get_attachment_internal below) + html = IncomingMessage.binary_mask_all_emails(html) + + response.content_type = 'text/html' + render :text => html + end + + # Internal function + def get_attachment_internal @incoming_message = IncomingMessage.find(params[:incoming_message_id]) @info_request = @incoming_message.info_request if @incoming_message.info_request_id != params[:id].to_i raise sprintf("Incoming message %d does not belong to request %d", @incoming_message.info_request_id, params[:id]) end @part_number = params[:part].to_i + @filename = params[:file_name] @attachment = IncomingMessage.get_attachment_by_url_part_number(@incoming_message.get_attachments_for_display, @part_number) # Prevent spam to magic request address. # XXX Bit dodgy modifying a binary like this but hey. Maybe only do for some mime types? @attachment.body = IncomingMessage.binary_mask_all_emails(@attachment.body) - - response.content_type = 'application/octet-stream' - if !@attachment.content_type.nil? - response.content_type = @attachment.content_type - end - render :text => @attachment.body end # FOI officers can upload a response diff --git a/app/helpers/link_to_helper.rb b/app/helpers/link_to_helper.rb index e8be14162..d64ce253d 100644 --- a/app/helpers/link_to_helper.rb +++ b/app/helpers/link_to_helper.rb @@ -5,12 +5,12 @@ # Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: link_to_helper.rb,v 1.44 2008-10-03 17:09:06 francis Exp $ +# $Id: link_to_helper.rb,v 1.45 2008-10-17 20:32:42 francis Exp $ module LinkToHelper # Links to various models - + # Requests def request_url(info_request) return show_request_url(:url_title => info_request.url_title, :only_path => true) diff --git a/app/models/incoming_message.rb b/app/models/incoming_message.rb index 0b01f18d1..f76479f65 100644 --- a/app/models/incoming_message.rb +++ b/app/models/incoming_message.rb @@ -19,7 +19,7 @@ # Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: incoming_message.rb,v 1.152 2008-10-17 07:02:01 francis Exp $ +# $Id: incoming_message.rb,v 1.153 2008-10-17 20:32:42 francis Exp $ # TODO # Move some of the (e.g. quoting) functions here into rblib, as they feel @@ -161,6 +161,37 @@ class FOIAttachment return (s / 1024).to_s + 'K' end end + + def body_as_html + tempfile = Tempfile.new('foiextract') + tempfile.print body + tempfile.flush + + if content_type == 'application/vnd.ms-word' + # XXX do something with PNG files this spits out so they view too :) + system("/usr/bin/wvHtml " + tempfile.path + " " + tempfile.path + ".html") + html = File.read(tempfile.path + ".html") + File.unlink(tempfile.path + ".html") +# elsif content_type == 'application/pdf' +# IO.popen("/usr/bin/pdftohtml " + tempfile.path + " -", "r") do |child| +# html = child.read() + "\n\n" +# end + else + raise "No HTML conversion available for type " + content_type + end + + tempfile.close + return html + end + + def has_body_as_html? + if content_type == 'application/vnd.ms-word' + return true +# elsif content_type == 'application/pdf' +# return true + end + return false + end end class IncomingMessage < ActiveRecord::Base diff --git a/app/views/request/_bubble.rhtml b/app/views/request/_bubble.rhtml index 4787fcdcd..438925d51 100644 --- a/app/views/request/_bubble.rhtml +++ b/app/views/request/_bubble.rhtml @@ -4,8 +4,14 @@ <hr class="top"> <% attachments.each do |a| %> <p class="attachment"> - <% attachment_url = get_attachment_url(:id => incoming_message.info_request_id, - :incoming_message_id => incoming_message.id, :part => a.url_part_number, :file_name => a.display_filename) %> + <% + attachment_url = get_attachment_url(:id => incoming_message.info_request_id, + :incoming_message_id => incoming_message.id, :part => a.url_part_number, + :file_name => a.display_filename) + attachment_as_html_url = get_attachment_as_html_url(:id => incoming_message.info_request_id, + :incoming_message_id => incoming_message.id, :part => a.url_part_number, + :file_name => a.display_filename) + %> <% img_filename = "icon_" + a.content_type.sub('/', '_') + "_large.png" full_filename = File.join(File.dirname(__FILE__), "../../../public/images", img_filename) if File.exist?(full_filename) %> @@ -18,6 +24,9 @@ <br> <%= a.display_size %> <%= link_to "Download", attachment_url %> + <% if a.has_body_as_html? %> + <%= link_to "View as HTML", attachment_as_html_url %> + <% end %> <!-- (<%= a.content_type %>) --> </p> <% end %> diff --git a/app/views/request/_view_html_prefix.rhtml b/app/views/request/_view_html_prefix.rhtml new file mode 100644 index 000000000..ac620ffb9 --- /dev/null +++ b/app/views/request/_view_html_prefix.rhtml @@ -0,0 +1,14 @@ +<% + attachment_url = get_attachment_url(:id => @incoming_message.info_request_id, + :incoming_message_id => @incoming_message.id, :part => @part_number, + :file_name => @filename ) + +%> + +<div class="view_html_prefix"> + <div class="view_html_download_link"> + <%=link_to "Download original attachment", attachment_url %> + </div> + Attachment to FOI request '<%=link_to h(@info_request.title), incoming_message_url(@incoming_message)%>' (HTML version) +</div> + diff --git a/app/views/request/_view_html_stylesheet.rhtml b/app/views/request/_view_html_stylesheet.rhtml new file mode 100644 index 000000000..d6cb932a8 --- /dev/null +++ b/app/views/request/_view_html_stylesheet.rhtml @@ -0,0 +1 @@ +<%= stylesheet_link_tag 'main', :title => "Main", :rel => "stylesheet" %> |