Fix import news from Rss2 authentication and upload filename

This commit is contained in:
Manson Wang 2014-03-06 14:07:22 +08:00
parent 61e8962f96
commit a3d060710a
1 changed files with 37 additions and 19 deletions

View File

@ -1,8 +1,11 @@
# encoding: utf-8
require "net/http" require "net/http"
require "uri" require "uri"
require 'json' require 'json'
class Admin::ImportController < ApplicationController class Admin::ImportController < OrbitBackendController
open_for_admin
def rss2_news def rss2_news
@url = params['url'] @url = params['url']
@ -42,6 +45,7 @@ class Admin::ImportController < ApplicationController
@user = User.where(:user_id => "rulingcom").first @user = User.where(:user_id => "rulingcom").first
@data.each do |row| @data.each do |row|
# next if row['files'].size == 0
bulletin = Bulletin.new bulletin = Bulletin.new
bulletin.title_translations = row['Title'] bulletin.title_translations = row['Title']
bulletin.subtitle_translations = row['Summary'] bulletin.subtitle_translations = row['Summary']
@ -83,10 +87,24 @@ class Admin::ImportController < ApplicationController
end end
row['files'].each do |f| row['files'].each do |f|
bf = BulletinFile.new 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.remote_file_url = f['url']
bf.title_translations = {"en" => f['title'], "zh_tw" => f['title']} bf.title_translations = {"en" => f['title'], "zh_tw" => f['title']}
bf.bulletin_id = bulletin.id bf.bulletin_id = bulletin.id
bf.save! 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 end
bulletin.tagged_ids = [] bulletin.tagged_ids = []
@ -95,6 +113,6 @@ class Admin::ImportController < ApplicationController
I18n.locale = l I18n.locale = l
render :json => {"success"=>true}.to_json redirect_to(panel_announcement_back_end_bulletins_url)
end end
end end