From dac6348402be1c8cc4885b5b6e1de9651350a499 Mon Sep 17 00:00:00 2001 From: chris2tof Date: Sat, 19 Nov 2011 14:33:26 +0800 Subject: [PATCH] Integration of module_app workflow --- app/controllers/admin/purchases_controller.rb | 60 ++++++++++++++++++- app/models/{app_module.rb => module_app.rb} | 2 +- app/models/purchase/p_module_app.rb | 3 + app/views/admin/sites/_registration.html.erb | 4 +- app/views/admin/sites/index.html.erb | 4 +- lib/central_server_exchange_app.rb | 2 + lib/tasks/dev.rake | 2 +- vendor/plugins/NewBlog/NewBlog.json | 2 +- 8 files changed, 71 insertions(+), 8 deletions(-) rename app/models/{app_module.rb => module_app.rb} (92%) create mode 100644 app/models/purchase/p_module_app.rb diff --git a/app/controllers/admin/purchases_controller.rb b/app/controllers/admin/purchases_controller.rb index d6431ebed..40a042f44 100644 --- a/app/controllers/admin/purchases_controller.rb +++ b/app/controllers/admin/purchases_controller.rb @@ -43,7 +43,12 @@ class Admin::PurchasesController < ApplicationController temp_file.write response.body temp_file.rewind zip_name = response['content-disposition'].split('filename=')[1].gsub(/[\\\"]|.zip/, '') rescue '' - unzip_design(temp_file, zip_name) + case purchase._type + when 'Pdesign' + unzip_design(temp_file, zip_name) + when 'PModuleApp' + unzip_module_app(temp_file, zip_name) + end temp_file.close temp_file.unlink end @@ -113,6 +118,59 @@ class Admin::PurchasesController < ApplicationController end end + def unzip_module_app(file, zip_name) + destination = "#{Rails.root}/vendor/plugins" + Zip::ZipFile.open(file) do |zip_file| + encrypted = {} + ['encrypted_data', 'encrypted_key', 'encrypted_iv'].each do |e| + encrypted.merge!(e => zip_file.read(e)) + end + orig_file = decrypt_data(encrypted['encrypted_data'], encrypted['encrypted_key'], encrypted['encrypted_iv']) + + temp_file = Tempfile.new("temp_file_zip") + temp_file.binmode + temp_file.write orig_file + temp_file.rewind + + Zip::ZipFile.open(temp_file) { |orig_zip| + module_app = ModuleApp.new.from_json(orig_zip.read("#{zip_name}/#{zip_name}.json")) + orig_zip.remove("#{zip_name}/#{zip_name}.json") + + orig_zip.each do |orig| + inner_temp_file = Tempfile.new("inner_temp_file_zip") + inner_temp_file.binmode + inner_temp_file.write orig_zip.read(orig) + inner_temp_file.rewind + + Zip::ZipFile.open(inner_temp_file) {|inner_zip| + inner_zip.each do |f| + if (f.to_s=~/^#{zip_name}\/*/) + f_path = File.join(destination, f.name) + FileUtils.mkdir_p(File.dirname(f_path)) + inner_zip.extract(f, f_path) unless File.exist?(f_path) + end + end + } + inner_temp_file.close + inner_temp_file.unlink + end + + module_app.save + + log = Logger.new(STDOUT) + log.level = Logger::WARN + log.warn("App unzip procress is finished,please restart the server to apply effect") + } + + temp_file.close + temp_file.unlink + + end + end + + + + def build_file(orig_zip, zip_name, dir, object, type = nil) title = object.file_filename temp = File.new(dir + '/' + title, 'w+') diff --git a/app/models/app_module.rb b/app/models/module_app.rb similarity index 92% rename from app/models/app_module.rb rename to app/models/module_app.rb index d25f18917..f692fb211 100644 --- a/app/models/app_module.rb +++ b/app/models/module_app.rb @@ -1,4 +1,4 @@ -class AppModule +class ModuleApp include Mongoid::Document include Mongoid::Timestamps diff --git a/app/models/purchase/p_module_app.rb b/app/models/purchase/p_module_app.rb new file mode 100644 index 000000000..18598b2a7 --- /dev/null +++ b/app/models/purchase/p_module_app.rb @@ -0,0 +1,3 @@ +class PModuleApp < Purchase + +end \ No newline at end of file diff --git a/app/views/admin/sites/_registration.html.erb b/app/views/admin/sites/_registration.html.erb index 413b864f6..062db5b97 100644 --- a/app/views/admin/sites/_registration.html.erb +++ b/app/views/admin/sites/_registration.html.erb @@ -1,5 +1,5 @@ -<%# if @site && @site.registered? %> -<% if @site %> +<% if @site && @site.registered? %> +<%# if @site %>
<%= t('admin.registered') %>
diff --git a/app/views/admin/sites/index.html.erb b/app/views/admin/sites/index.html.erb index 6739cbe55..ec22707c9 100644 --- a/app/views/admin/sites/index.html.erb +++ b/app/views/admin/sites/index.html.erb @@ -3,11 +3,11 @@ - + - + diff --git a/lib/central_server_exchange_app.rb b/lib/central_server_exchange_app.rb index 536cd21f1..518c876e7 100644 --- a/lib/central_server_exchange_app.rb +++ b/lib/central_server_exchange_app.rb @@ -35,6 +35,8 @@ class CentralServerExchangeApp < Sinatra::Base case params[:type] when 'Design' purchase = PDesign.new.from_json(private_key.private_decrypt(request.params['purchase'])) + when 'ModuleApp' + purchase = PModuleApp.new.from_json(private_key.private_decrypt(request.params['purchase'])) end purchase.save diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index 975ed3362..c905115d3 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -11,7 +11,7 @@ namespace :dev do include end.each(&:drop) - Site.create( :school => 'The Best School', :department => 'Awesome', :valid_locales => [ 'en', 'zh_tw' ], :in_use_locales => [ 'zh_tw', 'en' ]) + Site.create( :school => 'RulingDigital Universitry', :department => 'Computer Science', :valid_locales => [ 'en', 'zh_tw' ], :in_use_locales => [ 'zh_tw', 'en' ]) user = User.new( :email => 'chris@rulingcom.com', :password => 'password', :password_confirmation => 'password', :admin => true, :active_role => 'teacher') user.user_roles.build(:key => 'teacher', :discipline_en => 'Database', :discipline_zh_tw => '資料庫' ) diff --git a/vendor/plugins/NewBlog/NewBlog.json b/vendor/plugins/NewBlog/NewBlog.json index 0197b9d05..1c0faa558 100644 --- a/vendor/plugins/NewBlog/NewBlog.json +++ b/vendor/plugins/NewBlog/NewBlog.json @@ -1,5 +1,5 @@ { - "name_of_app": "NewBlog", + "title": "NewBlog", "version": "0.1", "organization": "Rulingcom", "author": "RD dep",
School NameRulingDigital Universitry<%= @site.school %>
DepartmentComputer Science<%= @site.department %>
Version