adding upload package for design. bugs in

This commit is contained in:
Kaito 2011-08-11 18:20:16 +08:00
parent 34d0b8fac1
commit e0811c14db
10 changed files with 117 additions and 8 deletions

View File

@ -8,7 +8,7 @@ gem 'carrierwave'
gem 'ckeditor' gem 'ckeditor'
gem 'devise' gem 'devise'
gem 'mini_magick' gem 'mini_magick'
gem 'rubyzip'
gem 'mongoid' gem 'mongoid'

View File

@ -129,6 +129,7 @@ GEM
ruby-debug-base19 (>= 0.11.19) ruby-debug-base19 (>= 0.11.19)
ruby_core_source (0.1.5) ruby_core_source (0.1.5)
archive-tar-minitar (>= 0.5.2) archive-tar-minitar (>= 0.5.2)
rubyzip (0.9.4)
shoulda-matchers (1.0.0.beta2) shoulda-matchers (1.0.0.beta2)
sprockets (2.0.0.beta.10) sprockets (2.0.0.beta.10)
hike (~> 1.0) hike (~> 1.0)
@ -165,6 +166,7 @@ DEPENDENCIES
rspec (~> 2.0) rspec (~> 2.0)
rspec-rails (~> 2.0) rspec-rails (~> 2.0)
ruby-debug19 ruby-debug19
rubyzip
shoulda-matchers shoulda-matchers
watchr watchr
yajl-ruby yajl-ruby

View File

@ -1,8 +1,63 @@
class Admin::DesignsController < ApplicationController class Admin::DesignsController < ApplicationController
require "net/http"
require "uri"
require 'zip/zip'
layout "admin" layout "admin"
before_filter :authenticate_user! before_filter :authenticate_user!
before_filter :is_admin? 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 def index
@designs = Design.all.entries @designs = Design.all.entries

View File

@ -12,7 +12,7 @@ class Design
validates_presence_of :author validates_presence_of :author
embeds_one :layout embeds_one :layout
mount_uploader :structure_css, DesignFileUploader mount_uploader :structure_css, AssetUploader
embeds_many :themes embeds_many :themes
embeds_many :javascripts embeds_many :javascripts

View File

@ -5,7 +5,5 @@ class DesignFile
field :to_save, :type => Boolean field :to_save, :type => Boolean
field :to_destroy, :type => Boolean field :to_destroy, :type => Boolean
end end

View File

@ -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

View File

@ -1,3 +0,0 @@
<div class="style_uploader">
<% fields_for_
%>

View File

@ -0,0 +1,9 @@
<%= form_tag '',:multipart => true,:action=>"post" do |f| %>
<p>
<%= file_field :design,:package_file %>
</p>
<%= submit_tag %>
<% end %>

View File

@ -45,9 +45,10 @@ PrototypeR4::Application.routes.draw do
end end
resources :layouts resources :layouts
resources :designs do resources :designs do
get 'upload_package' ,:on => :collection
post 'upload_package' ,:on => :collection
member do member do
post 'edit_file' => 'designs#edit_file' post 'edit_file' => 'designs#edit_file'
get 'apply'
get 'delete' get 'delete'
end end
end end