thumbnail_on_demand 0.0.0 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +5 -0
- data/Gemfile.lock +25 -0
- data/VERSION +1 -1
- data/lib/thumbnail_on_demand.rb +81 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/thumbnail_on_demand_spec.rb +13 -3
- data/thumbnail_on_demand.gemspec +60 -0
- metadata +12 -11
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,12 +1,34 @@
|
|
1
1
|
GEM
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
|
+
activemodel (3.1.1)
|
5
|
+
activesupport (= 3.1.1)
|
6
|
+
builder (~> 3.0.0)
|
7
|
+
i18n (~> 0.6)
|
8
|
+
activerecord (3.1.1)
|
9
|
+
activemodel (= 3.1.1)
|
10
|
+
activesupport (= 3.1.1)
|
11
|
+
arel (~> 2.2.1)
|
12
|
+
tzinfo (~> 0.3.29)
|
13
|
+
activesupport (3.1.1)
|
14
|
+
multi_json (~> 1.0)
|
15
|
+
arel (2.2.1)
|
16
|
+
builder (3.0.0)
|
17
|
+
cocaine (0.2.0)
|
4
18
|
diff-lcs (1.1.3)
|
5
19
|
git (1.2.5)
|
20
|
+
i18n (0.6.0)
|
6
21
|
jeweler (1.6.4)
|
7
22
|
bundler (~> 1.0)
|
8
23
|
git (>= 1.2.5)
|
9
24
|
rake
|
25
|
+
mime-types (1.16)
|
26
|
+
multi_json (1.0.3)
|
27
|
+
paperclip (2.4.5)
|
28
|
+
activerecord (>= 2.3.0)
|
29
|
+
activesupport (>= 2.3.2)
|
30
|
+
cocaine (>= 0.0.2)
|
31
|
+
mime-types
|
10
32
|
rake (0.9.2)
|
11
33
|
rcov (0.9.11)
|
12
34
|
rspec (2.3.0)
|
@@ -17,12 +39,15 @@ GEM
|
|
17
39
|
rspec-expectations (2.3.0)
|
18
40
|
diff-lcs (~> 1.1.2)
|
19
41
|
rspec-mocks (2.3.0)
|
42
|
+
tzinfo (0.3.30)
|
20
43
|
|
21
44
|
PLATFORMS
|
22
45
|
ruby
|
23
46
|
|
24
47
|
DEPENDENCIES
|
48
|
+
activerecord (~> 3.0)
|
25
49
|
bundler (~> 1.0.0)
|
26
50
|
jeweler (~> 1.6.4)
|
51
|
+
paperclip (~> 2.4.4)
|
27
52
|
rcov
|
28
53
|
rspec (~> 2.3.0)
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.1.0
|
data/lib/thumbnail_on_demand.rb
CHANGED
@@ -0,0 +1,81 @@
|
|
1
|
+
Paperclip::Attachment.class_eval do
|
2
|
+
|
3
|
+
IMAGE_EXTENSIONS = [".jpg", ".jpeg", ".jpe", ".png", ".gif", ".bmp"]
|
4
|
+
|
5
|
+
# Creates a thumbnail if it doesn't already exist and not if it does. Either way, it returns the URL to the thumbnail
|
6
|
+
#
|
7
|
+
# @param [Symbol] style A symbol telling which style to use (from the :on_demand_styles option)
|
8
|
+
# @param [Symbol] from_style A symbol telling which paperclip style to create the thumbnail from.
|
9
|
+
# If either nil or :original it will use the originally uploaded image (default_style)
|
10
|
+
# @param [Object] options An object of url options, same as the second param in paperclip's url method
|
11
|
+
# @return [String] The URL to the thumbnail
|
12
|
+
# TODO check to be sure the attachment is an image (thumbnailable)?
|
13
|
+
# TODO S3/other storage support
|
14
|
+
# TODO should remove thumbnails when removing the orginal paperclip attachment
|
15
|
+
# TODO would be nice to have the ability to crop default_url according to the requested style
|
16
|
+
def thumbnail(style_name, from_style = nil, options = {})
|
17
|
+
options = handle_url_options(options)
|
18
|
+
thumbnail_url = interpolate(most_appropriate_url, style_name)
|
19
|
+
thumbnail_path = original_filename.nil? ? nil : interpolate(@options.path, style_name)
|
20
|
+
# Since this add-on only supports images, make sure thumbnail_path is going to save with an image file extension.
|
21
|
+
# Otherwise it will save an image with the extension of the original file, which will be wrong if the original isn't an image.
|
22
|
+
# We do this to support thumbnailing of other attachment types, such as video (see README.rdoc for an example).
|
23
|
+
# TODO we shouldn't just assume jpg format when picking an image extension to add
|
24
|
+
unless IMAGE_EXTENSIONS.include? File.extname(thumbnail_path)
|
25
|
+
thumbnail_path = "#{thumbnail_path}#{IMAGE_EXTENSIONS.first}"
|
26
|
+
thumbnail_url = "#{thumbnail_url}#{IMAGE_EXTENSIONS.first}"
|
27
|
+
end
|
28
|
+
# Initalize original_path using the default_style (original attachment)
|
29
|
+
original_path = original_filename.nil? ? nil : interpolate(@options.path, default_style)
|
30
|
+
# We can optionally create the thumbnail from another (non on-demand) style and set original_path to that instead
|
31
|
+
# TODO test to be sure from_style is in @options.styles
|
32
|
+
# TODO if from_style isn't found in @options.styles also check @options.raw_options[:on_demand_styles] and attempt to use it from there
|
33
|
+
unless from_style.nil? || from_style == :original
|
34
|
+
# get the path to the thumbnail
|
35
|
+
from_style_path = original_filename.nil? ? nil : interpolate(@options.path, from_style)
|
36
|
+
# only try to use it if that file exists
|
37
|
+
if File.exists? from_style_path
|
38
|
+
original_path = from_style_path
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# If thumbnail_path or original_filename is nil then we have no image so we'll let it return the default_url
|
43
|
+
unless thumbnail_path.nil? || original_filename.nil? || !File.exists?(original_path)
|
44
|
+
# If the file doesn't exist we need to make it first
|
45
|
+
unless File.exists?(thumbnail_path)
|
46
|
+
begin
|
47
|
+
# TODO should create this on init of paperclip somehow and not here?
|
48
|
+
style = Paperclip::Style.new(style_name, @options.raw_options[:on_demand_styles][:"#{style_name}"], self)
|
49
|
+
original_file = File.new(original_path)
|
50
|
+
if original_file && original_file.size > 0
|
51
|
+
temp_thumbnail = Paperclip.processor(:thumbnail).make(original_file, style.processor_options, self)
|
52
|
+
original_file.close
|
53
|
+
if temp_thumbnail && temp_thumbnail.size > 0
|
54
|
+
# If the folder doesn't exist, make it
|
55
|
+
FileUtils.mkdir_p(File.dirname(thumbnail_path))
|
56
|
+
# Save the thumnail to thumbnail_path
|
57
|
+
FileUtils.cp(temp_thumbnail.path, thumbnail_path)
|
58
|
+
# Close and unlink the tempfile
|
59
|
+
temp_thumbnail.close!
|
60
|
+
else
|
61
|
+
thumbnail_url = interpolate(default_url, style_name)
|
62
|
+
end
|
63
|
+
else
|
64
|
+
thumbnail_url = interpolate(default_url, style_name)
|
65
|
+
end
|
66
|
+
rescue
|
67
|
+
thumbnail_url = interpolate(default_url, style_name)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
else
|
71
|
+
thumbnail_url = interpolate(default_url, style_name)
|
72
|
+
end
|
73
|
+
|
74
|
+
thumbnail_url = url_timestamp(thumbnail_url) if options[:timestamp]
|
75
|
+
thumbnail_url = escape_url(thumbnail_url) if options[:escape]
|
76
|
+
thumbnail_url
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
class ThumbnailOnDemandError < StandardError; end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
2
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
3
|
require 'rspec'
|
4
|
+
require 'active_record'
|
5
|
+
require 'paperclip'
|
4
6
|
require 'thumbnail_on_demand'
|
5
7
|
|
6
8
|
# Requires supporting files with custom matchers and macros, etc,
|
@@ -1,7 +1,17 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
class TestModel < ActiveRecord::Base
|
4
|
+
include Paperclip
|
5
|
+
has_attached_file :attachment,
|
6
|
+
:styles => { :large => '1000x1000>'},
|
7
|
+
:on_demand_styles => { :small => "600x600>" }
|
8
|
+
end
|
9
|
+
|
10
|
+
describe ThumbnailOnDemand do
|
11
|
+
before(:each) do
|
12
|
+
|
13
|
+
end
|
14
|
+
describe "#thumbnail" do
|
15
|
+
it "should return a string"
|
6
16
|
end
|
7
17
|
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{thumbnail_on_demand}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = [%q{Jonathan Dean}]
|
12
|
+
s.date = %q{2011-10-27}
|
13
|
+
s.description = %q{An add-on to paperclip that allows for on-demand, cached thumbnail creation}
|
14
|
+
s.email = %q{jon@jonathandean.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".rspec",
|
22
|
+
"Gemfile",
|
23
|
+
"Gemfile.lock",
|
24
|
+
"LICENSE.txt",
|
25
|
+
"README.rdoc",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"lib/thumbnail_on_demand.rb",
|
29
|
+
"spec/spec_helper.rb",
|
30
|
+
"spec/thumbnail_on_demand_spec.rb",
|
31
|
+
"thumbnail_on_demand.gemspec"
|
32
|
+
]
|
33
|
+
s.homepage = %q{http://github.com/jonathandean/thumbnail_on_demand}
|
34
|
+
s.licenses = [%q{MIT}]
|
35
|
+
s.require_paths = [%q{lib}]
|
36
|
+
s.rubygems_version = %q{1.8.6}
|
37
|
+
s.summary = %q{An add-on to paperclip that allows for on-demand, cached thumbnail creation}
|
38
|
+
|
39
|
+
if s.respond_to? :specification_version then
|
40
|
+
s.specification_version = 3
|
41
|
+
|
42
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
43
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
|
44
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
45
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
|
46
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
47
|
+
else
|
48
|
+
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
|
49
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
50
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
51
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
52
|
+
end
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
|
55
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
56
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
57
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: thumbnail_on_demand
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-10-
|
12
|
+
date: 2011-10-27 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70355859360900 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 2.3.0
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70355859360900
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: bundler
|
27
|
-
requirement: &
|
27
|
+
requirement: &70355859360420 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 1.0.0
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70355859360420
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: jeweler
|
38
|
-
requirement: &
|
38
|
+
requirement: &70355859322820 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 1.6.4
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70355859322820
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rcov
|
49
|
-
requirement: &
|
49
|
+
requirement: &70355859322340 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70355859322340
|
58
58
|
description: An add-on to paperclip that allows for on-demand, cached thumbnail creation
|
59
59
|
email: jon@jonathandean.com
|
60
60
|
executables: []
|
@@ -74,6 +74,7 @@ files:
|
|
74
74
|
- lib/thumbnail_on_demand.rb
|
75
75
|
- spec/spec_helper.rb
|
76
76
|
- spec/thumbnail_on_demand_spec.rb
|
77
|
+
- thumbnail_on_demand.gemspec
|
77
78
|
homepage: http://github.com/jonathandean/thumbnail_on_demand
|
78
79
|
licenses:
|
79
80
|
- MIT
|
@@ -89,7 +90,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
89
90
|
version: '0'
|
90
91
|
segments:
|
91
92
|
- 0
|
92
|
-
hash:
|
93
|
+
hash: 1666669109934222201
|
93
94
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
95
|
none: false
|
95
96
|
requirements:
|