From e0811c14db4f0763be7198414d39d596efb8b45b Mon Sep 17 00:00:00 2001 From: Kaito Date: Thu, 11 Aug 2011 18:20:16 +0800 Subject: [PATCH] adding upload package for design. bugs in --- Gemfile | 2 +- Gemfile.lock | 2 + app/controllers/admin/designs_controller.rb | 55 ++++++++++++++++++ app/models/design.rb | 2 +- app/models/design_file.rb | 2 - .../design_file_uploader.rb.to_be_delete | 47 +++++++++++++++ app/views/admin/designs/._form.html.erb.swp | Bin 12288 -> 12288 bytes .../admin/designs/_stylesheet_field.html.erb | 3 - .../admin/designs/upload_package.html.erb | 9 +++ config/routes.rb | 3 +- 10 files changed, 117 insertions(+), 8 deletions(-) create mode 100644 app/uploaders/design_file_uploader.rb.to_be_delete create mode 100644 app/views/admin/designs/upload_package.html.erb 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 2dbb06f611c3edf518c6d138af89225ed017a4a7..5e28e536e8822416f776fcc14f68eb6a33f933db 100644 GIT binary patch literal 12288 zcmeI2zi-n(6vvNl0SX94mLpKPMUlgg0b!AVkU%id0Ra+1$hySm=IF&XzDr9*ix6Uf zg^7Qm17l~z$im0~3mXG0h`#{A_w4*2sI(bCLg-ogsI~9;`Q7K;d1&1_cH{CDe#$?= zaO`93+NJaJQ-`M5>v_f&g|ex;zRj_`yu3|BIsBkVSihQHt>;2%6P^r@*F~1qmy}#K zbrE${Qjerj3yG;6F5AvmNX^<~0R;u%i!t&_M>s02v?yWPl8i0Wv@a$N(8217v^< z{3ixP#MnlKv9+C;5dQz~{s!=FFJmvjI#>hq;4-)X4uUUx82b!9fsf!Ncm|$=C*U!- z2~LA)@O7H8S6~CIgS#LG0^9~iz_;CaE_ef;gQwszcnCi1V(dM52i}6CUfDDiUGC&6YT?1$8S(BZaY4BVok<59JiQFood85g_@v^3`T z7b>jd(sZP>#@j;gc(Huwx38CD5kNU-9EV#K7twmZC5&{P=Cl3A`siD) zZF{*4+s634T+eaWDR&Q-+Q{PfGDGaU<%2bZ<-&up8;j=7aIRv$dE%B9$>P~EtifYn zi9I+mnb<9607K3pCz%bP21bj!P3}&@K#yXZI4GOUyWBwDP!*X zb^k_MIr_7$^2$ilz>}MemG|smE-Jw;#Z1~-m}2*PZqzF)7_}wn#|^ttoNu3A^lT^1 zC&)ht96q4DFnYr99Wh?NRaU!B{91ppWXtB23oGbH4j)jQ|CAq%9=#Ppm9%v)lA#p~ XyxMASAj_P@@!7^fVNJgy?|p%-G(IUtb{Pd+;sz2HXc90S+#Mi{LQ$dySAk!5`pv@EiCJd;vZL+u)z) z3Hb$l4L$`KVBjiP0}qa29dI9f2|fp7kbpP9QSifagnR5Y1x|sVp2b?=HmHCC zIk*h^U;})0l#mZV1$IFQHo!w@^dm4@9gz4tH=F`a0jGdd;Hg)j8{~aLX}3jr8qt>i zj=ROJtsWivLm_!M6kJBMqrcPkoz9MAW!dX{NhOs)r{tyA?i6Y-=V%oP+eO1YGkI4} zqDPndO+4z^_wue;o9oxEdu{r#n-QM)&<;7K;-VZw@$N8^GId)^tZe~HU(z^3+HDQ4y&Hld1aj}~E>vb>Bl<)o-eY4{?i39S9~Zj+eQMtiL<~Tu#%e!c=nQc3)^Q> zqO6q)AA4D){Hl=W=(NyKE=xXnNNojd9yxY|DS9nR#c)gnR7&+MVq>JbAAOB0Ap8|XMMkXdRD18D^{3)YB*cK uHY{I32_^Z01&rX$Bc?O4oULH)>jkuJ9sN*>bQCJKJ3YfgoP)V%X8jLTVaG`T 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 @@ -
- <% fields_for_ -%> diff --git a/app/views/admin/designs/upload_package.html.erb b/app/views/admin/designs/upload_package.html.erb new file mode 100644 index 00000000..08ce91cf --- /dev/null +++ b/app/views/admin/designs/upload_package.html.erb @@ -0,0 +1,9 @@ + +<%= form_tag '',:multipart => true,:action=>"post" do |f| %> + +

+<%= 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