From a3d060710af7c51cca5dc872991bb3ee8819284e Mon Sep 17 00:00:00 2001 From: Manson Wang Date: Thu, 6 Mar 2014 14:07:22 +0800 Subject: [PATCH] Fix import news from Rss2 authentication and upload filename --- app/controllers/admin/import_controller.rb | 56 ++++++++++++++-------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/app/controllers/admin/import_controller.rb b/app/controllers/admin/import_controller.rb index b5b3c8c..40696e9 100644 --- a/app/controllers/admin/import_controller.rb +++ b/app/controllers/admin/import_controller.rb @@ -1,8 +1,11 @@ +# encoding: utf-8 require "net/http" require "uri" require 'json' -class Admin::ImportController < ApplicationController +class Admin::ImportController < OrbitBackendController + + open_for_admin def rss2_news @url = params['url'] @@ -42,32 +45,33 @@ class Admin::ImportController < ApplicationController @user = User.where(:user_id => "rulingcom").first @data.each do |row| + # next if row['files'].size == 0 bulletin = Bulletin.new bulletin.title_translations = row['Title'] bulletin.subtitle_translations = row['Summary'] @site_valid_locales.each do |locale| - temp = row['Content'][locale] + temp = row['Content'][locale] - urls = Nokogiri::HTML(temp).css("img").map do |link| - if URI.parse(link.attr("src")).host == @host - link.attr("src") + urls = Nokogiri::HTML(temp).css("img").map do |link| + if URI.parse(link.attr("src")).host == @host + link.attr("src") + end end + + urls.each do |url| + a = Asset.new + a.remote_data_url = url + a.assetable_type = "User" + a.assetable_id = @user.id + a.title = {"en" => a["data"], "zh_tw" => a["data"]} + a.save! + temp.sub!(url,a.data.to_s) + end + row['Content'][locale] = temp end - urls.each do |url| - a = Asset.new - a.remote_data_url = url - a.assetable_type = "User" - a.assetable_id = @user.id - a.title = {"en" => a["data"], "zh_tw" => a["data"]} - a.save! - temp.sub!(url,a.data.to_s) - end - row['Content'][locale] = temp - end - - bulletin.text_translations = row['Content'] + bulletin.text_translations = row['Content'] bulletin.category_id = @new_category_ids[row["Category"]] bulletin.view_count = row["Visits"] bulletin.postdate = row["PostDate"] @@ -83,10 +87,24 @@ class Admin::ImportController < ApplicationController end row['files'].each do |f| bf = BulletinFile.new + if f['url'].split('title=').size == 1 + f['url'] = f['url']+"檔案下載" + end + + f['title'] = "檔案下載" if f['title'].blank? + bf.remote_file_url = f['url'] bf.title_translations = {"en" => f['title'], "zh_tw" => f['title']} bf.bulletin_id = bulletin.id bf.save! + + # Rename uploaded file + file_ext = File.extname(f['url'].split('&')[0]) + file = File.new("tmp/uploads/#{bf.title}#{file_ext}","w+b") + file.write(bf.file.read) + bf.file.cache!(file) + bf.save! + File.delete(file) end bulletin.tagged_ids = [] @@ -95,6 +113,6 @@ class Admin::ImportController < ApplicationController I18n.locale = l - render :json => {"success"=>true}.to_json + redirect_to(panel_announcement_back_end_bulletins_url) end end \ No newline at end of file