From da614597fe16b34a47c7ec0ec6be8f9af738735f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Wed, 10 Jul 2013 22:55:37 +0200 Subject: [PATCH] post process attachments --- lib/cooked_post_processor.rb | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rb index 97507b86bed..84db9207449 100644 --- a/lib/cooked_post_processor.rb +++ b/lib/cooked_post_processor.rb @@ -16,10 +16,26 @@ class CookedPostProcessor end def post_process + post_process_attachments post_process_images post_process_oneboxes end + def post_process_attachments + attachments.each do |attachment| + href = attachment['href'] + attachment['href'] = relative_to_absolute(href) + if upload = Upload.get_from_url(href) + # update reverse index + associate_to_post(upload) + # append the size + append_human_size!(attachment, upload) + end + # mark as dirty + @dirty = true + end + end + def post_process_images images = extract_images return if images.blank? @@ -33,7 +49,7 @@ class CookedPostProcessor # make sure the img has proper width and height attributes update_dimensions!(img) # retrieve the associated upload, if any - if upload = Upload.get_from_url(img['src']) + if upload = Upload.get_from_url(src) # update reverse index associate_to_post(upload) end @@ -209,6 +225,22 @@ class CookedPostProcessor rescue URI::InvalidURIError end + def attachments + if SiteSetting.enable_s3_uploads? + @doc.css("a[href^=\"#{S3.base_url}\"]") + else + # local uploads are identified using a relative uri + @doc.css("a[href^=\"#{LocalStore.directory}\"]") + end + end + + def append_human_size!(attachment, upload) + size = Nokogiri::XML::Node.new("span", @doc) + size["class"] = "size" + size.content = "(#{number_to_human_size(upload.filesize)})" + attachment.add_next_sibling(size) + end + def dirty? @dirty end