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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
+3
View File
@@ -0,0 +1,3 @@
class PDesign < Purchase
end
+11
View File
@@ -0,0 +1,11 @@
class Purchase
include Mongoid::Document
include Mongoid::Timestamps
field :purchase_id
field :title
field :author
field :intro
field :downloaded
end
+17
View File
@@ -6,4 +6,21 @@ class Site
field :in_use_locales, :type => Array
field :valid_locales, :type => Array
field :roaming_id
field :private_key, :type => Binary
field :public_key, :type => Binary
field :school
field :department
def generate_keys
private_key = OpenSSL::PKey::RSA.generate(2048)
self.public_key = private_key.public_key.to_s
self.private_key = private_key.to_s
end
def registered?
!self.roaming_id.blank?
end
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
+10
View File
@@ -0,0 +1,10 @@
<% @purchases.each do |purchase| %>
<%= purchase.title %> |
<%= purchase.intro %> |
<%= purchase.author %> |
<% if purchase.downloaded %>
Downloaded
<% else %>
<%= link_to 'Download', download_admin_purchase_path(purchase) %>
<% end %>
<% end %>
-6
View File
@@ -51,12 +51,6 @@
<p>Rulingcom</p>
</div>
</div>
<%= javascript_include_tag "jquery",
"jquery-ui",
"rails",
"easy",
"application", :cache => 'all' %>
<%= yield :page_specific_javascript %>
</body>
</html>
+2 -12
View File
@@ -5,12 +5,8 @@
<title>R4</title>
<link rel="shortcut icon" href="/favicon.ico">
<%= yield :page_specific_link %>
<%= stylesheet_link_tag "easy", "main", "devise", :media => "screen, projection" %>
<%= stylesheet_link_tag "easyprint", :media => "print" %>
<!--[if IE]>
<%= stylesheet_link_tag "ie", :media => "screen, projection" %>
<![endif]-->
<%= yield :page_specific_css %>
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tag %>
</head>
<body>
@@ -36,12 +32,6 @@
<p>Rulingcom</p>
</div>
</div>
<%#= javascript_include_tag "jquery",
"jquery-ui",
"rails",
"easy",
"application", :cache => 'all' %>
<%= yield :page_specific_javascript %>
</body>
</html>
+2 -13
View File
@@ -5,13 +5,8 @@
<title>R4</title>
<link rel="shortcut icon" href="/favicon.ico">
<%= yield :page_specific_link %>
<%= stylesheet_link_tag "rulingcom/easy", :media => "screen, projection" %>
<%= stylesheet_link_tag "rulingcom/easyprint", :media => "print" %>
<%= stylesheet_link_tag "application", :media => "screen, projection" %>
<!--[if IE]>
<%= stylesheet_link_tag "ie", :media => "screen, projection" %>
<![endif]-->
<%= yield :page_specific_css %>
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tag %>
</head>
<body>
@@ -46,12 +41,6 @@
<p>Rulingcom</p>
</div>
</div>
<%= javascript_include_tag "jquery",
"jquery-ui",
"rails",
"easy",
"application", :cache => 'all' %>
<%= yield :page_specific_javascript %>
</body>
</html>
+7 -1
View File
@@ -1 +1,7 @@
<%= @title %>
<% if @site && @site.registered? %>
registered
<% else %>
<%= link_to 'register', 'http://localhost:3001/user/sites/register?' +
{:site => {:school => @site.school, :department => @site.department}}.to_query,
:target => '_blank' %>
<% end %>