From a8b35baa6fef33d51a54a9e7eeeff21ba804ae27 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Mon, 7 Mar 2016 17:11:07 +0800 Subject: [PATCH] added converter for images in feeds --- .../announcement_feeds_controller.rb | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/app/controllers/announcement_feeds_controller.rb b/app/controllers/announcement_feeds_controller.rb index 974748c..2e6b2db 100644 --- a/app/controllers/announcement_feeds_controller.rb +++ b/app/controllers/announcement_feeds_controller.rb @@ -44,6 +44,23 @@ class AnnouncementFeedsController < ApplicationController private + def smart_convertor(text) + html_string = text + links = html_string.scan(/img.*?src="(.*?)"/i) + links.each do |link| + l = link.first + new_link = nil + if l.starts_with?("/") + new_link = request.protocol + request.host_with_port + l + elsif l.starts_with?("..") + l1 = l.gsub("../","") + new_link = request.protocol + request.host_with_port + "/" + l1 + end + html_string = html_string.sub(l,new_link) if !new_link.nil? + end + return html_string + end + def get_announcements(uid) bf = BulletinFeed.find_by(:uid => uid) rescue nil if !bf.nil? @@ -68,7 +85,8 @@ class AnnouncementFeedsController < ApplicationController a["id"] = anns.uid a["title_translations"] = anns.title_translations a["subtitle_translations"] = anns.subtitle_translations - a["text_translations"] = anns.text_translations + a["text_translations"]["en"] = smart_convertor(anns.text_translations["en"]) + a["text_translations"]["zh_tw"] = smart_convertor(anns.text_translations["zh_tw"]) a["postdate"] = anns.postdate a["image_description_translations"] = anns.image_description_translations a["image"] = {}