diff --git a/app/assets/javascripts/admin/filemanager.js b/app/assets/javascripts/admin/filemanager.js index f005317..ac26a68 100644 --- a/app/assets/javascripts/admin/filemanager.js +++ b/app/assets/javascripts/admin/filemanager.js @@ -21,8 +21,8 @@ $(function () { 'use strict'; // Initialize the jQuery File Upload widget: $('#fileupload').fileupload({ - maxFileSize: 5000000, - acceptFileTypes: /(\.|\/)(gif|jpe?g|png|pdf|doc|docx|ppt|pptx|xls|xlsx)$/i, + maxFileSize: 10000000, + acceptFileTypes: /(\.|\/)(gif|jpe?g|png|pdf|doc|docx|ppt|pptx|xls|xlsx|csv|txt |zip|rar|tar|gz)$/i, dropZone: $('#dropzone'), headers:{ 'X-CSRF-Token': $('meta[name="csrf-token"]').attr("content") diff --git a/app/controllers/admin/import_controller.rb b/app/controllers/admin/import_controller.rb index 96fc3c6..b5e6718 100644 --- a/app/controllers/admin/import_controller.rb +++ b/app/controllers/admin/import_controller.rb @@ -4,186 +4,191 @@ require "uri" require 'json' class Admin::ImportController < OrbitAdminController - before_action :get_data ,:except=>'index' + layout "structure" + + def check_url + params['url'] = params['url']+"/" if params['url'].last!="/" + uri = URI.parse(params['url']) - def rss2_news - @new_category_ids = {} - module_id = ModuleApp.where(:key=>"announcement").first.id - - @categories.each do |category| - x = Category.where(:title => category["zh_tw"]).first - if x.nil? - cat = Category.new - titles = {} - @site_valid_locales.each do |locale| - titles[locale] = category[locale.to_s] - end - cat.title_translations = titles - cat.module_app_id = module_id - cat.save! - @new_category_ids[category["id"]] = cat.id - else - @new_category_ids[category["id"]] = x.id - end + begin + @host = uri.host + http = Net::HTTP.new(uri.host, uri.port) + request = Net::HTTP::Get.new(uri.request_uri) + response = http.request(request) + data = response.body + data = JSON.parse(data) + msg = data['status'] + rescue + msg = "Failed to connect to RSS2 (#{uri.to_s})" end - @data.each do |row| - bulletin = Bulletin.new - bulletin.update_user = @user - bulletin.title_translations = row['Title'] - bulletin.subtitle_translations = row['Summary'] - - next if row['Title']==[] and row['Summary']==[] and row['Content']==[] - - @site_valid_locales.each do |locale| - locale = locale.to_s - if row['Content'][locale]=="" and row['Summary'][locale]!="" - row['Content'][locale] = row['Summary'][locale] - row['Summary'][locale] = " " - end - - temp = row['Content'][locale] - - 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| - next if url.nil? - a = Asset.new - a.remote_data_url = url - a.title_translations = {"en" => a.data.filename, "zh_tw" => a.data.filename} - a.save! - @user.assets << a - temp.gsub!(CGI::escapeHTML(url), a.data.to_s) - end - row['Content'][locale] = temp - end - - bulletin.text_translations = row['Content'] - bulletin.category = Category.find(@new_category_ids[row["Category"]]) - bulletin.view_count = row["Visits"].blank? ? 0 : row["Visits"] - bulletin.postdate = row["PostDate"] - bulletin.deadline = row['Deadline'] - bulletin.remote_image_url = row["Pic"] if row["Pic"] - - if row["URL"] && row['URL'] != "" - bl = BulletinLink.new - bl.url = row["URL"] - bl.title_translations = {"en" => "Link", "zh_tw" => "Link"} - bl.save! - bulletin.bulletin_links << bl - 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.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! - - bulletin.bulletin_files << bf - - File.delete(file) - end - - bulletin.save! - end - - I18n.locale = @org_locale - - redirect_to('/admin/announcements') + render :json=>{"status"=>msg} end - def rss2_archive - unless @site_url.nil? - module_app = ModuleApp.where(:key=>"archive").first + def module_categories + module_app = ModuleApp.find_by(:key=>params['module']) + uri = URI.parse(params['url']) + http = Net::HTTP.new(uri.host, uri.port) + request = Net::HTTP::Get.new(uri.request_uri) + response = http.request(request) + data = response.body + categories = JSON.parse(data) - @new_categories = {} - @categories.each do |id, category| - old_cate = Category.where(:title => category['title']["zh-tw"]).first - if old_cate.nil? - cate = Category.new - titles = {} - @site_valid_locales.each do |locale| - titles[locale] = category['title'][locale.to_s.gsub('_', '-')] - end - cate.title_translations = titles - cate.save! - module_app.categories << cate - @new_categories[id] = cate.id - else - @new_categories[id] = old_cate.id - end - end - I18n.locale = :en - - @data.each do |id, file| - titles = {} - @site_valid_locales.each do |locale| - titles[locale] = file['title'][locale.to_s.gsub('_', '-')] - end - - the_file = ArchiveFileMultiple.new({ - :file_title_translations=>titles, - :choose_lang => @site_valid_locales, - :remote_file_url => @site_url+file['url'], - :sort_number=>file['sort_number'] - }) - the_file.save - - archive_file = ArchiveFile.new({ - :title_translations=>titles, - :sort_number=>file['sort_number'] - }) - archive_file.category = Category.find(@new_categories[file["category"]]) - - archive_file.create_user_id = current_user.id - archive_file.update_user_id = current_user.id - - archive_file.save - archive_file.archive_file_multiples << the_file - end - end - - I18n.locale = @org_locale - - redirect_to admin_archive_files_path - end - - private - - def get_data - @org_locale = I18n.locale I18n.locale = :zh_tw + categories.each do |id, category| + old_cate = module_app.categories.where(:title => category["zh_tw"]).first + if old_cate.nil? + cat = Category.new + cat.title_translations = category + cat.save! + module_app.categories << cat + categories[id]['id'] = cat.id.to_s + else + categories[id]['id'] = old_cate.id.to_s + end + end + render :json=>categories + end - @site_url = params['site_url'] - @url = params['url'] - uri = URI.parse(@url) - - @host = uri.host + def module_data_list + uri = URI.parse(params['url']) + http = Net::HTTP.new(uri.host, uri.port) + request = Net::HTTP::Get.new(uri.request_uri) + response = http.request(request) + data = response.body + data = JSON.parse(data) + render :json=>data + end + + def module_data + uri = URI.parse(params['url']) http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Get.new(uri.request_uri) response = http.request(request) data = response.body data = JSON.parse(data) - @categories = data['categories'] - @data = data['data'] + case params['module'] + when 'archive' + import_archive(data) + when 'announcement' + import_announcement(data) + end + render :json=>['status'=>'ok'] + end + + def import_archive(file) + archive_file = ArchiveFile.where(:rss2_sn=>file['Sn']).first + if archive_file.nil? + archive_file = ArchiveFile.new + archive_file.rss2_sn = file['Sn'] + end + + archive_file.title_translations = file['title'] + archive_file.sort_number = file['sort_number'] + archive_file.category = Category.find(params["category"]) + archive_file.create_user_id = current_user.id + archive_file.update_user_id = current_user.id + archive_file.save + + if(file['url']) + archive = ArchiveFileMultiple.new({ + :file_title_translations=>file['title'], + :choose_lang => @site_valid_locales, + :remote_file_url => file['url'], + :sort_number=>file['sort_number'] + }) + archive.save + archive_file.archive_file_multiples << archive + end + + if(file['url2']) + archive = ArchiveFileMultiple.new({ + :file_title_translations=>file['title'], + :choose_lang => @site_valid_locales, + :remote_file_url => file['url2'], + :sort_number=>file['sort_number'] + }) + archive.save + archive_file.archive_file_multiples << archive + end + end + + def import_announcement(news) + bulletin = Bulletin.where(:rss2_sn=>news['Sn']).first + if bulletin.nil? + bulletin = Bulletin.new + bulletin.rss2_sn = news['Sn'] + end + bulletin.update_user = current_user + bulletin.title_translations = news['Title'] + bulletin.subtitle_translations = news['Summary'] + + @site_valid_locales.each do |locale| + locale = locale.to_s + if news['Content'][locale]=="" and news['Summary'][locale]!="" + news['Content'][locale] = news['Summary'][locale] + news['Summary'][locale] = " " + end + + temp = news['Content'][locale] + + 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| + next if url.nil? + a = Asset.new + a.remote_data_url = url + a.title_translations = {"en" => a.data.filename, "zh_tw" => a.data.filename} + a.save! + @user.assets << a + temp.gsub!(CGI::escapeHTML(url), a.data.to_s) + end + news['Content'][locale] = temp + end + + bulletin.text_translations = news['Content'] + bulletin.category = Category.find(params["category"]) + bulletin.view_count = news["Visits"].blank? ? 0 : news["Visits"] + bulletin.postdate = news["PostDate"] + bulletin.deadline = news['Deadline'] + bulletin.remote_image_url = news["Pic"] if news["Pic"] + + if news["URL"] && news['URL'] != "" + bl = BulletinLink.new + bl.url = news["URL"] + bl.title_translations = {"en" => "Link", "zh_tw" => "Link"} + bl.save! + bulletin.bulletin_links << bl + end + news['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.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! + + bulletin.bulletin_files << bf + + File.delete(file) + end - @user = User.where(:user_name => "rulingcom").first + bulletin.save! end end \ No newline at end of file diff --git a/app/views/admin/import/index.html.erb b/app/views/admin/import/index.html.erb index 1dc20d3..b6bb55c 100644 --- a/app/views/admin/import/index.html.erb +++ b/app/views/admin/import/index.html.erb @@ -1,31 +1,291 @@ -