This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
orbit-4-1/app/controllers/admin/designs_controller.rb

144 lines
4.2 KiB
Ruby

class Admin::DesignsController < ApplicationController
require "net/http"
require "uri"
require 'zip/zip'
layout "admin"
before_filter :authenticate_user!
before_filter :is_admin?
def upload_package
if !params[:design].nil?
temp_file = Tempfile.new("temp_file")
temp_file.binmode
original_file = params[:design][:package_file]
#if original_file.content_type == 'application/zip'
temp_file.write(original_file.read)
temp_file.rewind
filename = File.basename(original_file.original_filename,".zip")
unzip_design(temp_file, filename)
#else
# flash[:error] = "Upload file should be in zip format"
#end
temp_file.close
end
end
def unzip_design(file, zip_name)
Zip::ZipFile.open(file) { |zip_file|
design = Design.new.from_json(zip_file.read("#{zip_name}/info.json"))
Dir.mktmpdir('f_path') { |dir|
#layout_entry =""
#struct_css_entry =""
design.build_layout
themes_entries = []
javascripts_entries = []
images_entries = []
debugger
temp_file4layout = File.new(dir + '/' + zip_name+'-1', 'w+')
temp_file4layout.binmode
#dir/zip_name
temp_file4structure = File.new(dir + '/' + zip_name+'-2', 'w+')
temp_file4structure.binmode
#dir/zip_name
zip_file.entries.each do |entry|
case (path = entry.to_s)
when /\A(#{zip_name})\/[^\/]*(\.css)\z/ #for structure css
# struct_css_entry = entry
temp_file4structure.write (zip_file.read entry )
design.structure_css = temp_file4structure
when /\A(#{zip_name})\/[^\/]*(\.html)\z/ #for layout html
# layout_entry = entry
temp_file4layout.write (zip_file.read entry )
design.layout.file = temp_file4structure
design.layout.to_save=true
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
debugger
['themes', 'javascripts', 'images'].each do |type|
#incompleted
eval("#{type}.entries").each do |entry|
title = File.basename entry
debugger
temp_file = File.new(dir + '/' + title+'-3', 'w+')
temp_file.write zip_file.read entry
object.file = temp_file
object.to_save = true
end
end
}
design.save
}
end
def index
@designs = Design.all.entries
end
def new
@design = Design.new
end
def update
params[:design][:existing_task_attributes] ||= {}
@design = Design.find(params[:id])
if @design.update_attributes(params[:design])
flash[:notice] = "Successfully updated design and tasks."
redirect_to admin_designs_path(@design)
else
render :action => 'edit'
end
end
def edit_file
@design = Design.find(params[:id])
filename = params[:filename]
files = @design.stylesheets + @design.javascripts + @design.images
file_to_removed = files.find{ |obj|
obj.file_filename == filename
}
type = file_to_removed._type
new_file = ""
Dir.mktmpdir('design_temp'){ |dir|
temp_file = File.new(dir+'/'+filename,'w+')
temp_file.write params[:context]
replace_target = eval("@design.#{type.downcase.pluralize}")
new_file = replace_target.build(:file => temp_file,:to_save=>true)
file_to_removed.destroy
@design.save
temp_file.close
}
render :json => new_file.file.url
end
def edit
@design = Design.find(params[:id])
end
def destroy
@design = Design.find(params[:id])
@design.destroy
redirect_to admin_designs_url
end
def create
@design = Design.new(params[:design])
if @design.save
flash[:notice] = "Successfully created design and tasks."
redirect_to admin_designs_path
else
render :action => 'new'
end
end
end