completed installing of theme from the backend .. now checks the availability of the theme also

This commit is contained in:
Harry Bomrah 2014-01-24 02:31:21 +05:30 committed by Saurabh Bhatia
parent d19223c873
commit ab94c79fdb
5 changed files with 102 additions and 19 deletions

View File

@ -1,22 +1,95 @@
require 'net/http'
require 'uri'
require 'fileutils'
require 'zip/zip'
class Admin::TemplateStoreController < OrbitBackendController
@@store_domain = STORE_CONFIG[:store_settings]["url"]
def index
@store = @@store_domain
@design_ids = Design.all.map{|d| d.template_store_id}
@templates = JSON.parse(get_templates)
end
def show
@store = @@store_domain
@design_ids = Design.all.map{|d| d.template_store_id}
@template = JSON.parse(get_template(params[:id])) rescue nil
end
def download_theme
render :json => {"success"=>true}.to_json
url = @@store_domain + params["url"]
url_base = url.split('/')[2]
url_path = '/'+url.split('/')[3..-1].join('/')
Net::HTTP.start(url_base) do |http|
open("public/#{params['slug']}.zip", "wb") do |file|
http.get(url_path) do |str|
file.write(str)
end
end
end
upload_package("#{params['slug']}.zip", params["id"])
File.delete("public/#{params['slug']}.zip")
render :json => {"success"=>true,"url"=>@@store_domain + params["url"]}.to_json
end
protected
def upload_package(package_name,template_store_id)
temp_file = Tempfile.new("temp_file")
original_file = File.open("#{Rails.root}/public/#{package_name}")
temp_file.write(original_file.read.force_encoding('UTF-8'))
temp_file.rewind
filename = File.basename(original_file,".zip")
unzip_design(temp_file, filename, template_store_id)
temp_file.close
end
def unzip_design(file, zip_name, template_store_id)
Zip::ZipFile.open(file) { |zip_file|
design = Design.new.from_json(zip_file.read("#{zip_name}/info.json"))
design.template_store_id = template_store_id
Dir.mktmpdir('f_path') { |dir|
themes_entries = []
javascripts_entries = []
images_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})\/(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
end
end
['themes', 'javascripts', 'images'].each do |type|
eval("#{type}_entries").each do |entry|
eval("design.#{type}").build(:file => get_temp_file(zip_file, dir, entry))
end
end
}
design.save
}
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
def get_template(id)
uri = URI.parse("#{@@store_domain}/api/templates/#{id}")
http = Net::HTTP.new(uri.host, uri.port)

View File

@ -7,6 +7,7 @@ class Design
field :intro, :type => String
field :title, :type => String
field :version, :type => String
field :template_store_id, :type => String
mount_uploader :zip_file, AssetUploader

View File

@ -60,7 +60,11 @@
<span class="item-price">Free</span>
</span>
</a>
<%= link_to "Download", "javascript:void(0);", "data-url" => template['template']['template']['url'], :class=> 'btn btn-primary download-link' %>
<% if @design_ids.include?(template["_id"]["$oid"]) %>
<%= link_to "Installed", "javascript:void(0);", "data-url" => template['template']['template']['url'], :class=> 'btn btn-success download-link', "disabled"=>"disabled", "data-name"=>template['title'], "data-slug"=>template["_slugs"][0], "data-id"=>template["_id"]["$oid"] %>
<% else %>
<%= link_to "Download", "javascript:void(0);", "data-url" => template['template']['template']['url'], :class=> 'btn btn-primary download-link', "data-name"=>template['title'], "data-slug"=>template["_slugs"][0], "data-id"=>template["_id"]["$oid"] %>
<% end %>
</li>
<% end %>
@ -71,20 +75,20 @@
<script type="text/javascript">
(function(){
$("a.download-link").on(clickEvent,function(){
var el = $(this),
downloadurl = $(this).data("url");
if($(this).attr("disabled") == "disabled")return false;
var el = $(this);
el.removeClass("btn-primary").addClass("btn-info").text("Installing").attr('disabled',"disabled");
$.ajax({
url : "<%= admin_template_store_download_theme_path %>",
data : {"url" : downloadurl},
data : el.data(),
dataType : "json",
type : "post",
success : function(data){
if(data.success){
type : "post"
}).done(function(data){
if(data.success){
el.removeClass('btn-info').addClass('btn-success').text("Installed");
}
}
}).fail(function(){
el.removeClass('btn-info').addClass('btn-danger').text("Error, please try again.").removeAttr('disabled');
})
})
})()

View File

@ -8,7 +8,11 @@
<div id="item-info">
<%= image_tag "#{@store}#{@template['preview']['preview']['thumb']['url']}", :class => "item-thumb" %>
<h2 class="item-name"><%= @template['title'] %></h2>
<%= link_to "Download", "javascript:void(0);", "data-url" => @template['template']['template']['url'], :class=> 'btn btn-primary download-link' %>
<% if @design_ids.include?(@template["_id"]["$oid"]) %>
<%= link_to "Installed", "javascript:void(0);", "data-url" => @template['template']['template']['url'], :class=> 'btn btn-success download-link', "disabled"=>"disabled", "data-name"=>@template['title'], "data-slug"=>@template["_slugs"][0], "data-id"=>@template["_id"]["$oid"] %>
<% else %>
<%= link_to "Download", "javascript:void(0);", "data-url" => @template['template']['template']['url'], :class=> 'btn btn-primary download-link', "data-name"=>@template['title'], "data-slug"=>@template["_slugs"][0], "data-id"=>@template["_id"]["$oid"] %>
<% end %>
<ul class="item-extra-info">
<li><span class="title">views</span><span class="count"><%= Random.new.rand(10..100) %></span></li>
<!-- <li><span class="title">color</span><span class="color-tag green"></span></li> -->
@ -64,21 +68,22 @@
<script type="text/javascript">
(function(){
$("a.download-link").on(clickEvent,function(){
var el = $(this),
downloadurl = $(this).data("url");
if($(this).attr("disabled") == "disabled")return false;
var el = $(this);
el.removeClass("btn-primary").addClass("btn-info").text("Installing").attr('disabled',"disabled");
$.ajax({
url : "<%= admin_template_store_download_theme_path %>",
data : {"url" : downloadurl},
data : el.data(),
dataType : "json",
type : "post",
success : function(data){
if(data.success){
type : "post"
}).done(function(data){
if(data.success){
el.removeClass('btn-info').addClass('btn-success').text("Installed");
}
}
}).fail(function(){
el.removeClass('btn-info').addClass('btn-danger').text("Error, please try again.").removeAttr('disabled');
})
})
})()
</script>

Binary file not shown.