2011-08-18 05:43:33 +00:00
|
|
|
class Admin::PurchasesController < ApplicationController
|
|
|
|
require "net/http"
|
|
|
|
require "uri"
|
|
|
|
require 'zip/zip'
|
|
|
|
|
2011-08-22 05:45:21 +00:00
|
|
|
layout "admin"
|
|
|
|
|
2011-08-18 05:43:33 +00:00
|
|
|
def index
|
|
|
|
@purchases = Purchase.all.entries
|
|
|
|
end
|
|
|
|
|
|
|
|
def download
|
|
|
|
@purchase = Purchase.first(:conditions => {:id => params[:id]})
|
|
|
|
download_purchase(@purchase, request.env['REQUEST_URI'].split('admin')[0].chop)
|
|
|
|
@purchase.downloaded = true
|
|
|
|
@purchase.save
|
|
|
|
redirect_to admin_purchases_url
|
|
|
|
end
|
|
|
|
|
2011-11-16 07:21:31 +00:00
|
|
|
def install_app
|
|
|
|
unzip_app
|
|
|
|
redirect_to admin_purchases_url
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2011-08-18 05:43:33 +00:00
|
|
|
protected
|
|
|
|
|
|
|
|
def download_purchase(purchase, url)
|
2011-11-14 02:22:31 +00:00
|
|
|
uri = URI.parse("http://#{APP_CONFIG['store_ip']}/download/purchase")
|
2011-08-18 05:43:33 +00:00
|
|
|
http = Net::HTTP.new(uri.host, uri.port)
|
|
|
|
request = Net::HTTP::Post.new(uri.request_uri)
|
|
|
|
|
|
|
|
public_key = OpenSSL::PKey::RSA.new(File.read(APP_CONFIG['main_public_key']))
|
|
|
|
encrypted_data = public_key.public_encrypt({:purchase_id => purchase.purchase_id,
|
2012-07-30 10:34:54 +00:00
|
|
|
:purchase_type => purchase.class.to_s,
|
2011-08-18 05:43:33 +00:00
|
|
|
:roaming_id => Site.find(session[:site]).roaming_id,
|
|
|
|
:url => url}.to_json)
|
|
|
|
|
|
|
|
request.set_form_data({ :purchase => encrypted_data})
|
|
|
|
response = http.request(request)
|
|
|
|
temp_file = Tempfile.new("temp_file")
|
|
|
|
temp_file.binmode
|
|
|
|
temp_file.write response.body
|
|
|
|
temp_file.rewind
|
|
|
|
zip_name = response['content-disposition'].split('filename=')[1].gsub(/[\\\"]|.zip/, '') rescue ''
|
2012-07-30 10:34:54 +00:00
|
|
|
case purchase.class.to_s
|
2011-11-19 06:33:26 +00:00
|
|
|
when 'Pdesign'
|
|
|
|
unzip_design(temp_file, zip_name)
|
|
|
|
when 'PModuleApp'
|
|
|
|
unzip_module_app(temp_file, zip_name)
|
|
|
|
end
|
2011-08-18 05:43:33 +00:00
|
|
|
temp_file.close
|
|
|
|
temp_file.unlink
|
|
|
|
end
|
|
|
|
|
|
|
|
def unzip_design(file, zip_name)
|
|
|
|
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|
|
|
|
|
design = Design.new.from_json(orig_zip.read("#{zip_name}/#{zip_name}.json"))
|
|
|
|
Dir.mktmpdir('f_path') { |dir|
|
|
|
|
|
2011-09-13 03:57:20 +00:00
|
|
|
build_file(orig_zip, zip_name, dir, design.layout) if design.layout
|
2012-10-24 10:40:38 +00:00
|
|
|
build_file(orig_zip, zip_name, dir, design.css_default) if design.css_default
|
|
|
|
build_file(orig_zip, zip_name, dir, design.css_reset) if design.css_reset
|
2011-08-18 05:43:33 +00:00
|
|
|
|
|
|
|
['themes', 'javascripts', 'images'].each do |type|
|
2011-08-20 14:54:07 +00:00
|
|
|
design.send(type).each do |object|
|
2011-09-13 03:57:20 +00:00
|
|
|
build_file(orig_zip, zip_name, dir, object, type)
|
2011-08-18 05:43:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
temp_file.close
|
|
|
|
temp_file.unlink
|
|
|
|
design.save
|
|
|
|
}
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
2011-09-13 03:57:20 +00:00
|
|
|
|
2011-11-16 07:21:31 +00:00
|
|
|
def unzip_app
|
|
|
|
zip_name = "NewBlog"
|
|
|
|
file = "#{Rails.root}/lib/NewBlog.zip"
|
|
|
|
destination = "#{Rails.root}/vendor/plugins"
|
|
|
|
|
|
|
|
Zip::ZipFile.open(file) do |zip_file|
|
|
|
|
orig_file = file
|
|
|
|
|
|
|
|
temp_file = Tempfile.new("temp_file_zip")
|
|
|
|
temp_file.binmode
|
|
|
|
temp_file.write orig_file
|
|
|
|
temp_file.rewind
|
2011-11-16 09:01:44 +00:00
|
|
|
|
|
|
|
app = AppModule.new.from_json(zip_file.read("#{zip_name}/#{zip_name}/#{zip_name}.json"))
|
|
|
|
zip_file.each do |f|
|
|
|
|
if (f.to_s=~/^#{zip_name}\/#{zip_name}\/*/)
|
|
|
|
f_path=File.join(destination, f.name.gsub(/^#{zip_name}\//, ''))
|
|
|
|
FileUtils.mkdir_p(File.dirname(f_path))
|
|
|
|
zip_file.extract(f, f_path) unless File.exist?(f_path)
|
|
|
|
end
|
|
|
|
end
|
2011-11-16 07:21:31 +00:00
|
|
|
|
2011-11-16 09:01:44 +00:00
|
|
|
log = Logger.new(STDOUT)
|
|
|
|
log.level = Logger::WARN
|
|
|
|
log.warn("App unzip procress is finished,please restart the server to apply effect")
|
2011-11-16 07:21:31 +00:00
|
|
|
|
2011-11-16 09:01:44 +00:00
|
|
|
#TODO Perform touch tmp/restart to restart in production env
|
2011-11-16 07:21:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-19 06:33:26 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-09-13 03:57:20 +00:00
|
|
|
def build_file(orig_zip, zip_name, dir, object, type = nil)
|
2012-01-24 03:36:24 +00:00
|
|
|
title = object.file_identifier
|
2011-09-13 03:57:20 +00:00
|
|
|
temp = File.new(dir + '/' + title, 'w+')
|
|
|
|
temp.write orig_zip.read(zip_name + '/' + (type ? (type + '/') : '') + title)
|
|
|
|
object.file = temp
|
|
|
|
object.to_save = true
|
|
|
|
end
|
2011-08-18 05:43:33 +00:00
|
|
|
|
2011-11-16 07:21:31 +00:00
|
|
|
def unzip_file (file, destination)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2011-08-18 05:43:33 +00:00
|
|
|
end
|