Skip to content

Instantly share code, notes, and snippets.

@nigeljonez
Created July 18, 2019 09:53
Show Gist options
  • Save nigeljonez/4204a8f20e0ebaaa103197c8a0a69322 to your computer and use it in GitHub Desktop.
Save nigeljonez/4204a8f20e0ebaaa103197c8a0a69322 to your computer and use it in GitHub Desktop.
RequestController.class_eval do
alias_method :orig_get_attachment, :get_attachment
def skip_attachment_cache
incoming_message = IncomingMessage.find(params[:incoming_message_id])
raise ActiveRecord::RecordNotFound.new("Message not found") if incoming_message.nil?
ir = incoming_message.info_request
(!authenticated_user.nil?) && (@user == ir.user || @user.is_admin?)
end
skip_around_action :cache_attachments, :if => :skip_attachment_cache
# Special version of get_attachment()
def get_attachment
if !skip_attachment_cache
orig_get_attachment()
logger.info "using regular get_attachment"
else
logger.info "using new get_attachment"
get_attachment_internal(false)
return unless @attachment
# we don't use @attachment.content_type here, as we want same mime type when cached in cache_attachments above
content_type =
AlaveteliFileTypes.filename_to_mimetype(params[:file_name]) ||
'application/octet-stream'
body = @attachment.default_body
if content_type == 'text/html'
body =
Loofah.scrub_document(body, :prune).
to_html(encoding: 'UTF-8').
try(:html_safe)
end
logger.debug "Skipping cache"
render :body => body, :content_type => content_type
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment