lib/milton/attachment.rb in citrusbyte-milton-0.1.2 vs lib/milton/attachment.rb in citrusbyte-milton-0.1.5
- old
+ new
@@ -33,10 +33,11 @@
include Citrusbyte::Milton::Attachment::InstanceMethods
end
end
+ # These get mixed in to your model when you use Milton
module InstanceMethods
# Sets the filename to the given filename (sanitizes the given filename
# as well)
#
# TODO: change the filename on the underlying file system on save so as
@@ -106,10 +107,13 @@
padded = ("0"*(12-id.to_s.size)+id.to_s).split('')
File.join(*[0, 3, 6, 9].collect{ |i| padded.slice(i, 3).join })
end
end
+ attr_accessor :filename
+ attr_accessor :attachment
+
# TODO: can probably fanagle a way to only pass a reference to the model
# and not need the filename (or better yet just the filename and
# decouple)
def initialize(attachment, filename)
@attachment = attachment
@@ -118,11 +122,11 @@
# Returns the full path and filename to the file with the given options.
# If no options are given then returns the path and filename to the
# original file.
def path(options={})
- options.empty? ? File.join(dirname, @filename) : Derivative.new(@filename, options).path
+ options.empty? ? File.join(dirname, filename) : Derivative.new(filename, options).path
end
# Returns the full directory path up to the file, w/o the filename.
def dirname
File.join(root_path, partitioned_path)
@@ -134,11 +138,10 @@
end
# Removes the file from the underlying file system and any derivatives of
# the file.
def destroy
- destroy_derivatives
destroy_file
end
protected
# Returns the file as a File object opened for reading.
@@ -147,11 +150,11 @@
end
# Returns the partitioned path segment based on the id of the model
# this file is attached to.
def partitioned_path
- self.class.partition(@attachment.id)
+ self.class.partition(self.attachment.id)
end
# The full path to the root of where files will be stored on disk.
def root_path
self.class.options[:file_system_path]
@@ -160,34 +163,22 @@
# Recreates the directory this file will be stored in.
def recreate_directory
self.class.recreate_directory(dirname) unless File.exists?(dirname)
end
- # Removes the file from the filesystem.
+ # Removes the containing directory from the filesystem (and hence the
+ # file and any derivatives)
def destroy_file
- FileUtils.rm path if File.exists?(path)
+ FileUtils.rm_rf dirname if File.exists?(dirname)
end
-
- # Derivatives of this Attachment ====================================
- # Returns an array of derivatives of this attachment
+ # Returns an array of Derivatives of this AttachableFile.
def derivatives
- Dir.glob(Derivative.dirname_for(path)).collect do |filename|
+ Dir.glob(dirname + '/*').reject{ |filename| filename == self.filename }.collect do |filename|
Derivative.from_filename(filename)
end
end
-
- # Recreates the directory derivatives of this file will be stored in.
- def recreate_derivative_directory
- dirname = Derivative.dirname_for(path)
- self.class.recreate_directory(dirname) unless File.exists?(dirname)
- end
-
- # Removes the derivatives folder for this file and all files within.
- def destroy_derivatives
- FileUtils.rm_rf dirname if File.exists?(dirname)
- end
end
# Represents a file created on the file system that is a derivative of the
# one referenced by the model, i.e. a thumbnail of an image, or a transcode
# of a video.
@@ -196,11 +187,11 @@
# passing options for the creation of derivatives.
#
# Files created as derivatives have their creation options appended into
# their filenames so it can be checked later if a file w/ the given
# options already exists (so as not to create it again).
- #
+ #
class Derivative
attr_reader :options
class << self
# Given a string of attachment options, splits them out into a hash,
@@ -229,32 +220,27 @@
# Creates a new Derivative from the given filename by extracting the
# options.
def from_filename(filename)
Derivative.new(filename, options_from(extract_options_from(filename)))
end
-
- # Gives the path to where derivatives of this file are stored.
- # Derivatives are any files which are based off of this file but are
- # not Attachments themselves (i.e. thumbnails, transcoded copies,
- # etc...)
- def dirname_for(path)
- File.join(File.dirname(path), File.basename(path, File.extname(path)))
- end
end
+ # Instantiate a new Derivative, takes a reference to the AttachableFile
+ # (or specialization class) that this will be a derivative of, and a hash
+ # of the options defining the derivative.
def initialize(file, options)
@file = file
@options = options
end
- # The filename of this Derivative with embedded options.
+ # The resulting filename of this Derivative with embedded options.
def filename
self.class.filename_for(@file.path, options)
end
# The full path and filename to this Derivative.
def path
- File.join(Derivative.dirname_for(@file.path), filename)
+ File.join(@file.dirname, filename)
end
# Returns true if the file resulting from this Derivative exists.
def exists?
File.exists?(path)