2013-10-07 09:43:13 +00:00
|
|
|
class DesktopAppsController < ApplicationController
|
2013-05-14 09:18:05 +00:00
|
|
|
require "net/http"
|
|
|
|
require "uri"
|
|
|
|
require 'zip/zip'
|
|
|
|
|
|
|
|
def index
|
|
|
|
@desktopapps = DesktopApp.all
|
2013-07-30 08:15:04 +00:00
|
|
|
render :layout => false
|
2013-05-14 09:18:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def upload
|
|
|
|
if !params[:desktop_app].nil?
|
|
|
|
temp_file = Tempfile.new("temp_file");
|
|
|
|
original_file = params[:desktop_app][:package_file]
|
|
|
|
temp_file.write(original_file.read.force_encoding("UTF-8"))
|
|
|
|
temp_file.rewind
|
|
|
|
filename = File.basename(original_file.original_filename,".zip")
|
|
|
|
unzip_app(temp_file,filename)
|
|
|
|
temp_file.close
|
|
|
|
end
|
2013-07-30 08:15:04 +00:00
|
|
|
render :layout => false
|
2013-05-14 09:18:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def unzip_app(file, zip_name)
|
|
|
|
Zip::ZipFile.open(file) { |zip_file|
|
|
|
|
da = DesktopApp.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})\/(images)\/.*((\.jpg)|(\.png)|(\.gif))\z/ #for img
|
|
|
|
images_entries << entry
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
images_entries.each do |image|
|
|
|
|
da.images.build(:file => get_temp_file(zip_file,dir,image))
|
|
|
|
end
|
|
|
|
}
|
|
|
|
da.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
|