2010-09-22 22:59:30 +00:00
|
|
|
require 'zip/zipfilesystem'
|
|
|
|
|
|
|
|
module Locomotive
|
|
|
|
module Import
|
|
|
|
class Job
|
|
|
|
|
2010-10-28 23:36:45 +00:00
|
|
|
include Logger
|
|
|
|
|
|
|
|
def initialize(zipfile, site, options = {})
|
2010-09-22 22:59:30 +00:00
|
|
|
@site = site
|
2010-10-28 23:36:45 +00:00
|
|
|
@options = {
|
|
|
|
:reset => false,
|
|
|
|
:samples => false,
|
|
|
|
:enabled => {}
|
|
|
|
}.merge(options)
|
2010-10-27 00:11:44 +00:00
|
|
|
|
|
|
|
@identifier = self.store_zipfile(zipfile)
|
|
|
|
|
|
|
|
raise "Theme identifier not found" if @identifier.blank?
|
2010-12-27 11:59:54 +00:00
|
|
|
|
|
|
|
@uploader = nil # fix issue with Ruby 1.9.2 and serialization
|
2010-09-28 22:08:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def before(worker)
|
|
|
|
@worker = worker
|
2010-09-22 22:59:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform
|
2010-10-28 23:36:45 +00:00
|
|
|
self.log "theme identifier #{@identifier}"
|
2010-09-28 22:08:11 +00:00
|
|
|
|
2010-09-22 22:59:30 +00:00
|
|
|
self.unzip!
|
|
|
|
|
2011-06-17 21:32:54 +00:00
|
|
|
raise "No config/compiled_site.yml found in the theme zipfile" if @database.nil?
|
2010-09-22 22:59:30 +00:00
|
|
|
|
2010-09-23 23:00:13 +00:00
|
|
|
context = {
|
|
|
|
:database => @database,
|
|
|
|
:site => @site,
|
2010-09-28 08:09:49 +00:00
|
|
|
:theme_path => @theme_path,
|
2010-09-28 22:08:11 +00:00
|
|
|
:error => nil,
|
|
|
|
:worker => @worker
|
2010-09-23 23:00:13 +00:00
|
|
|
}
|
|
|
|
|
2010-10-28 23:36:45 +00:00
|
|
|
self.reset! if @options[:reset]
|
|
|
|
|
2011-11-26 05:22:48 +00:00
|
|
|
%w(site content_types content_assets snippets pages).each do |step|
|
2010-10-28 23:36:45 +00:00
|
|
|
if @options[:enabled][step] != false
|
|
|
|
"Locomotive::Import::#{step.camelize}".constantize.process(context, @options)
|
2010-10-12 00:12:18 +00:00
|
|
|
@worker.update_attributes :step => step if @worker
|
2010-09-28 22:08:11 +00:00
|
|
|
else
|
2010-10-19 10:20:09 +00:00
|
|
|
self.log "skipping #{step}"
|
2010-09-28 08:09:49 +00:00
|
|
|
end
|
|
|
|
end
|
2010-09-22 22:59:30 +00:00
|
|
|
end
|
|
|
|
|
2010-10-19 10:20:09 +00:00
|
|
|
def success(worker)
|
|
|
|
self.log 'deleting original zip file'
|
|
|
|
|
2010-12-16 23:42:38 +00:00
|
|
|
uploader = self.get_uploader(@site)
|
2010-10-19 10:20:09 +00:00
|
|
|
|
|
|
|
uploader.retrieve_from_store!(@identifier)
|
|
|
|
|
|
|
|
uploader.remove!
|
|
|
|
|
|
|
|
self.log 'deleting working folder'
|
|
|
|
|
|
|
|
FileUtils.rm_rf(themes_folder) rescue nil
|
|
|
|
end
|
|
|
|
|
2010-12-16 23:42:38 +00:00
|
|
|
def self.run!(zipfile, site, options = {})
|
|
|
|
job = self.new(zipfile, site, options)
|
|
|
|
|
|
|
|
if Locomotive.config.delayed_job
|
2011-05-27 15:19:20 +00:00
|
|
|
puts "delayed::JOB !"
|
2010-12-16 23:42:38 +00:00
|
|
|
Delayed::Job.enqueue job, { :site => site, :job_type => 'import' }
|
|
|
|
else
|
|
|
|
job.perform
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-09-22 22:59:30 +00:00
|
|
|
protected
|
|
|
|
|
2010-10-19 10:20:09 +00:00
|
|
|
def themes_folder
|
|
|
|
File.join(Rails.root, 'tmp', 'themes', @site.id.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def prepare_folder
|
|
|
|
FileUtils.rm_rf self.themes_folder if File.exists?(self.themes_folder)
|
|
|
|
|
|
|
|
FileUtils.mkdir_p(self.themes_folder)
|
|
|
|
end
|
|
|
|
|
2010-10-27 00:11:44 +00:00
|
|
|
def store_zipfile(zipfile)
|
|
|
|
return nil if zipfile.blank?
|
|
|
|
|
2010-12-16 23:42:38 +00:00
|
|
|
uploader = self.get_uploader(@site)
|
2010-10-27 00:11:44 +00:00
|
|
|
|
|
|
|
begin
|
2011-07-04 13:25:02 +00:00
|
|
|
if zipfile.is_a?(String) && zipfile =~ /^https?:\/\//
|
2011-02-04 14:43:43 +00:00
|
|
|
uploader.download!(zipfile)
|
|
|
|
uploader.store!
|
|
|
|
else
|
2011-06-08 14:33:33 +00:00
|
|
|
file = ::CarrierWave::SanitizedFile.new(zipfile)
|
2011-02-04 14:43:43 +00:00
|
|
|
uploader.store!(file)
|
|
|
|
end
|
2010-12-27 11:59:54 +00:00
|
|
|
uploader.identifier
|
2011-06-08 14:33:33 +00:00
|
|
|
rescue ::CarrierWave::IntegrityError
|
2010-12-27 11:59:54 +00:00
|
|
|
nil
|
2010-10-27 00:11:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def retrieve_zipfile
|
2010-12-16 23:42:38 +00:00
|
|
|
uploader = self.get_uploader(@site)
|
2010-10-19 10:20:09 +00:00
|
|
|
|
|
|
|
uploader.retrieve_from_store!(@identifier)
|
|
|
|
|
|
|
|
if uploader.file.respond_to?(:url)
|
|
|
|
self.log 'file from remote storage'
|
|
|
|
|
|
|
|
@theme_file = File.join(self.themes_folder, @identifier)
|
|
|
|
|
2011-01-27 13:07:10 +00:00
|
|
|
if RUBY_VERSION =~ /1\.9/
|
|
|
|
bytes = uploader.file.read.force_encoding('UTF-8')
|
|
|
|
else
|
|
|
|
bytes = uploader.file.read
|
|
|
|
end
|
|
|
|
|
|
|
|
File.open(@theme_file, 'w') { |f| f.write(bytes) }
|
2010-10-19 10:20:09 +00:00
|
|
|
else # local filesystem
|
|
|
|
self.log 'file from local storage'
|
|
|
|
|
|
|
|
@theme_file = uploader.path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-09-22 22:59:30 +00:00
|
|
|
def unzip!
|
2010-10-19 10:20:09 +00:00
|
|
|
self.prepare_folder
|
2010-09-28 08:09:49 +00:00
|
|
|
|
2010-10-27 00:11:44 +00:00
|
|
|
self.retrieve_zipfile
|
2010-10-19 10:20:09 +00:00
|
|
|
|
|
|
|
self.log "unzip #{@theme_file}"
|
|
|
|
|
|
|
|
Zip::ZipFile.open(@theme_file) do |zipfile|
|
|
|
|
destination_path = self.themes_folder
|
2010-09-22 22:59:30 +00:00
|
|
|
|
|
|
|
zipfile.each do |entry|
|
|
|
|
next if entry.name =~ /__MACOSX/
|
|
|
|
|
2011-06-17 21:32:54 +00:00
|
|
|
if entry.name =~ /config\/compiled_site.yml$/
|
2010-09-22 22:59:30 +00:00
|
|
|
|
|
|
|
@database = YAML.load(zipfile.read(entry.name))
|
2011-06-17 21:32:54 +00:00
|
|
|
@theme_path = File.join(destination_path, entry.name.gsub('config/compiled_site.yml', ''))
|
2010-09-22 22:59:30 +00:00
|
|
|
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2010-09-23 23:00:13 +00:00
|
|
|
FileUtils.mkdir_p(File.dirname(File.join(destination_path, entry.name)))
|
|
|
|
|
|
|
|
zipfile.extract(entry, File.join(destination_path, entry.name))
|
2010-09-22 22:59:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-28 23:36:45 +00:00
|
|
|
def reset!
|
|
|
|
@site.pages.destroy_all
|
2011-11-26 05:22:48 +00:00
|
|
|
@site.content_assets.destroy_all
|
2010-10-28 23:36:45 +00:00
|
|
|
@site.theme_assets.destroy_all
|
|
|
|
@site.content_types.destroy_all
|
|
|
|
end
|
|
|
|
|
2010-12-16 23:42:38 +00:00
|
|
|
def get_uploader(site)
|
|
|
|
unless Locomotive.config.delayed_job
|
|
|
|
ThemeUploader.storage = :file
|
|
|
|
end
|
|
|
|
@uploader ||= ThemeUploader.new(site)
|
|
|
|
end
|
|
|
|
|
2010-09-22 22:59:30 +00:00
|
|
|
end
|
|
|
|
end
|
2011-11-26 05:22:48 +00:00
|
|
|
end
|