Merge server and rails_3_1

This commit is contained in:
chris2tof committed 2011-08-18 13:43:33 +08:00
1 parent d2c2e174ac
commit 86443661e9
42 files changed
+274 -190

No files matched your search

@@ -0,0 +1,88 @@
class Admin::PurchasesController < ApplicationController
require "net/http"
require "uri"
require 'zip/zip'
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://localhost:3001/download/design")
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,
: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 ''
debugger
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|
title = design.layout.file_filename
temp = File.new(dir + '/' + title, 'w+')
temp.write orig_zip.read(zip_name + '/' + title)
design.layout.file = temp
title = design.structure_css_filename
temp = File.new(dir + '/' + title, 'w+')
temp.write orig_zip.read(zip_name + '/' + title)
design.layout.file = temp
['themes', 'javascripts', 'images'].each do |type|
eval("design.#{type}").each do |object|
title = object.file_filename
temp = File.new(dir + '/' + title, 'w+')
temp.write orig_zip.read(zip_name + '/' + type + '/' + title)
object.file = temp
object.to_save = true
end
end
}
temp_file.close
temp_file.unlink
design.save
}
end
end
end
+18
View File
@@ -25,6 +25,8 @@ class ApplicationController < ActionController::Base
end
end
protected
# Set I18n.locale
def set_locale
# update session if passed
@@ -55,5 +57,21 @@ class ApplicationController < ActionController::Base
def set_current_item
session[:current_page] = params[:id] || @item.id rescue nil
end
def decrypt_data(encrypted_data, encrypted_key, encrypted_iv)
site = Site.find(session[:site])
if encrypted_data
private_key = OpenSSL::PKey::RSA.new(site.private_key)
cipher = OpenSSL::Cipher.new('aes-256-cbc')
cipher.decrypt
cipher.key = private_key.private_decrypt(encrypted_key)
cipher.iv = private_key.private_decrypt(encrypted_iv)
decrypted_data = cipher.update(encrypted_data)
decrypted_data << cipher.final
else
''
end
end
end
+16
View File
@@ -0,0 +1,16 @@
require 'mongo'
class GridfsController < ActionController::Metal
def serve
gridfs_path = env["PATH_INFO"].gsub("/gridfs/", "")
begin
gridfs_file = Mongo::GridFileSystem.new(Mongoid.database).open(gridfs_path, 'r')
self.response_body = gridfs_file.read
self.content_type = gridfs_file.content_type
rescue
self.status = :file_not_found
self.content_type = 'text/plain'
self.response_body = ''
end
end
end
+1 -5
View File
@@ -4,11 +4,7 @@ class SitesController < ApplicationController
def index
@site = Site.first
if @site
@title = "site"
else
redirect_to :action => :new
end
redirect_to :action => :new unless @site
end
def new