peterpunk-merb_paperclip 0.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +47 -0
- data/Rakefile +56 -0
- data/TODO +0 -0
- data/lib/generators/paperclip_generator.rb +53 -0
- data/lib/generators/templates/file_name.rb +17 -0
- data/lib/merb_paperclip.rb +19 -0
- data/lib/merb_paperclip/merbtasks.rb +38 -0
- data/lib/paperclip.rb +246 -0
- data/lib/paperclip/attachment.rb +287 -0
- data/lib/paperclip/geometry.rb +109 -0
- data/lib/paperclip/iostream.rb +47 -0
- data/lib/paperclip/storage.rb +204 -0
- data/lib/paperclip/thumbnail.rb +80 -0
- data/lib/paperclip/upfile.rb +37 -0
- data/merb_paperclip.gemspec +31 -0
- data/spec/merb_paperclip_spec.rb +7 -0
- data/spec/spec_helper.rb +2 -0
- metadata +77 -0
@@ -0,0 +1,80 @@
|
|
1
|
+
module Paperclip
|
2
|
+
# Handles thumbnailing images that are uploaded.
|
3
|
+
class Thumbnail
|
4
|
+
|
5
|
+
attr_accessor :file, :current_geometry, :target_geometry, :format, :whiny_thumbnails
|
6
|
+
|
7
|
+
# Creates a Thumbnail object set to work on the +file+ given. It
|
8
|
+
# will attempt to transform the image into one defined by +target_geometry+
|
9
|
+
# which is a "WxH"-style string. +format+ will be inferred from the +file+
|
10
|
+
# unless specified. Thumbnail creation will raise no errors unless
|
11
|
+
# +whiny_thumbnails+ is true (which it is, by default.
|
12
|
+
def initialize file, target_geometry, format = nil, whiny_thumbnails = true
|
13
|
+
@file = file
|
14
|
+
@crop = target_geometry[-1,1] == '#'
|
15
|
+
@target_geometry = Geometry.parse target_geometry
|
16
|
+
@current_geometry = Geometry.from_file file
|
17
|
+
@whiny_thumbnails = whiny_thumbnails
|
18
|
+
|
19
|
+
@current_format = File.extname(@file.path)
|
20
|
+
@basename = File.basename(@file.path, @current_format)
|
21
|
+
|
22
|
+
@format = format
|
23
|
+
end
|
24
|
+
|
25
|
+
# Creates a thumbnail, as specified in +initialize+, +make+s it, and returns the
|
26
|
+
# resulting Tempfile.
|
27
|
+
def self.make file, dimensions, format = nil, whiny_thumbnails = true
|
28
|
+
new(file, dimensions, format, whiny_thumbnails).make
|
29
|
+
end
|
30
|
+
|
31
|
+
# Returns true if the +target_geometry+ is meant to crop.
|
32
|
+
def crop?
|
33
|
+
@crop
|
34
|
+
end
|
35
|
+
|
36
|
+
# Performs the conversion of the +file+ into a thumbnail. Returns the Tempfile
|
37
|
+
# that contains the new image.
|
38
|
+
def make
|
39
|
+
src = @file
|
40
|
+
dst = Tempfile.new([@basename, @format].compact.join("."))
|
41
|
+
dst.binmode
|
42
|
+
|
43
|
+
command = <<-end_command
|
44
|
+
#{ Paperclip.path_for_command('convert') }
|
45
|
+
"#{ File.expand_path(src.path) }"
|
46
|
+
#{ transformation_command }
|
47
|
+
"#{ File.expand_path(dst.path) }"
|
48
|
+
end_command
|
49
|
+
success = system(command.gsub(/\s+/, " "))
|
50
|
+
|
51
|
+
if success && $?.exitstatus != 0 && @whiny_thumbnails
|
52
|
+
raise PaperclipError, "There was an error processing this thumbnail"
|
53
|
+
end
|
54
|
+
|
55
|
+
dst
|
56
|
+
end
|
57
|
+
|
58
|
+
# Returns the command ImageMagick's +convert+ needs to transform the image
|
59
|
+
# into the thumbnail.
|
60
|
+
def transformation_command
|
61
|
+
scale, crop = @current_geometry.transformation_to(@target_geometry, crop?)
|
62
|
+
trans = "-scale \"#{scale}\""
|
63
|
+
trans << " -crop \"#{crop}\" +repage" if crop
|
64
|
+
trans
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# Due to how ImageMagick handles its image format conversion and how Tempfile
|
69
|
+
# handles its naming scheme, it is necessary to override how Tempfile makes
|
70
|
+
# its names so as to allow for file extensions. Idea taken from the comments
|
71
|
+
# on this blog post:
|
72
|
+
# http://marsorange.com/archives/of-mogrify-ruby-tempfile-dynamic-class-definitions
|
73
|
+
class Tempfile < ::Tempfile
|
74
|
+
# Replaces Tempfile's +make_tmpname+ with one that honors file extensions.
|
75
|
+
def make_tmpname(basename, n)
|
76
|
+
extension = File.extname(basename)
|
77
|
+
sprintf("%s,%d,%d%s", File.basename(basename, extension), $$, n, extension)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Paperclip
|
2
|
+
# The Upfile module is a convenience module for adding uploaded-file-type methods
|
3
|
+
# to the +File+ class. Useful for testing.
|
4
|
+
# user.avatar = File.new("test/test_avatar.jpg")
|
5
|
+
module Upfile
|
6
|
+
|
7
|
+
# Infer the MIME-type of the file from the extension.
|
8
|
+
def content_type
|
9
|
+
type = (self.path.match(/\.(\w+)$/)[1] rescue "octet-stream").downcase
|
10
|
+
case type
|
11
|
+
when %r"jpe?g" then "image/jpeg"
|
12
|
+
when %r"tiff?" then "image/tiff"
|
13
|
+
when %r"png", "gif", "bmp" then "image/#{type}"
|
14
|
+
when "txt" then "text/plain"
|
15
|
+
when %r"html?" then "text/html"
|
16
|
+
when "csv", "xml", "css", "js" then "text/#{type}"
|
17
|
+
else "application/x-#{type}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Returns the file's normal name.
|
22
|
+
def original_filename
|
23
|
+
File.basename(self.path)
|
24
|
+
end
|
25
|
+
|
26
|
+
# Returns the size of the file.
|
27
|
+
def size
|
28
|
+
File.size(self)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
class File #:nodoc:
|
35
|
+
include Paperclip::Upfile
|
36
|
+
end
|
37
|
+
|
@@ -0,0 +1,31 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "merb_paperclip"
|
3
|
+
s.version = "0.9.3"
|
4
|
+
s.date = "2008-04-24"
|
5
|
+
s.summary = "Paperclip port for merb"
|
6
|
+
s.email = ""
|
7
|
+
s.homepage = "http://www.thoughtbot.com/projects/paperclip"
|
8
|
+
s.description = "Paperclip is a plugin for Ruby on RailsActiveRecord (this is a port for merb-ar) that lets files work as simply as any other attributes do. There are no extra database tables, only one library to install for image processing, and the ease of being able to refer to your files as easily as you refer to your other attributes."
|
9
|
+
s.has_rdoc = false
|
10
|
+
s.authors = ["Jon Yurek"]
|
11
|
+
s.files = ["README",
|
12
|
+
"TODO",
|
13
|
+
"Rakefile",
|
14
|
+
"merb_paperclip.gemspec",
|
15
|
+
"lib/generators/paperclip_generator.rb",
|
16
|
+
"lib/generators/templates/file_name.rb",
|
17
|
+
"lib/merb_paperclip/merbtasks.rb",
|
18
|
+
"lib/merb_paperclip.rb",
|
19
|
+
"lib/paperclip.rb",
|
20
|
+
"lib/paperclip/attachment.rb",
|
21
|
+
"lib/paperclip/geometry.rb",
|
22
|
+
"lib/paperclip/iostream.rb",
|
23
|
+
"lib/paperclip/storage.rb",
|
24
|
+
"lib/paperclip/thumbnail.rb",
|
25
|
+
"lib/paperclip/upfile.rb"]
|
26
|
+
s.test_files = ["spec/merb_paperclip_spec.rb",
|
27
|
+
"spec/spec_helper.rb"]
|
28
|
+
s.rdoc_options = ["--main", "README"]
|
29
|
+
s.extra_rdoc_files = ["README"]
|
30
|
+
s.add_dependency("activerecord", ["> 0.0.0"])
|
31
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: peterpunk-merb_paperclip
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jon Yurek
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-04-24 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activerecord
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 0.0.0
|
23
|
+
version:
|
24
|
+
description: Paperclip is a plugin for Ruby on RailsActiveRecord (this is a port for merb-ar) that lets files work as simply as any other attributes do. There are no extra database tables, only one library to install for image processing, and the ease of being able to refer to your files as easily as you refer to your other attributes.
|
25
|
+
email: ""
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files:
|
31
|
+
- README
|
32
|
+
files:
|
33
|
+
- README
|
34
|
+
- TODO
|
35
|
+
- Rakefile
|
36
|
+
- merb_paperclip.gemspec
|
37
|
+
- lib/generators/paperclip_generator.rb
|
38
|
+
- lib/generators/templates/file_name.rb
|
39
|
+
- lib/merb_paperclip/merbtasks.rb
|
40
|
+
- lib/merb_paperclip.rb
|
41
|
+
- lib/paperclip.rb
|
42
|
+
- lib/paperclip/attachment.rb
|
43
|
+
- lib/paperclip/geometry.rb
|
44
|
+
- lib/paperclip/iostream.rb
|
45
|
+
- lib/paperclip/storage.rb
|
46
|
+
- lib/paperclip/thumbnail.rb
|
47
|
+
- lib/paperclip/upfile.rb
|
48
|
+
has_rdoc: false
|
49
|
+
homepage: http://www.thoughtbot.com/projects/paperclip
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options:
|
52
|
+
- --main
|
53
|
+
- README
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
version:
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "0"
|
67
|
+
version:
|
68
|
+
requirements: []
|
69
|
+
|
70
|
+
rubyforge_project:
|
71
|
+
rubygems_version: 1.2.0
|
72
|
+
signing_key:
|
73
|
+
specification_version: 2
|
74
|
+
summary: Paperclip port for merb
|
75
|
+
test_files:
|
76
|
+
- spec/merb_paperclip_spec.rb
|
77
|
+
- spec/spec_helper.rb
|