class Passifier::Storage
Disk storage for a pass
Attributes
Public Class Methods
@param [String] scratch directory The directory to use for file storage @param [Array<Object>] assets The file assets to store
# File lib/passifier/storage.rb, line 12 def initialize(scratch_directory, assets) @assets = [assets].flatten @scratch_directory = scratch_directory end
Public Instance Methods
Clean up temp files
# File lib/passifier/storage.rb, line 45 def cleanup remove_temp_files remove_directory end
Path to the stored version @param [String] filename The filename to return the path for @return [String] The full path to the file with the passed-in filename
# File lib/passifier/storage.rb, line 20 def path(filename) "#{@scratch_directory}/#{filename}" end
Remove a zip archive @param [String] zip_path The path of the archive to delete
# File lib/passifier/storage.rb, line 52 def remove_zip(zip_path) File.delete(zip_path) if File.exists?(zip_path) end
Store the files for a group of pass assets @param [Array<Object>] The pass asset objects to store files for
# File lib/passifier/storage.rb, line 26 def store ensure_directory_exists @assets.each { |asset| write_file(asset) } end
Create a zip archive given a filename for the archive and a set of pass assets @param [String] zip_path The desired output path @return [String] The full path of the resulting zip archive
# File lib/passifier/storage.rb, line 34 def zip(zip_path) remove_zip(zip_path) # ensure that older version is deleted if it exists Zip::ZipFile.open(zip_path, Zip::ZipFile::CREATE) do |zipfile| @assets.each do |asset| zipfile.add(asset.filename, path(asset.filename)) end end zip_path end
Protected Instance Methods
# File lib/passifier/storage.rb, line 79 def ensure_directory_exists(directory = nil) directory ||= @scratch_directory last_directory = "" tree = directory.split("/") tree.each do |directory| dir = "#{last_directory}#{directory}" Dir.mkdir(dir) unless File.exists?(dir) || dir == "" last_directory = "#{dir}/" end last_directory end
# File lib/passifier/storage.rb, line 66 def remove_directory(directory = nil) directory ||= @scratch_directory Dir.rmdir(directory) if File.exists?(directory) && File.directory?(directory) end
Remove assets (created by Passifier::Storage#store
)
# File lib/passifier/storage.rb, line 72 def remove_temp_files @assets.map do |asset| path = path(asset.filename) File.delete(path) if File.exists?(path) end end
Store the file for the given pass asset to disk @param [Object] asset Store the file for the given pass asset
# File lib/passifier/storage.rb, line 60 def write_file(asset) File.open(path(asset.filename), 'wb') do |file| file.write(asset.content) if asset.respond_to?(:content) end end