orbit-basic/app/controllers/desktop_themes_controller.rb

89 lines
2.3 KiB
Ruby
Raw Permalink Normal View History

2013-08-22 06:14:19 +00:00
class DesktopThemesController < ApplicationController
require "net/http"
require "uri"
require 'zip/zip'
def index
@desktopThemes = DesktopTheme.all
2013-07-30 08:15:04 +00:00
render :layout => false
end
def destroy
dt = DesktopTheme.find(params[:id])
dt.destroy
redirect_to desktop_theme_path
end
def upload
if !params[:desktop_theme].nil?
temp_file = Tempfile.new("temp_file")
original_file = params[:desktop_theme][:package_file]
#if original_file.content_type == 'application/zip'
temp_file.write(original_file.read.force_encoding('UTF-8'))
temp_file.rewind
filename = File.basename(original_file.original_filename,".zip")
unzip_widget(temp_file, filename)
#else
# flash[:error] = "Upload file should be in zip format"
#end
temp_file.close
end
2013-07-30 08:15:04 +00:00
render :layout => false
end
def edit
@dw = DesktopWidget.find(params[:id])
end
def unzip_widget(file, zip_name)
Zip::ZipFile.open(file) { |zip_file|
dt = DesktopTheme.new.from_json(zip_file.read("#{zip_name}/settings.json"))
Dir.mktmpdir('f_path') { |dir|
images_entries = []
zip_file.entries.each do |entry|
case (path = entry.to_s)
when /\A(#{zip_name})\/(theme\.css)\z/
#for default css
dt.build_css_default(:file => get_temp_file(zip_file, dir, entry))
when /\A(#{zip_name})\/(background)\/.*((\.jpg)|(\.png)|(\.gif))\z/ #for img
images_entries << entry
end
end
images_entries.each do |image|
dt.images.build(:file => get_temp_file(zip_file,dir,image))
end
}
dt.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 settings
if params[:id] == "custom"
@id = DesktopTheme.first.id.to_s
else
@id = params[:id]
end
dt = DesktopTheme.find(@id)
if !dt.nil?
dt['css'] = dt.css_default.file.url
dt['background'] = dt.images.where(:name=>dt.background).first.file.url
end
icons = DesktopTheme::ICONS
dt['icons'] = icons
render :json => dt.to_json
end
end