diff --git a/Gemfile b/Gemfile index 4ba1051a..052637c8 100644 --- a/Gemfile +++ b/Gemfile @@ -8,7 +8,7 @@ gem 'carrierwave' gem 'ckeditor' gem 'devise' gem 'mini_magick' - +gem 'rubyzip' gem 'mongoid' diff --git a/Gemfile.lock b/Gemfile.lock index ab5e7389..79f31998 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -129,6 +129,7 @@ GEM ruby-debug-base19 (>= 0.11.19) ruby_core_source (0.1.5) archive-tar-minitar (>= 0.5.2) + rubyzip (0.9.4) shoulda-matchers (1.0.0.beta2) sprockets (2.0.0.beta.10) hike (~> 1.0) @@ -165,6 +166,7 @@ DEPENDENCIES rspec (~> 2.0) rspec-rails (~> 2.0) ruby-debug19 + rubyzip shoulda-matchers watchr yajl-ruby diff --git a/app/controllers/admin/designs_controller.rb b/app/controllers/admin/designs_controller.rb index 7aa93526..3231df46 100644 --- a/app/controllers/admin/designs_controller.rb +++ b/app/controllers/admin/designs_controller.rb @@ -1,8 +1,63 @@ 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) + filename = original_file.original_filename + 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}/#{zip_name}.json")) + + Dir.mktmpdir('f_path') { |dir| + title = design.layout_filename + temp_file = File.new(dir + '/' + title, 'w+') + temp_file.write zip_file.read(zip_name + '/' + title) + design.layout = temp_file + ['stylesheets', 'javascripts', 'images'].each do |type| + eval("design.#{type}").each do |object| + title = object.file_filename + temp_file = File.new(dir + '/' + title, 'w+') + temp_file.write zip_file.read(zip_name + '/' + type + '/' + title) + object.file = temp_file + object.to_save = true + end + end + } + + design.save + } + end + def proc_package + #uri = URI.parse("http://localhost:3001/design/fraisier") + #http = Net::HTTP.new(uri.host, uri.port) + #request = Net::HTTP::Get.new(uri.request_uri) + #response = http.request(request) + temp_file = Tempfile.new("temp_file") + temp_file.binmode + temp_file.write response.body + zip_name = response['content-disposition'].split('filename=')[1].gsub(/[\\\"]|.zip/, '') rescue '' + unzip_design(temp_file, zip_name) + temp_file.close + end + def index @designs = Design.all.entries diff --git a/app/models/design.rb b/app/models/design.rb index 1851b07d..397cf154 100644 --- a/app/models/design.rb +++ b/app/models/design.rb @@ -12,7 +12,7 @@ class Design validates_presence_of :author embeds_one :layout - mount_uploader :structure_css, DesignFileUploader + mount_uploader :structure_css, AssetUploader embeds_many :themes embeds_many :javascripts diff --git a/app/models/design_file.rb b/app/models/design_file.rb index 07c09e14..fe419eea 100644 --- a/app/models/design_file.rb +++ b/app/models/design_file.rb @@ -5,7 +5,5 @@ class DesignFile field :to_save, :type => Boolean field :to_destroy, :type => Boolean - - end diff --git a/app/uploaders/design_file_uploader.rb.to_be_delete b/app/uploaders/design_file_uploader.rb.to_be_delete new file mode 100644 index 00000000..091471fd --- /dev/null +++ b/app/uploaders/design_file_uploader.rb.to_be_delete @@ -0,0 +1,47 @@ +# encoding: utf-8 + +class DesignFileUploader < CarrierWave::Uploader::Base + + # Include RMagick or ImageScience support: + # include CarrierWave::RMagick + # include CarrierWave::ImageScience + + # Choose what kind of storage to use for this uploader: + # storage :file + # storage :s3 + + # Override the directory where uploaded files will be stored. + # This is a sensible default for uploaders that are meant to be mounted: + def store_dir + "assets/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" + end + + # Provide a default URL as a default if there hasn't been a file uploaded: + # def default_url + # "/images/fallback/" + [version_name, "default.png"].compact.join('_') + # end + + # Process files as they are uploaded: + # process :scale => [200, 300] + # + # def scale(width, height) + # # do something + # end + + # Create different versions of your uploaded files: + # version :thumb do + # process :scale => [50, 50] + # end + + # Add a white list of extensions which are allowed to be uploaded. + # For images you might use something like this: + # def extension_white_list + # %w(jpg jpeg gif png) + # end + + # Override the filename of the uploaded files: + # def filename + # "something.jpg" if original_filename + # end + +end diff --git a/app/views/admin/designs/._form.html.erb.swp b/app/views/admin/designs/._form.html.erb.swp index 2dbb06f6..5e28e536 100644 Binary files a/app/views/admin/designs/._form.html.erb.swp and b/app/views/admin/designs/._form.html.erb.swp differ diff --git a/app/views/admin/designs/_stylesheet_field.html.erb b/app/views/admin/designs/_stylesheet_field.html.erb index 6527b16b..e69de29b 100644 --- a/app/views/admin/designs/_stylesheet_field.html.erb +++ b/app/views/admin/designs/_stylesheet_field.html.erb @@ -1,3 +0,0 @@ -
+<%= file_field :design,:package_file %> +
+<%= submit_tag %> + +<% end %> diff --git a/config/routes.rb b/config/routes.rb index 53123d7f..e99fbd1e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -45,9 +45,10 @@ PrototypeR4::Application.routes.draw do end resources :layouts resources :designs do + get 'upload_package' ,:on => :collection + post 'upload_package' ,:on => :collection member do post 'edit_file' => 'designs#edit_file' - get 'apply' get 'delete' end end