added converter for images in feeds

This commit is contained in:
Harry Bomrah 2016-03-07 17:11:07 +08:00
parent 0f168e366e
commit a8b35baa6f
1 changed files with 19 additions and 1 deletions

View File

@ -44,6 +44,23 @@ class AnnouncementFeedsController < ApplicationController
private 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) def get_announcements(uid)
bf = BulletinFeed.find_by(:uid => uid) rescue nil bf = BulletinFeed.find_by(:uid => uid) rescue nil
if !bf.nil? if !bf.nil?
@ -68,7 +85,8 @@ class AnnouncementFeedsController < ApplicationController
a["id"] = anns.uid a["id"] = anns.uid
a["title_translations"] = anns.title_translations a["title_translations"] = anns.title_translations
a["subtitle_translations"] = anns.subtitle_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["postdate"] = anns.postdate
a["image_description_translations"] = anns.image_description_translations a["image_description_translations"] = anns.image_description_translations
a["image"] = {} a["image"] = {}