file_column_with_s3 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README +4 -2
- data/Rakefile +0 -2
- data/lib/file_column.rb +2 -10
- data/lib/file_column/validations.rb +9 -47
- data/lib/file_column_helper.rb +0 -66
- data/lib/rails_file_column.rb +1 -1
- data/test/abstract_unit.rb +14 -8
- data/test/connection.rb +1 -1
- data/test/file_column_helper_test.rb +9 -5
- data/test/file_column_test.rb +30 -33
- data/test/fixtures/entry.rb +5 -8
- metadata +36 -52
- data/lib/magick_file_column.rb +0 -265
- data/test/magick_test.rb +0 -383
- data/test/magick_view_only_test.rb +0 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a873a910fc1cbaafc4f2e5756510c1a41f37e3393fd1c1b256b1368a3dcf1060
|
4
|
+
data.tar.gz: ea40ce044ddeb43e49bf96d6d755048ef53d588c909db0aec6bc7dbc63e71847
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe3e04f3b4a29cc641c966ab28f437b29ce57f64e95c32809f8edc205c4e1da42e899926afbed84c9a80eaa3de1d63fef5ac8439be2b10eee0cfab3bf3234b3f
|
7
|
+
data.tar.gz: 055f3b4a607e2bbf0f1ce25fb216d5b3037b575e0d6420de7c333c6b68963f2e88d70b6224fc883533e0efc6aa436f0a9fcc6f591c6a812abfadd3dafbcd01cc
|
data/README
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
Master branch :image:https://app.snap-ci.com/ThoughtWorksStudios/file_column/branch/master/build_image["Build Status", link="https://app.snap-ci.com/ThoughtWorksStudios/file_column/branch/master"]
|
2
|
+
|
1
3
|
FEATURES
|
2
4
|
========
|
3
5
|
|
@@ -22,8 +24,8 @@ end
|
|
22
24
|
uploaded file. Note that you need an Entry object in the instance variable @entry for this
|
23
25
|
to work.
|
24
26
|
|
25
|
-
*
|
26
|
-
|
27
|
+
* Does not support RMagick based features anymore. Look for version 0.2.0 and below for the same. Images cannot be resized or
|
28
|
+
checked for resolution.
|
27
29
|
USAGE
|
28
30
|
=====
|
29
31
|
|
data/Rakefile
CHANGED
data/lib/file_column.rb
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
require 'fileutils'
|
2
2
|
require 'tempfile'
|
3
|
-
require 'magick_file_column'
|
4
3
|
require 'storage'
|
5
|
-
require 'file_column/validations'
|
4
|
+
require File.dirname(__FILE__) + '/file_column/validations'
|
6
5
|
|
7
6
|
module FileColumn # :nodoc:
|
8
7
|
def self.append_features(base)
|
@@ -337,7 +336,7 @@ module FileColumn # :nodoc:
|
|
337
336
|
def get_content_type(fallback=nil)
|
338
337
|
if options[:file_exec]
|
339
338
|
begin
|
340
|
-
content_type = `#{options[:file_exec]} -
|
339
|
+
content_type = `#{options[:file_exec]} -b --mime "#{File.join(@dir,@filename)}"`.chomp
|
341
340
|
content_type = fallback unless $?.success?
|
342
341
|
content_type.gsub!(/;.+$/,"") if content_type
|
343
342
|
content_type
|
@@ -499,12 +498,6 @@ module FileColumn # :nodoc:
|
|
499
498
|
# As a convenience you can access files stored in sub-directories via the +subdir+
|
500
499
|
# parameter if they have the same filename.
|
501
500
|
#
|
502
|
-
# Suppose your uploaded file is named "vancouver.jpg" and you want to create a
|
503
|
-
# thumb-nail and store it in the "thumb" directory. If you call
|
504
|
-
# <tt>image("thumb")</tt>, you
|
505
|
-
# will receive an absolute path for the file "thumb/vancouver.jpg" in the same
|
506
|
-
# directory "vancouver.jpg" is stored. Look at the documentation of FileColumn::Magick
|
507
|
-
# for more examples and how to create these thumb-nails automatically.
|
508
501
|
#
|
509
502
|
# == File Extensions
|
510
503
|
#
|
@@ -756,7 +749,6 @@ module FileColumn # :nodoc:
|
|
756
749
|
|
757
750
|
private after_save_method, after_destroy_method
|
758
751
|
|
759
|
-
FileColumn::MagickExtension::file_column(self, attr, my_options) if options[:magick]
|
760
752
|
end
|
761
753
|
|
762
754
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module FileColumn
|
2
2
|
module Validations #:nodoc:
|
3
|
-
|
3
|
+
|
4
4
|
def self.append_features(base)
|
5
5
|
super
|
6
6
|
base.extend(ClassMethods)
|
@@ -16,7 +16,7 @@ module FileColumn
|
|
16
16
|
# end
|
17
17
|
module ClassMethods
|
18
18
|
EXT_REGEXP = /\.([A-z0-9]+)$/
|
19
|
-
|
19
|
+
|
20
20
|
# This validates the file type of one or more file_columns. A list of file columns
|
21
21
|
# should be given followed by an options hash.
|
22
22
|
#
|
@@ -28,12 +28,12 @@ module FileColumn
|
|
28
28
|
# validates_file_format_of :field, :in => ["gif", "png", "jpg"]
|
29
29
|
# validates_file_format_of :field, :in => ["image/jpeg"]
|
30
30
|
def validates_file_format_of(*attrs)
|
31
|
-
|
31
|
+
|
32
32
|
options = attrs.pop if attrs.last.is_a?Hash
|
33
33
|
raise ArgumentError, "Please include the :in option." if !options || !options[:in]
|
34
34
|
options[:in] = [options[:in]] if options[:in].is_a?String
|
35
35
|
raise ArgumentError, "Invalid value for option :in" unless options[:in].is_a?Array
|
36
|
-
|
36
|
+
|
37
37
|
validates_each(attrs, options) do |record, attr, value|
|
38
38
|
unless value.blank?
|
39
39
|
mime_extensions = record.send("#{attr}_options")[:mime_extensions]
|
@@ -41,9 +41,9 @@ module FileColumn
|
|
41
41
|
record.errors.add attr, "is not a valid format." unless extensions.include?(value.scan(EXT_REGEXP).flatten.first)
|
42
42
|
end
|
43
43
|
end
|
44
|
-
|
44
|
+
|
45
45
|
end
|
46
|
-
|
46
|
+
|
47
47
|
# This validates the file size of one or more file_columns. A list of file columns
|
48
48
|
# should be given followed by an options hash.
|
49
49
|
#
|
@@ -54,12 +54,12 @@ module FileColumn
|
|
54
54
|
# Examples:
|
55
55
|
# validates_filesize_of :field, :in => 0..100.megabytes
|
56
56
|
# validates_filesize_of :field, :in => 15.kilobytes..1.megabyte
|
57
|
-
def validates_filesize_of(*attrs)
|
58
|
-
|
57
|
+
def validates_filesize_of(*attrs)
|
58
|
+
|
59
59
|
options = attrs.pop if attrs.last.is_a?Hash
|
60
60
|
raise ArgumentError, "Please include the :in option." if !options || !options[:in]
|
61
61
|
raise ArgumentError, "Invalid value for option :in" unless options[:in].is_a?Range
|
62
|
-
|
62
|
+
|
63
63
|
validates_each(attrs, options) do |record, attr, value|
|
64
64
|
unless value.blank?
|
65
65
|
size = File.size(value)
|
@@ -67,45 +67,7 @@ module FileColumn
|
|
67
67
|
record.errors.add attr, "is larger than the allowed size range." if size > options[:in].last
|
68
68
|
end
|
69
69
|
end
|
70
|
-
|
71
|
-
end
|
72
|
-
|
73
|
-
IMAGE_SIZE_REGEXP = /^(\d+)x(\d+)$/
|
74
|
-
|
75
|
-
# Validates the image size of one or more file_columns. A list of file columns
|
76
|
-
# should be given followed by an options hash. The validation will pass
|
77
|
-
# if both image dimensions (rows and columns) are at least as big as
|
78
|
-
# given in the <tt>:min</tt> option.
|
79
|
-
#
|
80
|
-
# Required options:
|
81
|
-
# * <tt>:min</tt> => minimum image dimension string, in the format NNxNN
|
82
|
-
# (columns x rows).
|
83
|
-
#
|
84
|
-
# Example:
|
85
|
-
# validates_image_size :field, :min => "1200x1800"
|
86
|
-
#
|
87
|
-
# This validation requires RMagick to be installed on your system
|
88
|
-
# to check the image's size.
|
89
|
-
def validates_image_size(*attrs)
|
90
|
-
options = attrs.pop if attrs.last.is_a?Hash
|
91
|
-
raise ArgumentError, "Please include a :min option." if !options || !options[:min]
|
92
|
-
minimums = options[:min].scan(IMAGE_SIZE_REGEXP).first.collect{|n| n.to_i} rescue []
|
93
|
-
raise ArgumentError, "Invalid value for option :min (should be 'XXxYY')" unless minimums.size == 2
|
94
70
|
|
95
|
-
require 'RMagick'
|
96
|
-
|
97
|
-
validates_each(attrs, options) do |record, attr, value|
|
98
|
-
unless value.blank?
|
99
|
-
begin
|
100
|
-
img = ::Magick::Image::read(value).first
|
101
|
-
record.errors.add('image', "is too small, must be at least #{minimums[0]}x#{minimums[1]}") if ( img.rows < minimums[1] || img.columns < minimums[0] )
|
102
|
-
rescue ::Magick::ImageMagickError
|
103
|
-
record.errors.add('image', "invalid image")
|
104
|
-
end
|
105
|
-
img = nil
|
106
|
-
GC.start
|
107
|
-
end
|
108
|
-
end
|
109
71
|
end
|
110
72
|
end
|
111
73
|
end
|
data/lib/file_column_helper.rb
CHANGED
@@ -77,72 +77,6 @@ module FileColumnHelper
|
|
77
77
|
|
78
78
|
end
|
79
79
|
|
80
|
-
# Same as +url_for_file_colum+ but allows you to access different versions
|
81
|
-
# of the image that have been processed by RMagick.
|
82
|
-
#
|
83
|
-
# If your +options+ parameter is non-nil this will
|
84
|
-
# access a different version of an image that will be produced by
|
85
|
-
# RMagick. You can use the following types for +options+:
|
86
|
-
#
|
87
|
-
# * a <tt>:symbol</tt> will select a version defined in the model
|
88
|
-
# via FileColumn::Magick's <tt>:versions</tt> feature.
|
89
|
-
# * a <tt>geometry_string</tt> will dynamically create an
|
90
|
-
# image resized as specified by <tt>geometry_string</tt>. The image will
|
91
|
-
# be stored so that it does not have to be recomputed the next time the
|
92
|
-
# same version string is used.
|
93
|
-
# * <tt>some_hash</tt> will dynamically create an image
|
94
|
-
# that is created according to the options in <tt>some_hash</tt>. This
|
95
|
-
# accepts exactly the same options as Magick's version feature.
|
96
|
-
#
|
97
|
-
# The version produced by RMagick will be stored in a special sub-directory.
|
98
|
-
# The directory's name will be derived from the options you specified
|
99
|
-
# (via a hash function) but if you want
|
100
|
-
# to set it yourself, you can use the <tt>:name => name</tt> option.
|
101
|
-
#
|
102
|
-
# Examples:
|
103
|
-
#
|
104
|
-
# <%= url_for_image_column @entry, "image", "640x480" %>
|
105
|
-
#
|
106
|
-
# will produce an URL like this
|
107
|
-
#
|
108
|
-
# /entry/image/42/bdn19n/filename.jpg
|
109
|
-
# # "640x480".hash.abs.to_s(36) == "bdn19n"
|
110
|
-
#
|
111
|
-
# and
|
112
|
-
#
|
113
|
-
# <%= url_for_image_column @entry, "image",
|
114
|
-
# :size => "50x50", :crop => "1:1", :name => "thumb" %>
|
115
|
-
#
|
116
|
-
# will produce something like this:
|
117
|
-
#
|
118
|
-
# /entry/image/42/thumb/filename.jpg
|
119
|
-
#
|
120
|
-
# Hint: If you are using the same geometry string / options hash multiple times, you should
|
121
|
-
# define it in a helper to stay with DRY. Another option is to define it in the model via
|
122
|
-
# FileColumn::Magick's <tt>:versions</tt> feature and then refer to it via a symbol.
|
123
|
-
#
|
124
|
-
# The URL produced by this method is relative to your application's root URL,
|
125
|
-
# although it will start with a slash.
|
126
|
-
# If you pass this URL to rails' +image_tag+ helper, it will be converted to an
|
127
|
-
# absolute URL automatically.
|
128
|
-
# If there is currently no image uploaded, or there is a problem while loading
|
129
|
-
# the image this method will return +nil+.
|
130
|
-
def url_for_image_column(object, method, options=nil, store_url_for_options={})
|
131
|
-
case object
|
132
|
-
when String, Symbol
|
133
|
-
object = instance_variable_get("@#{object.to_s}")
|
134
|
-
end
|
135
|
-
subdir = nil
|
136
|
-
if options
|
137
|
-
subdir = object.send("#{method}_state").create_magick_version_if_needed(options)
|
138
|
-
end
|
139
|
-
if subdir.nil?
|
140
|
-
nil
|
141
|
-
else
|
142
|
-
url_for_file_column(object, method, subdir, store_url_for_options)
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
80
|
private
|
147
81
|
def get_relative_url_for_rails(rails_version)
|
148
82
|
(rails_version == 2 ? ActionController::Base.relative_url_root : Rails.application.config.action_controller.relative_url_root).to_s
|
data/lib/rails_file_column.rb
CHANGED
data/test/abstract_unit.rb
CHANGED
@@ -2,6 +2,7 @@ require 'thread'
|
|
2
2
|
require 'test/unit'
|
3
3
|
require 'rubygems'
|
4
4
|
require 'active_support'
|
5
|
+
require 'active_support/core_ext/numeric/bytes' # is not loaded when active_support loads.
|
5
6
|
require 'active_record'
|
6
7
|
require 'action_view'
|
7
8
|
require 'action_controller'
|
@@ -44,10 +45,10 @@ module Rails
|
|
44
45
|
end
|
45
46
|
|
46
47
|
|
47
|
-
require 'file_column'
|
48
|
-
require 'file_column_helper'
|
49
|
-
require 'file_compat'
|
50
|
-
require 'test_case'
|
48
|
+
require File.expand_path(File.dirname(__FILE__) + '/../lib/file_column')
|
49
|
+
require File.expand_path(File.dirname(__FILE__) + '/../lib/file_column_helper')
|
50
|
+
require File.expand_path(File.dirname(__FILE__) + '/../lib/file_compat')
|
51
|
+
require File.expand_path(File.dirname(__FILE__) + '/../lib/test_case')
|
51
52
|
|
52
53
|
# do not use the file executable normally in our tests as
|
53
54
|
# it may not be present on the machine we are running on
|
@@ -74,6 +75,13 @@ class Test::Unit::TestCase
|
|
74
75
|
assert_equal normalize_path(expected_path), normalize_path(path)
|
75
76
|
end
|
76
77
|
|
78
|
+
def with_failing_validation(klass, column, &block)
|
79
|
+
klass.validates_each(column) do |record, attr, value|
|
80
|
+
record.errors.add(column, 'some stupid error')
|
81
|
+
end
|
82
|
+
yield
|
83
|
+
klass.clear_validators!
|
84
|
+
end
|
77
85
|
|
78
86
|
private
|
79
87
|
|
@@ -82,10 +90,8 @@ class Test::Unit::TestCase
|
|
82
90
|
end
|
83
91
|
|
84
92
|
def clear_validations
|
85
|
-
|
86
|
-
|
87
|
-
Movie.write_inheritable_attribute attr, []
|
88
|
-
end
|
93
|
+
Entry.clear_validators!
|
94
|
+
Movie.clear_validators!
|
89
95
|
end
|
90
96
|
|
91
97
|
def file_path(filename)
|
data/test/connection.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/abstract_unit'
|
2
|
-
require File.dirname(__FILE__) + '/fixtures/entry'
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/abstract_unit')
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/fixtures/entry')
|
3
3
|
|
4
4
|
class UrlForFileColumnTest < Test::Unit::TestCase
|
5
5
|
include FileColumnHelper
|
@@ -81,7 +81,6 @@ class UrlForFileColumnWithContextPathTest < Test::Unit::TestCase
|
|
81
81
|
|
82
82
|
# mock up some request data structures for AssetTagHelper
|
83
83
|
@request = RequestMock.new
|
84
|
-
ActionController::Base.relative_url_root = "/foo/bar"
|
85
84
|
@controller = self
|
86
85
|
end
|
87
86
|
|
@@ -89,11 +88,11 @@ class UrlForFileColumnWithContextPathTest < Test::Unit::TestCase
|
|
89
88
|
@request
|
90
89
|
end
|
91
90
|
|
92
|
-
IMAGE_URL = %r{^/foo/bar/entry/image/.+/skanthak.png
|
91
|
+
IMAGE_URL = %r{^/foo/bar/entry/image/.+/skanthak.png}
|
93
92
|
def test_with_image_tag
|
94
93
|
e = Entry.new(:image => upload(f("skanthak.png")))
|
95
|
-
html = image_tag url_for_file_column(e, "image")
|
96
94
|
|
95
|
+
html = image_tag url_for_file_column(e, "image")
|
97
96
|
url = html.scan(/src=\"([^?]+)\?*.*\"/).first.first
|
98
97
|
|
99
98
|
assert_match IMAGE_URL, url
|
@@ -113,4 +112,9 @@ class UrlForFileColumnWithContextPathTest < Test::Unit::TestCase
|
|
113
112
|
url_for_file_column(e, "image", :absolute => true)
|
114
113
|
assert_equal "/foo/bar", get_relative_url_for_rails(Rails::VERSION::MAJOR)
|
115
114
|
end
|
115
|
+
|
116
|
+
private
|
117
|
+
def config
|
118
|
+
OpenStruct.new(relative_url_root: '/foo/bar')
|
119
|
+
end
|
116
120
|
end
|
data/test/file_column_test.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
|
-
require File.dirname(__FILE__) + '/abstract_unit'
|
3
|
-
|
4
|
-
require File.dirname(__FILE__) + '/fixtures/entry'
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/abstract_unit')
|
3
|
+
require File.expand_path(File.dirname(__FILE__) + '/fixtures/entry')
|
5
4
|
|
6
5
|
class Movie < ActiveRecord::Base
|
7
6
|
end
|
@@ -143,7 +142,7 @@ class FileColumnTest < Test::Unit::TestCase
|
|
143
142
|
|
144
143
|
# run this test only if the machine we are running on
|
145
144
|
# has the file utility installed
|
146
|
-
if File.executable?(FILE_UTILITY)
|
145
|
+
if File.executable?(FILE_UTILITY)
|
147
146
|
e = Entry.new(:image => uploaded_file(file_path("skanthak.png"), "", "skanthak.jpg"))
|
148
147
|
|
149
148
|
assert_equal "skanthak.png", File.basename(e.image)
|
@@ -256,11 +255,14 @@ class FileColumnTest < Test::Unit::TestCase
|
|
256
255
|
end
|
257
256
|
|
258
257
|
def test_keep_tmp_image
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
258
|
+
image_temp = ''
|
259
|
+
local_path = ''
|
260
|
+
with_failing_validation(Entry, :image) do
|
261
|
+
e = Entry.new("image" => uploaded_file(file_path("kerb.jpg"), "image/jpeg", "kerb.jpg"))
|
262
|
+
assert !e.save, "e should not save due to validation errors"
|
263
|
+
assert File.exists?(local_path = e.image)
|
264
|
+
image_temp = e.image_temp
|
265
|
+
end
|
264
266
|
e = Entry.new("image_temp" => image_temp)
|
265
267
|
assert_equal local_path, e.image
|
266
268
|
assert e.save
|
@@ -271,11 +273,13 @@ class FileColumnTest < Test::Unit::TestCase
|
|
271
273
|
e = Entry.new("image" =>uploaded_file(file_path("kerb.jpg"), "image/jpeg", "kerb.jpg"))
|
272
274
|
assert e.save
|
273
275
|
assert File.exists?(local_path = e.image)
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
276
|
+
temp_path = ''
|
277
|
+
with_failing_validation(Entry, :image) do
|
278
|
+
e = Entry.find(e.id)
|
279
|
+
e.image = uploaded_file(file_path("skanthak.png"), "image/png", "skanthak.png")
|
280
|
+
assert !e.save
|
281
|
+
temp_path = e.image_temp
|
282
|
+
end
|
279
283
|
e = Entry.find(e.id)
|
280
284
|
e.image_temp = temp_path
|
281
285
|
assert e.save
|
@@ -293,11 +297,14 @@ class FileColumnTest < Test::Unit::TestCase
|
|
293
297
|
end
|
294
298
|
|
295
299
|
def do_test_replace_tmp_image(order)
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
300
|
+
image_temp = ''
|
301
|
+
temp_path = ''
|
302
|
+
with_failing_validation(Entry, :image) do
|
303
|
+
e = Entry.new("image" => uploaded_file(file_path("kerb.jpg"), "image/jpeg", "kerb.jpg"))
|
304
|
+
assert !e.save
|
305
|
+
image_temp = e.image_temp
|
306
|
+
temp_path = e.image
|
307
|
+
end
|
301
308
|
new_img = uploaded_file(file_path("skanthak.png"), "image/png", "skanthak.png")
|
302
309
|
e = Entry.new
|
303
310
|
for method in order
|
@@ -515,7 +522,8 @@ class FileColumnTest < Test::Unit::TestCase
|
|
515
522
|
|
516
523
|
e.image = upload(f("skanthak.png"))
|
517
524
|
assert !e.save
|
518
|
-
assert e.
|
525
|
+
assert e.invalid? :image
|
526
|
+
assert_equal ['is smaller than the allowed size range.'], e.errors.messages[:image]
|
519
527
|
end
|
520
528
|
|
521
529
|
def test_validates_file_format_simple
|
@@ -529,19 +537,8 @@ class FileColumnTest < Test::Unit::TestCase
|
|
529
537
|
|
530
538
|
e.image = upload(f("mysql.sql"))
|
531
539
|
assert !e.save
|
532
|
-
assert
|
533
|
-
|
534
|
-
end
|
535
|
-
|
536
|
-
def test_validates_image_size
|
537
|
-
Entry.validates_image_size :image, :min => "640x480"
|
538
|
-
|
539
|
-
e = Entry.new(:image => upload(f("kerb.jpg")))
|
540
|
-
assert e.save
|
541
|
-
|
542
|
-
e = Entry.new(:image => upload(f("skanthak.png")))
|
543
|
-
assert !e.save
|
544
|
-
assert e.errors.invalid?("image")
|
540
|
+
assert e.invalid? :image
|
541
|
+
assert_equal ['is not a valid format.'], e.errors.messages[:image]
|
545
542
|
end
|
546
543
|
|
547
544
|
def do_permission_test(uploaded_file, permissions=0641)
|