Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically Removed Whitespace #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.so
*.pyc

# Logs and databases #
######################
*.log

# OS generated files #
######################
.DS_Store*
ehthumbs.db
Icon?
Thumbs.db
2 changes: 1 addition & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@
0.1.1 (2005-08-11)
* fixed nasty bug in url_for_file_column that made it unusable on Apache
* prepared for public release

0.1 (2005-08-10)
* initial release
3 changes: 1 addition & 2 deletions init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
require 'file_compat'
require 'file_column_helper'
require 'validations'
require 'test_case'

ActiveRecord::Base.send(:include, FileColumn)
ActionView::Base.send(:include, FileColumnHelper)
ActiveRecord::Base.send(:include, FileColumn::Validations)
ActiveRecord::Base.send(:include, FileColumn::Validations)
117 changes: 64 additions & 53 deletions lib/file_column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def assign(file)
# this did not come in via a CGI request. However,
# assigning files directly may be useful, so we
# make just this file object similar enough to an uploaded
# file that we can handle it.
# file that we can handle it.
file.extend FileColumn::FileCompat
end

Expand Down Expand Up @@ -80,7 +80,7 @@ def on_save(&blk)
@on_save ||= []
@on_save << Proc.new
end

# the following methods are overriden by sub-classes if needed

def temp_path
Expand Down Expand Up @@ -108,22 +108,22 @@ def options
end

private

def store_dir
if options[:store_dir].is_a? Symbol
raise ArgumentError.new("'#{options[:store_dir]}' is not an instance method of class #{@instance.class.name}") unless @instance.respond_to?(options[:store_dir])

dir = File.join(options[:root_path], @instance.send(options[:store_dir]))
FileUtils.mkpath(dir) unless File.exists?(dir)
dir
else
else
options[:store_dir]
end
end

def tmp_base_dir
if options[:tmp_base_dir]
options[:tmp_base_dir]
options[:tmp_base_dir]
else
dir = File.join(store_dir, "tmp")
FileUtils.mkpath(dir) unless File.exists?(dir)
Expand All @@ -136,7 +136,7 @@ def clone_as(klass)
end

end


class NoUploadedFile < BaseUploadedFile # :nodoc:
def delete
Expand Down Expand Up @@ -188,7 +188,7 @@ def relative_path(subdir=nil)
private

# regular expressions to try for identifying extensions
EXT_REGEXPS = [
EXT_REGEXPS = [
/^(.+)\.([^.]+\.[^.]+)$/, # matches "something.tar.gz"
/^(.+)\.([^.]+)$/ # matches "something.jpg"
]
Expand All @@ -205,19 +205,19 @@ def split_extension(filename,fallback=nil)
end
[filename, ""]
end

end

class TempUploadedFile < RealUploadedFile # :nodoc:

def store_upload(file)
@tmp_dir = FileColumn.generate_temp_name
@dir = File.join(tmp_base_dir, @tmp_dir)
@dir = File.join(tmp_base_dir, @tmp_dir)
FileUtils.mkdir(@dir)

@filename = FileColumn::sanitize_filename(file.original_filename)
local_file_path = File.join(tmp_base_dir,@tmp_dir,@filename)

# stored uploaded file into local_file_path
# If it was a Tempfile object, the temporary file will be
# cleaned up automatically, so we do not have to care for this
Expand All @@ -229,7 +229,7 @@ def store_upload(file)
raise ArgumentError.new("Do not know how to handle #{file.inspect}")
end
File.chmod(options[:permissions], local_file_path)

if options[:fix_file_extensions]
# try to determine correct file extension and fix
# if necessary
Expand All @@ -242,7 +242,17 @@ def store_upload(file)
File.rename(local_file_path, new_local_file_path) unless new_local_file_path == local_file_path
local_file_path = new_local_file_path
end


# Added by SM
# See http://www.railsweenie.com/forums/1/topics/324
if options[:magick] && options[:magick][:format]
format = options[:magick][:format].downcase
@filename = @filename.gsub(/#{File.extname(@filename)}$/, ".#{format}")
new_local_file_path = File.join(tmp_base_dir,@tmp_dir,@filename)
File.rename(local_file_path, new_local_file_path) unless new_local_file_path == local_file_path
local_file_path = new_local_file_path
end

@instance[@attr] = @filename
@just_uploaded = true
end
Expand All @@ -259,20 +269,20 @@ def strip_extension(filename)
def correct_extension(filename, ext)
strip_extension(filename) << ".#{ext}"
end

def parse_temp_path(temp_path, instance_options=nil)
raise ArgumentError.new("invalid format of '#{temp_path}'") unless temp_path =~ %r{^((\d+\.)+\d+)/([^/].+)$}
@tmp_dir, @filename = $1, FileColumn.sanitize_filename($3)
@dir = File.join(tmp_base_dir, @tmp_dir)

@instance[@attr] = @filename unless instance_options == :ignore_instance
end

def upload(file)
# store new file
temp = clone_as TempUploadedFile
temp.store_upload(file)

# delete old copy
delete_files

Expand Down Expand Up @@ -323,7 +333,8 @@ def delete_files
def get_content_type(fallback=nil)
if options[:file_exec]
begin
content_type = `#{options[:file_exec]} -bi "#{File.join(@dir,@filename)}"`.chomp
content_type = `#{options[:file_exec]} -bI "#{File.join(@dir,@filename)}"`.chomp
# content_type = `#{options[:file_exec]} -bi "#{File.join(@dir,@filename)}"`.chomp
content_type = fallback unless $?.success?
content_type.gsub!(/;.+$/,"") if content_type
content_type
Expand All @@ -342,14 +353,14 @@ def relative_path_prefix
end
end


class PermanentUploadedFile < RealUploadedFile # :nodoc:
def initialize(*args)
super *args
@dir = File.join(store_dir, relative_path_prefix)
@filename = @instance[@attr]
@filename = nil if @filename.empty?
FileUtils.mkpath(File.dirname(@dir)) unless File.exists?(File.dirname(@dir))
FileUtils.mkpath(File.dirname(@dir))
end

def move_from(local_dir, just_uploaded)
Expand Down Expand Up @@ -394,13 +405,13 @@ def delete_files
end

private

def relative_path_prefix
raise RuntimeError.new("Trying to access file_column, but primary key got lost.") if @instance.id.to_s.empty?
File.join(*("%08d" % @instance.id).scan(/..../))
@instance.id.to_s
end
end

# The FileColumn module allows you to easily handle file uploads. You can designate
# one or more columns of your model's table as "file columns" like this:
#
Expand Down Expand Up @@ -468,7 +479,7 @@ def relative_path_prefix
# the final location if the object is successfully created. If the form is never completed, though, you
# can easily remove all the images in this "tmp" directory once per day or so.
#
# So in the example above, the image "test.png" would first be stored in
# So in the example above, the image "test.png" would first be stored in
# "public/entry/image/tmp/<some_random_key>/test.png" and be moved to
# "public/entry/image/<primary_key>/test.png".
#
Expand Down Expand Up @@ -520,13 +531,13 @@ def relative_path_prefix
# files are saved below the so-called "root_path" directory, which defaults to
# "RAILS_ROOT/public". For every file_column, you can set a separte "store_dir"
# option. It defaults to "model_name/attribute_name".
#
#
# Files will always be stored in sub-directories of the store_dir path. The
# subdirectory is named after the instance's +id+ attribute for a saved model,
# or "tmp/<randomkey>" for unsaved models.
#
# You can specify a custom root_path by setting the <tt>:root_path</tt> option.
#
#
# You can specify a custom storage_dir by setting the <tt>:storage_dir</tt> option.
#
# For setting a static storage_dir that doesn't change with respect to a particular
Expand Down Expand Up @@ -574,11 +585,11 @@ module ClassMethods
"audio/x-ms-wma" => "wma",
"audio/x-ms-wax" => "wax",
"audio/x-wav" => "wav",
"image/x-xbitmap" => "xbm",
"image/x-xpixmap" => "xpm",
"image/x-xwindowdump" => "xwd",
"text/css" => "css",
"text/html" => "html",
"image/x-xbitmap" => "xbm",
"image/x-xpixmap" => "xpm",
"image/x-xwindowdump" => "xwd",
"text/css" => "css",
"text/html" => "html",
"text/javascript" => "js",
"text/plain" => "txt",
"text/xml" => "xml",
Expand All @@ -601,11 +612,11 @@ module ClassMethods
:fix_file_extensions => true,
:permissions => 0644,

# path to the unix "file" executbale for
# path to the unix "file" executable for
# guessing the content-type of files
:file_exec => "file"
:file_exec => "file"
}

# handle the +attr+ attribute as a "file-upload" column, generating additional methods as explained
# above. You should pass the attribute's name as a symbol, like this:
#
Expand All @@ -615,14 +626,14 @@ module ClassMethods
# in +DEFAULT_OPTIONS+.
def file_column(attr, options={})
options = DEFAULT_OPTIONS.merge(options) if options
my_options = FileColumn::init_options(options,

my_options = FileColumn::init_options(options,
ActiveSupport::Inflector.underscore(self.name).to_s,
attr.to_s)

state_attr = "@#{attr}_state".to_sym
state_method = "#{attr}_state".to_sym

define_method state_method do
result = instance_variable_get state_attr
if result.nil?
Expand All @@ -631,13 +642,13 @@ def file_column(attr, options={})
end
result
end

private state_method

define_method attr do |*args|
send(state_method).absolute_path *args
end

define_method "#{attr}_relative_path" do |*args|
send(state_method).relative_path *args
end
Expand All @@ -659,30 +670,30 @@ def file_column(attr, options={})
end
end
end

define_method "#{attr}_temp" do
send(state_method).temp_path
end

define_method "#{attr}_temp=" do |temp_path|
instance_variable_set state_attr, send(state_method).assign_temp(temp_path)
end

after_save_method = "#{attr}_after_save".to_sym

define_method after_save_method do
instance_variable_set state_attr, send(state_method).after_save
end

after_save after_save_method

after_destroy_method = "#{attr}_after_destroy".to_sym

define_method after_destroy_method do
send(state_method).after_destroy
end
after_destroy after_destroy_method

define_method "#{attr}_just_uploaded?" do
send(state_method).just_uploaded?
end
Expand All @@ -698,24 +709,24 @@ def file_column(attr, options={})

FileColumn::MagickExtension::file_column(self, attr, my_options) if options[:magick]
end

end

private

def self.generate_temp_name
now = Time.now
"#{now.to_i}.#{now.usec}.#{Process.pid}"
end

def self.sanitize_filename(filename)
filename = File.basename(filename.gsub("\\", "/")) # work-around for IE
filename.gsub!(/[^a-zA-Z0-9\.\-\+_]/,"_")
filename = "_#{filename}" if filename =~ /^\.+$/
filename = "unnamed" if filename.size == 0
filename
end

end


Loading