orbit-basic/app/controllers/desktop_widgets_controller.rb

78 lines
2.4 KiB
Ruby
Raw Normal View History

2012-11-01 12:42:40 +00:00
class DesktopWidgetsController < OrbitBackendController
require "net/http"
require "uri"
require 'zip/zip'
def index
@desktopWidgets = DesktopWidget.all
2013-07-30 08:15:04 +00:00
render :layout => false
2012-11-01 12:42:40 +00:00
end
def upload
if !params[:desktop_widget].nil?
temp_file = Tempfile.new("temp_file")
original_file = params[:desktop_widget][: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
2012-11-01 12:42:40 +00:00
end
def edit
@dw = DesktopWidget.find(params[:id])
end
def update
@dw = DesktopWidget.find(params[:id])
if(params[:desktop_widget])
@imagefile = params[:desktop_widget][:image]
@dw.images.build(:file => @imagefile)
@dw.save
end
redirect_to action: "index"
end
2012-11-01 12:42:40 +00:00
def unzip_widget(file, zip_name)
Zip::ZipFile.open(file) { |zip_file|
dw = DesktopWidget.new.from_json(zip_file.read("#{zip_name}/settings.json"))
Dir.mktmpdir('f_path') { |dir|
javascripts_entries = []
images_entries = []
zip_file.entries.each do |entry|
case (path = entry.to_s)
2012-11-05 15:04:54 +00:00
when /\A(#{zip_name})\/(default\.css)\z/
#for default css
2012-11-01 12:42:40 +00:00
dw.build_css_default(:file => get_temp_file(zip_file, dir, entry))
when /\A(#{zip_name})\/(widget\.html)\z/ #for layout html
dw.build_widget_layout(:file => get_temp_file(zip_file, dir, 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
['javascripts', 'images'].each do |type|
eval("#{type}_entries").each do |entry|
eval("dw.#{type}").build(:file => get_temp_file(zip_file, dir, entry))
end
end
}
dw.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
end