sinatra-thumbnails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/LICENSE.txt +20 -0
  2. data/README.rdoc +19 -0
  3. data/lib/sinatra/thumbnails.rb +105 -0
  4. metadata +201 -0
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 João Távora
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ = sinatra-thumbnails
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to sinatra-thumbnails
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 João Távora. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1,105 @@
1
+ require 'sinatra/base'
2
+
3
+ module Sinatra
4
+ module Thumbnails
5
+ module Settings
6
+ def method_missing(name, *args)
7
+ if (name.to_s =~ /=$/)
8
+ key = name.to_s[0..-2].to_sym
9
+ raise NoMethodError unless self[key]
10
+ raise ArgumentError if args.length != 1
11
+ Thumbnails::Settings.send(:define_method, name) do |val|
12
+ self[key] = val
13
+ end
14
+ self.send(args[0])
15
+ elsif args.length == 0
16
+ raise NoMethodError unless self[name]
17
+ Thumbnails::Settings.send(:define_method, name) do
18
+ self[name]
19
+ end
20
+ self.send(name)
21
+ else
22
+ raise ArgumentError
23
+ end
24
+ end
25
+ end
26
+
27
+ def self.make_thumb(file, format, original_extension)
28
+ original_extension ||= "jpg"
29
+ thumbnail_file = File.join(settings.thumbnail_path, "#{format}/#{file}")
30
+ FileUtils.mkdir_p File.dirname(thumbnail_file)
31
+ orig_file = File.join(settings.image_path_prefix, file.gsub(/(.*\.)(.*$)/,"\\1#{original_extension}"))
32
+
33
+ unless File.exists?(thumbnail_file) and (File.stat(thumbnail_file).mtime >= File.stat(orig_file).mtime)
34
+
35
+ if original_extension =~ /mov$/
36
+ ffmpeg(orig_file, thumbnail_file, format)
37
+ else
38
+ convert(orig_file, thumbnail_file, format)
39
+ end
40
+ end
41
+ thumbnail_file
42
+ end
43
+
44
+ def self.im_version
45
+ `convert --version` =~ /Version: ImageMagick ([\d\.]+)/
46
+ Regexp.last_match(1).split('.').map{|s|s.to_i}
47
+ end
48
+
49
+ def self.convert(src, dest, format)
50
+ if (format =~ /(.*)-crop$/)
51
+ if (im_version <=> [6,6,4]) >= 0
52
+ format = $1 + "^" + " -gravity center -extent " + $1
53
+ else
54
+ format = $1
55
+ end
56
+ end
57
+ FileUtils.mkdir_p(File.dirname(dest))
58
+ command = "#{Sinatra::Thumbnails.settings.convert_executable} -define jpeg:size=400x400 '#{src}' -thumbnail #{format} '#{dest}'"
59
+ # puts "Sinatra::Thumbnails: issuing \"#{command}\""
60
+ run_command(command)
61
+ end
62
+
63
+ def self.ffmpeg(src, dest, format)
64
+ puts "making movie thumb on the fly"
65
+ seconds = (src =~ /\.ss([\d]+)/) ? Regexp.last_match(1).to_i : 0
66
+ command = "#{Sinatra::Thumbnails.settings.ffmpeg_executable} -y -i '#{src}' -an -ss #{seconds} -r 1 -vframes 1 -f mjpeg '#{dest}'"
67
+ # puts "Sinatra::Thumbnails: issuing \"#{command}\""
68
+ run_command(command)
69
+ convert(dest, dest, format)
70
+ end
71
+
72
+ def self.run_command(command)
73
+ output = `#{command} 2>&1`
74
+ raise "couldn't run #{command}: #{output}" unless $?.success?
75
+ end
76
+
77
+
78
+ def self.settings
79
+ @@settings ||= { :convert_executable => 'convert' ,
80
+ :ffmpeg_executable => 'ffmpeg' ,
81
+ :thumbnail_path => 'public/thumbnails' ,
82
+ :image_path_prefix => 'public' ,
83
+ :thumbnail_extension => 'png' ,
84
+ :thumbnail_format => '100x100' }.extend(Thumbnails::Settings)
85
+ end
86
+
87
+ module Helpers
88
+ def thumbnail_url_for(asset, format = Thumbnails.settings.thumbnail_format)
89
+ almost_original = asset.gsub(/(.*\.)(.*$)/,"\\1#{Thumbnails.settings.thumbnail_extension}")
90
+ original_extension = Regexp.last_match(2)
91
+ "#{Thumbnails.settings.thumbnail_path}/#{format}/#{almost_original}?original_extension=#{original_extension}"
92
+ end
93
+ end
94
+
95
+ def self.registered(app)
96
+ app.helpers Helpers
97
+
98
+ # gotta make this a regexp, :asset wont match stuff with "/" in it
99
+ app.get /#{settings.thumbnail_path}\/([^\/]+)\/(.*)$/ do
100
+ send_file Thumbnails.make_thumb(params[:captures][1], params[:captures][0], params[:original_extension])
101
+ end
102
+ end
103
+ end
104
+ register Thumbnails
105
+ end
metadata ADDED
@@ -0,0 +1,201 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-thumbnails
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - !binary |
14
+ Sm/Do28gVMOhdm9yYQ==
15
+
16
+ autorequire:
17
+ bindir: bin
18
+ cert_chain: []
19
+
20
+ date: 2011-08-29 00:00:00 Z
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ type: :runtime
24
+ version_requirements: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ requirement: *id001
34
+ prerelease: false
35
+ name: sinatra
36
+ - !ruby/object:Gem::Dependency
37
+ type: :development
38
+ version_requirements: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ requirement: *id002
48
+ prerelease: false
49
+ name: rb-fsevent
50
+ - !ruby/object:Gem::Dependency
51
+ type: :development
52
+ version_requirements: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ requirement: *id003
62
+ prerelease: false
63
+ name: guard
64
+ - !ruby/object:Gem::Dependency
65
+ type: :development
66
+ version_requirements: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ requirement: *id004
76
+ prerelease: false
77
+ name: growl_notify
78
+ - !ruby/object:Gem::Dependency
79
+ type: :development
80
+ version_requirements: &id005 !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ hash: 31
86
+ segments:
87
+ - 2
88
+ - 4
89
+ - 0
90
+ version: 2.4.0
91
+ requirement: *id005
92
+ prerelease: false
93
+ name: rspec
94
+ - !ruby/object:Gem::Dependency
95
+ type: :development
96
+ version_requirements: &id006 !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ hash: 23
102
+ segments:
103
+ - 1
104
+ - 0
105
+ - 0
106
+ version: 1.0.0
107
+ requirement: *id006
108
+ prerelease: false
109
+ name: bundler
110
+ - !ruby/object:Gem::Dependency
111
+ type: :development
112
+ version_requirements: &id007 !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ hash: 7
118
+ segments:
119
+ - 1
120
+ - 6
121
+ - 4
122
+ version: 1.6.4
123
+ requirement: *id007
124
+ prerelease: false
125
+ name: jeweler
126
+ - !ruby/object:Gem::Dependency
127
+ type: :development
128
+ version_requirements: &id008 !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ hash: 3
134
+ segments:
135
+ - 0
136
+ version: "0"
137
+ requirement: *id008
138
+ prerelease: false
139
+ name: rcov
140
+ - !ruby/object:Gem::Dependency
141
+ type: :development
142
+ version_requirements: &id009 !ruby/object:Gem::Requirement
143
+ none: false
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ hash: 3
148
+ segments:
149
+ - 0
150
+ version: "0"
151
+ requirement: *id009
152
+ prerelease: false
153
+ name: rack-test
154
+ description: farfalini
155
+ email: joaotavora@gmail.com
156
+ executables: []
157
+
158
+ extensions: []
159
+
160
+ extra_rdoc_files:
161
+ - LICENSE.txt
162
+ - README.rdoc
163
+ files:
164
+ - lib/sinatra/thumbnails.rb
165
+ - LICENSE.txt
166
+ - README.rdoc
167
+ homepage: http://github.com/capitaomorte/sinatra-thumbnails
168
+ licenses:
169
+ - MIT
170
+ post_install_message:
171
+ rdoc_options: []
172
+
173
+ require_paths:
174
+ - lib
175
+ required_ruby_version: !ruby/object:Gem::Requirement
176
+ none: false
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ hash: 3
181
+ segments:
182
+ - 0
183
+ version: "0"
184
+ required_rubygems_version: !ruby/object:Gem::Requirement
185
+ none: false
186
+ requirements:
187
+ - - ">="
188
+ - !ruby/object:Gem::Version
189
+ hash: 3
190
+ segments:
191
+ - 0
192
+ version: "0"
193
+ requirements: []
194
+
195
+ rubyforge_project:
196
+ rubygems_version: 1.8.6
197
+ signing_key:
198
+ specification_version: 3
199
+ summary: tururu turur
200
+ test_files: []
201
+