description
-screen shot
-<%= @template['title'] %>
+ <%= link_to 'free download', buy_template_admin_purchase_path(@template['id']), :id => "download" %> +-
+
- views15,241 +
- color +
diff --git a/app/assets/stylesheets/rulingorbit.css.erb b/app/assets/stylesheets/rulingorbit.css.erb index dd53ef19..db2be30b 100644 --- a/app/assets/stylesheets/rulingorbit.css.erb +++ b/app/assets/stylesheets/rulingorbit.css.erb @@ -287,7 +287,7 @@ header { } #content .main { float: right; - width: 680px; + width: 676px; } #item-info .item-thumb { width: 210px; diff --git a/app/controllers/admin/purchases_controller.rb b/app/controllers/admin/purchases_controller.rb index f4d1ae2a..f1663c95 100644 --- a/app/controllers/admin/purchases_controller.rb +++ b/app/controllers/admin/purchases_controller.rb @@ -9,6 +9,17 @@ class Admin::PurchasesController < ApplicationController @purchases = Purchase.all.entries end + def buy_template + original_file, zip_name = download_template(params[:id]) + temp_file = Tempfile.new("temp_file_zip") + temp_file.binmode + temp_file.write original_file + temp_file.rewind + unzip_design(temp_file, zip_name) + temp_file.close + temp_file.unlink + end + def download @purchase = Purchase.first(:conditions => {:id => params[:id]}) download_purchase(@purchase, request.env['REQUEST_URI'].split('admin')[0].chop) @@ -183,4 +194,66 @@ class Admin::PurchasesController < ApplicationController end + + def download_template(id) + uri = URI.parse("http://#{APP_CONFIG['store_ip']}/download/design/#{id}") + http = Net::HTTP.new(uri.host, uri.port) + request = Net::HTTP::Get.new(uri.request_uri) + response = http.request(request) + [response.body, (response['content-disposition'].split('filename=')[1].gsub(/[\\\"]|.zip/, '') rescue '')] + end + + def unzip_design(input_file, zip_name) + temp_file = Tempfile.new("temp_file") + temp_file.write(input_file.read.force_encoding('UTF-8')) + temp_file.rewind + Zip::ZipFile.open(temp_file) { |zip_file| + design = Design.new.from_json(zip_file.read("#{zip_name}/info.json")) + Dir.mktmpdir('f_path') { |dir| + themes_entries = [] + javascripts_entries = [] + images_entries = [] + screenshots_entries = [] + + zip_file.entries.each do |entry| + case (path = entry.to_s) + when /\A(#{zip_name})\/(default\.css)\z/ #for default css + design.build_css_default(:file => get_temp_file(zip_file, dir, entry)) + when /\A(#{zip_name})\/(reset\.css)\z/ #for reset css + design.build_css_reset(:file => get_temp_file(zip_file, dir, entry)) + when /\A(#{zip_name})\/(layout\.html)\z/ #for layout html + design.build_layout(:file => get_temp_file(zip_file, dir, entry)) + when /\A(#{zip_name})\/(thumb((\.jpg)|(\.png)|(\.gif)))\z/ #for thumb + design.build_thumb(:file => get_temp_file(zip_file, dir, entry)) + when /\A(#{zip_name})\/(themes)\/.*(\.css)\z/ #for themes css + themes_entries << entry + when /\A(#{zip_name})\/(javascripts)\/.*(\.js)\z/ #for js + javascripts_entries << entry + when /\A(#{zip_name})\/(images)\/.*((\.jpg)|(\.png)|(\.gif))\z/ #for img + images_entries << entry + when /\A(#{zip_name})\/(screenshots)\/.*((\.jpg)|(\.png)|(\.gif))\z/ #for img + screenshots_entries << entry + end + end + + ['themes', 'javascripts', 'images', 'screenshots'].each do |type| + eval("#{type}_entries").each do |entry| + eval("design.#{type}").build(:file => get_temp_file(zip_file, dir, entry)) + end + end + } + design.zip_file = input_file + design.save + } + temp_file.close + temp_file.unlink + end + + def get_temp_file(zip_file, dir, entry) + filename = File.basename(entry.to_s) + temp_file = File.new(dir + '/' + filename, 'w+') + temp_file.write (zip_file.read entry ).force_encoding('UTF-8') + temp_file + end + end \ No newline at end of file diff --git a/app/controllers/admin/template_store_controller.rb b/app/controllers/admin/template_store_controller.rb index cdda6ff9..16e8fc05 100644 --- a/app/controllers/admin/template_store_controller.rb +++ b/app/controllers/admin/template_store_controller.rb @@ -3,9 +3,29 @@ class Admin::TemplateStoreController < ApplicationController layout "new_admin" def index + @templates = JSON.parse(get_templates) end def show + @template = JSON.parse(get_template(params[:id])) + end + + protected + + def get_template(id) + uri = URI.parse("http://#{APP_CONFIG['store_ip']}/store/design/#{id}") + http = Net::HTTP.new(uri.host, uri.port) + request = Net::HTTP::Get.new(uri.request_uri) + response = http.request(request) + response.body + end + + def get_templates + uri = URI.parse("http://#{APP_CONFIG['store_ip']}/store/designs") + http = Net::HTTP.new(uri.host, uri.port) + request = Net::HTTP::Get.new(uri.request_uri) + response = http.request(request) + response.body end end \ No newline at end of file diff --git a/app/models/design/design.rb b/app/models/design/design.rb index aa43368a..d6de4a75 100644 --- a/app/models/design/design.rb +++ b/app/models/design/design.rb @@ -8,62 +8,26 @@ class Design field :title, :type => String field :version, :type => String + mount_uploader :zip_file, AssetUploader + has_one :css_default, as: :css, :autosave => true, :dependent => :destroy has_one :layout, :autosave => true, :dependent => :destroy has_one :css_reset, :autosave => true, :dependent => :destroy + has_one :thumb, :autosave => true, :dependent => :destroy has_many :images, as: :imgs, :autosave => true, :dependent => :destroy has_many :javascripts, as: :js, :autosave => true, :dependent => :destroy has_many :pages + has_many :screenshots, :autosave => true, :dependent => :destroy has_many :themes, :autosave => true, :dependent => :destroy accepts_nested_attributes_for :images, :allow_destroy => true accepts_nested_attributes_for :javascripts, :allow_destroy => true + accepts_nested_attributes_for :screenshots, :allow_destroy => true accepts_nested_attributes_for :themes, :allow_destroy => true validates_presence_of :author, :title after_save :parse_css_for_images - - - def new_files=(*attrs) - attrs[0].map do |key,items_ary| #Loop by JSs,Themes,Imgs - self.files=([items_ary, key]) - end - end - - # def javascripts=(*attrs) - # self.files = (attrs << 'javascripts') - # end - # - # def themes=(*attrs) - # self.files = (attrs << 'themes') - # end - # - # def images=(*attrs) - # self.files = (attrs << 'images') - # end - - # Update or create the attribute records - def files=(attrs) - case attrs.last - when 'layout' - files = self.layout.build - else - files = eval(attrs.last) - end - attrs[0].each do |a| - - if a[:id].blank? && !a[:file].blank? - files.build(:file => a[:file], :to_save => true) - else - files.each do |file| - if file.id.to_s == a[:id] - file.to_destroy = a[:to_destroy] - end - end - end - end - end protected diff --git a/app/models/design/screenshot.rb b/app/models/design/screenshot.rb new file mode 100644 index 00000000..f0116e14 --- /dev/null +++ b/app/models/design/screenshot.rb @@ -0,0 +1,3 @@ +class Screenshot < Image + belongs_to :design +end \ No newline at end of file diff --git a/app/models/design/thumb.rb b/app/models/design/thumb.rb new file mode 100644 index 00000000..5c226542 --- /dev/null +++ b/app/models/design/thumb.rb @@ -0,0 +1,3 @@ +class Thumb < Image + belongs_to :design +end \ No newline at end of file diff --git a/app/views/admin/designs/_design.html.erb b/app/views/admin/designs/_design.html.erb index bf75bd65..3f579844 100644 --- a/app/views/admin/designs/_design.html.erb +++ b/app/views/admin/designs/_design.html.erb @@ -1,6 +1,6 @@