class DesktopAppsController < ApplicationController require "net/http" require "uri" require 'zip/zip' def index @desktopapps = DesktopApp.all render :layout => false 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 render :layout => false 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