Skip to content

Commit

Permalink
move processors and backends to separate modules
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.techno-weenie.net/projects/plugins/attachment_fu@2569 567b1171-46fb-0310-a4c9-b4bef9110e78
  • Loading branch information
technoweenie committed Dec 23, 2006
1 parent 79f2913 commit c3b5889
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 227 deletions.
9 changes: 9 additions & 0 deletions init.rb
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
require 'tempfile'

class Tempfile
# overwrite so tempfiles have no extension
def make_tmpname(basename, n)
sprintf("%s%d-%d", basename, $$, n)
end
end

ActiveRecord::Base.send(:extend, Technoweenie::AttachmentFu::ActMethods)
35 changes: 18 additions & 17 deletions lib/technoweenie/attachment_fu.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
require File.join(File.dirname(__FILE__), 'attachment_fu', 'backends')
require File.join(File.dirname(__FILE__), 'attachment_fu', 'processors')
require 'tempfile'

class Tempfile
# overwrite so tempfiles have no extension
def make_tmpname(basename, n)
sprintf("%s%d-%d", basename, $$, n)
end
end

module Technoweenie # :nodoc:
module AttachmentFu # :nodoc:
@@tempfile_path = File.join(RAILS_ROOT, 'tmp', 'attachment_fu')
@@content_types = ['image/jpeg', 'image/pjpeg', 'image/gif', 'image/png', 'image/x-png']
mattr_reader :content_types, :tempfile_path
@@default_processors = %w(Rmagick)
@@tempfile_path = File.join(RAILS_ROOT, 'tmp', 'attachment_fu')
@@content_types = ['image/jpeg', 'image/pjpeg', 'image/gif', 'image/png', 'image/x-png']
mattr_reader :content_types, :tempfile_path, :default_processors

class ThumbnailError < StandardError; end
class AttachmentError < StandardError; end
Expand Down Expand Up @@ -57,7 +47,6 @@ def has_attachment(options = {})
class_inheritable_accessor :attachment_options
attr_accessor :thumbnail_resize_options

options[:processor] ||= :rmagick
options[:storage] ||= options[:file_system_path] ? :file_system : :db_file
options[:file_system_path] ||= File.join("public", table_name)
options[:file_system_path] = options[:file_system_path][1..-1] if options[:file_system_path].first == '/'
Expand All @@ -72,8 +61,20 @@ def has_attachment(options = {})
after_destroy :destroy_file
extend ClassMethods
include InstanceMethods
include Technoweenie::AttachmentFu::const_get("#{options[:storage].to_s.classify}Backend")
include Technoweenie::AttachmentFu::const_get("#{options[:processor].to_s.classify}Processor")
include Technoweenie::AttachmentFu::Backends.const_get("#{options[:storage].to_s.classify}")
case options[:processor]
when :none
when nil
processors = Technoweenie::AttachmentFu.default_processors.dup
begin
include Technoweenie::AttachmentFu::Processors.const_get(processors.first) if processors.any?
rescue LoadError
processors.shift
retry
end
else
include Technoweenie::AttachmentFu::Processors.const_get("#{options[:processor].to_s.classify}")
end
before_save :process_attachment
end

Expand Down
110 changes: 0 additions & 110 deletions lib/technoweenie/attachment_fu/backends.rb

This file was deleted.

36 changes: 36 additions & 0 deletions lib/technoweenie/attachment_fu/backends/db_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Technoweenie # :nodoc:
module AttachmentFu # :nodoc:
module Backends
# Methods for DB backed attachments
module DbFile
def self.included(base) #:nodoc:
Object.const_set(:DbFile, Class.new(ActiveRecord::Base)) unless Object.const_defined?(:DbFile)
base.belongs_to :db_file, :class_name => '::DbFile', :foreign_key => 'db_file_id'
end

def create_temp_file
write_to_temp_file db_file.data
end

# Destroys the file. Called in the after_destroy callback
def destroy_file
db_file.destroy if db_file
end

# Saves the data to the DbFile model
def save_to_storage
if save_attachment?
(db_file || build_db_file).data = temp_data
db_file.save!
self.class.update_all ['db_file_id = ?', self.db_file_id = db_file.id], ['id = ?', id]
end
true
end

def current_data
db_file.data
end
end
end
end
end
81 changes: 81 additions & 0 deletions lib/technoweenie/attachment_fu/backends/file_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
module Technoweenie # :nodoc:
module AttachmentFu # :nodoc:
module Backends
# Methods for file system backed attachments
module FileSystem
def self.included(base) #:nodoc:
base.before_update :rename_file
end

# Gets the full path to the filename in this format:
#
# # This assumes a model name like MyModel
# # public/#{table_name} is the default filesystem path
# RAILS_ROOT/public/my_models/5/blah.jpg
#
# Overwrite this method in your model to customize the filename.
# The optional thumbnail argument will output the thumbnail's filename.
def full_filename(thumbnail = nil)
file_system_path = (thumbnail ? thumbnail_class : self).attachment_options[:file_system_path].to_s
File.join(RAILS_ROOT, file_system_path, attachment_path_id, thumbnail_name_for(thumbnail))
end

# Used as the base path that #public_filename strips off full_filename to create the public path
def base_path
@base_path ||= File.join(RAILS_ROOT, 'public')
end

# The attachment ID used in the full path of a file
def attachment_path_id
((respond_to?(:parent_id) && parent_id) || id).to_s
end

# Gets the public path to the file
# The optional thumbnail argument will output the thumbnail's filename.
def public_filename(thumbnail = nil)
full_filename(thumbnail).gsub %r(^#{Regexp.escape(base_path)}), ''
end

def filename=(value)
@old_filename = full_filename unless filename.nil? || @old_filename
write_attribute :filename, sanitize_filename(value)
end

def create_temp_file
copy_to_temp_file full_filename
end

# Destroys the file. Called in the after_destroy callback
def destroy_file
FileUtils.rm full_filename rescue nil
end

def rename_file
return unless @old_filename && @old_filename != full_filename
if save_attachment? && File.exists?(@old_filename)
FileUtils.rm @old_filename
elsif File.exists?(@old_filename)
FileUtils.mv @old_filename, full_filename
end
@old_filename = nil
true
end

# Saves the file to the file system
def save_to_storage
if save_attachment?
# TODO: This overwrites the file if it exists, maybe have an allow_overwrite option?
FileUtils.mkdir_p(File.dirname(full_filename))
FileUtils.mv temp_path, full_filename
end
@old_filename = nil
true
end

def current_data
File.file?(full_filename) ? File.read(full_filename) : nil
end
end
end
end
end
Loading

0 comments on commit c3b5889

Please sign in to comment.