lib/cloud_crowd/action.rb in documentcloud-cloud-crowd-0.0.6 vs lib/cloud_crowd/action.rb in documentcloud-cloud-crowd-0.1.0
- old
+ new
@@ -26,11 +26,10 @@
def initialize(status, input, options, store)
@input, @options, @store = input, options, store
@job_id, @work_unit_id = options['job_id'], options['work_unit_id']
@work_directory = File.expand_path(File.join(@store.temp_storage_path, storage_prefix))
FileUtils.mkdir_p(@work_directory) unless File.exists?(@work_directory)
- Dir.chdir @work_directory
status == MERGING ? parse_input : download_input
end
# Each Action subclass must implement a +process+ method, overriding this.
def process
@@ -51,27 +50,25 @@
# Takes a local filesystem path, saves the file to S3, and returns the
# public (or authenticated) url on S3 where the file can be accessed.
def save(file_path)
save_path = File.join(storage_prefix, File.basename(file_path))
@store.save(file_path, save_path)
- return @store.url(save_path)
end
# After the Action has finished, we remove the work directory and return
# to the root directory (where daemons run by default).
def cleanup_work_directory
- Dir.chdir '/'
FileUtils.rm_r(@work_directory) if File.exists?(@work_directory)
end
private
# Convert an unsafe URL into a filesystem-friendly filename.
def safe_filename(url)
- ext = File.extname(url)
- name = File.basename(url).gsub(/%\d+/, '-').gsub(/[^a-zA-Z0-9_\-.]/, '')
+ ext = File.extname(url)
+ name = URI.unescape(File.basename(url)).gsub(/[^a-zA-Z0-9_\-.]/, '-').gsub(/-+/, '-')
File.basename(name, ext).gsub('.', '-') + ext
end
# The directory prefix to use for both local and S3 storage.
# [action_name]/job_[job_id]/unit_[work_unit_it]
@@ -88,14 +85,16 @@
@input = JSON.parse(@input)
end
# If the input is a URL, download the file before beginning processing.
def download_input
- input_is_url = !!URI.parse(@input) rescue false
- return unless input_is_url
- @input_path = File.join(@work_directory, safe_filename(@input))
- @file_name = File.basename(@input_path, File.extname(@input_path))
- download(@input, @input_path)
+ Dir.chdir(@work_directory) do
+ input_is_url = !!URI.parse(@input) rescue false
+ return unless input_is_url
+ @input_path = File.join(@work_directory, safe_filename(@input))
+ @file_name = File.basename(@input_path, File.extname(@input_path))
+ download(@input, @input_path)
+ end
end
end
end
\ No newline at end of file