88 lines
3.0 KiB
Ruby
88 lines
3.0 KiB
Ruby
class Admin::PurchasesController < ApplicationController
|
|
require "net/http"
|
|
require "uri"
|
|
require 'zip/zip'
|
|
|
|
layout "admin"
|
|
|
|
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
|
|
|
|
protected
|
|
|
|
def download_purchase(purchase, url)
|
|
uri = URI.parse("http://#{APP_CONFIG['store_ip']}/download/purchase")
|
|
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,
|
|
:purchase_type => purchase._type,
|
|
: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 ''
|
|
unzip_design(temp_file, zip_name)
|
|
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|
|
|
|
|
build_file(orig_zip, zip_name, dir, design.layout) if design.layout
|
|
build_file(orig_zip, zip_name, dir, design.default_css) if design.default_css
|
|
build_file(orig_zip, zip_name, dir, design.reset_css) if design.reset_css
|
|
|
|
['themes', 'javascripts', 'images'].each do |type|
|
|
design.send(type).each do |object|
|
|
build_file(orig_zip, zip_name, dir, object, type)
|
|
end
|
|
end
|
|
}
|
|
temp_file.close
|
|
temp_file.unlink
|
|
design.save
|
|
}
|
|
|
|
end
|
|
end
|
|
|
|
def build_file(orig_zip, zip_name, dir, object, type = nil)
|
|
title = object.file_filename
|
|
temp = File.new(dir + '/' + title, 'w+')
|
|
temp.write orig_zip.read(zip_name + '/' + (type ? (type + '/') : '') + title)
|
|
object.file = temp
|
|
object.to_save = true
|
|
end
|
|
|
|
end |