dynamic_image 0.9.5 → 0.9.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +1 -1
- data/Rakefile +22 -22
- data/VERSION +1 -1
- data/app/controllers/images_controller.rb +64 -64
- data/app/models/image.rb +187 -187
- data/config/routes.rb +5 -5
- data/dynamic_image.gemspec +30 -36
- data/init.rb +1 -1
- data/lib/binary_storage.rb +16 -16
- data/lib/binary_storage/active_record_extensions.rb +127 -127
- data/lib/binary_storage/blob.rb +100 -100
- data/lib/dynamic_image.rb +54 -54
- data/lib/dynamic_image/active_record_extensions.rb +49 -49
- data/lib/dynamic_image/engine.rb +3 -3
- data/lib/dynamic_image/filterset.rb +72 -72
- data/lib/dynamic_image/helper.rb +102 -102
- data/lib/generators/dynamic_image/USAGE +0 -1
- data/lib/generators/dynamic_image/dynamic_image_generator.rb +27 -27
- metadata +10 -15
- data/test/dynamic_image_test.rb +0 -8
- data/test/test_helper.rb +0 -3
data/config/routes.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# Rails 3 routes
|
2
2
|
Rails.application.routes.draw do
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
match "dynamic_images/:id/:original(/:size(/:filterset))/*filename" => "images#render_dynamic_image", :size => /\d*x\d*/, :original => /original/
|
4
|
+
match "dynamic_images/:id(/:size(/:filterset))/*filename" => "images#render_dynamic_image", :size => /\d*x\d*/
|
5
|
+
# Legacy
|
6
|
+
match "dynamic_image/:id/:original(/:size(/:filterset))/*filename" => "images#render_dynamic_image", :size => /\d*x\d*/, :original => /original/
|
7
|
+
match "dynamic_image/:id(/:size(/:filterset))/*filename" => "images#render_dynamic_image", :size => /\d*x\d*/
|
8
8
|
end
|
9
9
|
|
10
10
|
# Rails 2 routes
|
data/dynamic_image.gemspec
CHANGED
@@ -1,56 +1,50 @@
|
|
1
1
|
# Generated by jeweler
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
|
-
s.name =
|
8
|
-
s.version = "0.9.
|
7
|
+
s.name = "dynamic_image"
|
8
|
+
s.version = "0.9.6"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Inge J\303\270rgensen"]
|
12
|
-
s.date =
|
13
|
-
s.email =
|
12
|
+
s.date = "2012-09-25"
|
13
|
+
s.email = "inge@elektronaut.no"
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"LICENSE",
|
16
|
-
|
16
|
+
"README.rdoc"
|
17
17
|
]
|
18
18
|
s.files = [
|
19
19
|
"LICENSE",
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
20
|
+
"README.rdoc",
|
21
|
+
"Rakefile",
|
22
|
+
"VERSION",
|
23
|
+
"app/controllers/images_controller.rb",
|
24
|
+
"app/models/image.rb",
|
25
|
+
"config/routes.rb",
|
26
|
+
"dynamic_image.gemspec",
|
27
|
+
"init.rb",
|
28
|
+
"install.rb",
|
29
|
+
"lib/binary_storage.rb",
|
30
|
+
"lib/binary_storage/active_record_extensions.rb",
|
31
|
+
"lib/binary_storage/blob.rb",
|
32
|
+
"lib/dynamic_image.rb",
|
33
|
+
"lib/dynamic_image/active_record_extensions.rb",
|
34
|
+
"lib/dynamic_image/engine.rb",
|
35
|
+
"lib/dynamic_image/filterset.rb",
|
36
|
+
"lib/dynamic_image/helper.rb",
|
37
|
+
"lib/generators/dynamic_image/USAGE",
|
38
|
+
"lib/generators/dynamic_image/dynamic_image_generator.rb",
|
39
|
+
"lib/generators/dynamic_image/templates/migrations/create_images.rb",
|
40
|
+
"uninstall.rb"
|
41
41
|
]
|
42
|
-
s.homepage =
|
43
|
-
s.rdoc_options = ["--charset=UTF-8"]
|
42
|
+
s.homepage = "http://github.com/elektronaut/dynamic_image"
|
44
43
|
s.require_paths = ["lib"]
|
45
|
-
s.rubygems_version =
|
46
|
-
s.summary =
|
47
|
-
s.test_files = [
|
48
|
-
"test/dynamic_image_test.rb",
|
49
|
-
"test/test_helper.rb"
|
50
|
-
]
|
44
|
+
s.rubygems_version = "1.8.24"
|
45
|
+
s.summary = "DynamicImage is a rails plugin providing transparent uploading and processing of image files."
|
51
46
|
|
52
47
|
if s.respond_to? :specification_version then
|
53
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
54
48
|
s.specification_version = 3
|
55
49
|
|
56
50
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
data/init.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require 'dynamic_image'
|
1
|
+
require 'dynamic_image'
|
data/lib/binary_storage.rb
CHANGED
@@ -8,21 +8,21 @@ require File.join(File.dirname(__FILE__), 'binary_storage/active_record_extensio
|
|
8
8
|
require File.join(File.dirname(__FILE__), 'binary_storage/blob')
|
9
9
|
|
10
10
|
module BinaryStorage
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
11
|
+
class << self
|
12
|
+
def storage_dir
|
13
|
+
@@storage_dir ||= Rails.root.join('db/binary_storage', Rails.env)
|
14
|
+
end
|
15
|
+
|
16
|
+
def storage_dir=(new_storage_dir)
|
17
|
+
@@storage_dir = new_storage_dir
|
18
|
+
end
|
19
|
+
|
20
|
+
def hexdigest_file(path)
|
21
|
+
Digest::SHA1.file(path).hexdigest
|
22
|
+
end
|
23
23
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
24
|
+
def hexdigest(string)
|
25
|
+
Digest::SHA1.hexdigest(string)
|
26
|
+
end
|
27
|
+
end
|
28
28
|
end
|
@@ -1,144 +1,144 @@
|
|
1
1
|
require 'binary_storage'
|
2
2
|
|
3
3
|
module BinaryStorage
|
4
|
-
|
4
|
+
module ActiveRecordExtensions
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
6
|
+
def self.included(base)
|
7
|
+
base.send(:extend, BinaryStorage::ActiveRecordExtensions::ClassMethods)
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
|
12
|
+
def register_binary(klass, binary_name, binary_column)
|
13
|
+
@@binary_columns ||= {}
|
14
|
+
@@binary_columns[klass] ||= {}
|
15
|
+
@@binary_columns[klass][binary_name] = binary_column
|
16
|
+
end
|
17
|
+
|
18
|
+
def binary_column(klass, binary_name)
|
19
|
+
if @@binary_columns && @@binary_columns[klass] && @@binary_columns[klass][binary_name]
|
20
|
+
@@binary_columns[klass][binary_name]
|
21
|
+
else
|
22
|
+
nil
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# Count existing references to a binary
|
27
|
+
def binary_reference_count(hash_string)
|
28
|
+
references = 0
|
29
|
+
if @@binary_columns
|
30
|
+
@@binary_columns.each do |klass, binaries|
|
31
|
+
binaries.each do |binary_name, binary_column|
|
32
|
+
references += klass.count(:all, :conditions => ["`#{binary_column} = ?`", hash_string])
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def binary_storage(binary_name, binary_column)
|
39
|
+
binary_name = binary_name.to_s
|
40
|
+
binary_column = binary_column.to_s
|
41
41
|
|
42
|
-
|
42
|
+
register_binary(self, binary_name, binary_column)
|
43
43
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
44
|
+
class_eval <<-end_eval
|
45
|
+
before_save do |binary_model|
|
46
|
+
binary_model.save_binary("#{binary_name}")
|
47
|
+
end
|
48
|
+
|
49
|
+
after_destroy do |model|
|
50
|
+
binary_model.destroy_binary("#{binary_name}")
|
51
|
+
end
|
52
52
|
|
53
|
-
|
54
|
-
|
55
|
-
|
53
|
+
def #{binary_name}
|
54
|
+
self.get_binary_data("#{binary_name}")
|
55
|
+
end
|
56
56
|
|
57
|
-
|
58
|
-
|
59
|
-
|
57
|
+
def #{binary_name}=(binary_data)
|
58
|
+
self.set_binary_data("#{binary_name}", binary_data)
|
59
|
+
end
|
60
60
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
61
|
+
def #{binary_name}?
|
62
|
+
self.has_binary_data?("#{binary_name}")
|
63
|
+
end
|
64
|
+
end_eval
|
65
65
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
66
|
+
send(:include, BinaryStorage::ActiveRecordExtensions::InstanceMethods)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
module InstanceMethods
|
71
|
+
|
72
|
+
def binaries
|
73
|
+
@binaries ||= {}
|
74
|
+
end
|
75
75
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
76
|
+
def binary_column(binary_name)
|
77
|
+
if column_name = self.class.binary_column(self.class, binary_name)
|
78
|
+
column_name
|
79
|
+
else
|
80
|
+
raise "Binary column #{binary_name} not defined!"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def binary_hash_string(binary_name)
|
85
|
+
self.attributes[binary_column(binary_name)]
|
86
|
+
end
|
87
87
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
88
|
+
def save_binary(binary_name)
|
89
|
+
if binaries.has_key?(binary_name)
|
90
|
+
if binary = binaries[binary_name]
|
91
|
+
binary.save
|
92
|
+
self.attributes = self.attributes.merge({binary_column(binary_name).to_sym => binary.hash_string})
|
93
|
+
else
|
94
|
+
self.attributes = self.attributes.merge({binary_column(binary_name).to_sym => nil})
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
98
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
99
|
+
def destroy_binary(binary_name)
|
100
|
+
if binary = binaries[binary_name]
|
101
|
+
if hash_string = binary.hash_string
|
102
|
+
references = self.class.binary_reference_count
|
103
|
+
if references < 1
|
104
|
+
binary.delete!
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def get_binary_data(binary_name)
|
111
|
+
# Set directly?
|
112
|
+
if binary = binaries[binary_name]
|
113
|
+
binary.data
|
114
114
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
115
|
+
# Try loading
|
116
|
+
elsif hash_string = binary_hash_string(binary_name)
|
117
|
+
if binary = BinaryStorage::Blob.find(hash_string)
|
118
|
+
binaries[binary_name] = binary # Cache it
|
119
|
+
binary.data
|
120
|
+
else
|
121
|
+
nil
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def set_binary_data(binary_name, binary_data)
|
127
|
+
binary = (binary_data) ? BinaryStorage::Blob.new(binary_data) : nil
|
128
|
+
binaries[binary_name] = binary
|
129
|
+
end
|
130
|
+
|
131
|
+
def has_binary_data?(binary_name)
|
132
|
+
if binaries[binary_name]
|
133
|
+
true
|
134
|
+
else
|
135
|
+
hash_string = binary_hash_string(binary_name)
|
136
|
+
(hash_string && BinaryStorage::Blob.exists?(hash_string)) ? true : false
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
140
|
|
141
|
-
|
141
|
+
end
|
142
142
|
end
|
143
143
|
|
144
|
-
ActiveRecord::Base.send(:include, BinaryStorage::ActiveRecordExtensions)
|
144
|
+
ActiveRecord::Base.send(:include, BinaryStorage::ActiveRecordExtensions)
|
data/lib/binary_storage/blob.rb
CHANGED
@@ -1,105 +1,105 @@
|
|
1
1
|
module BinaryStorage
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
2
|
+
class Blob
|
3
|
+
|
4
|
+
class << self
|
5
|
+
def find(hash_string)
|
6
|
+
blob = self.new(:hash_string => hash_string)
|
7
|
+
return nil unless blob.exists?
|
8
|
+
blob.load
|
9
|
+
blob
|
10
|
+
end
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
def exists?(hash_string)
|
13
|
+
self.new(:hash_string => hash_string).exists?
|
14
|
+
end
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
16
|
+
def create(data)
|
17
|
+
blob = self.new(data)
|
18
|
+
blob.save
|
19
|
+
blob
|
20
|
+
end
|
21
|
+
|
22
|
+
def storage_dir(hash_string=nil)
|
23
|
+
root = BinaryStorage.storage_dir
|
24
|
+
(hash_string) ? File.join(root, hash_string.match(/^(..)/)[1]) : root
|
25
|
+
end
|
26
|
+
|
27
|
+
def storage_path(hash_string)
|
28
|
+
File.join(storage_dir(hash_string), hash_string.gsub(/^(..)/, ''))
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def initialize(*args)
|
33
|
+
args = *args
|
34
|
+
options = {
|
35
|
+
:hash_string => nil,
|
36
|
+
:data => nil
|
37
|
+
}
|
38
|
+
if args.kind_of?(Hash)
|
39
|
+
options.merge!(args)
|
40
|
+
else
|
41
|
+
options[:data] = args
|
42
|
+
end
|
43
|
+
@hash_string = options[:hash_string]
|
44
|
+
@data = options[:data]
|
45
|
+
end
|
46
|
+
|
47
|
+
def data
|
48
|
+
@data
|
49
|
+
end
|
50
|
+
|
51
|
+
def data=(new_data)
|
52
|
+
@hash_string = nil
|
53
|
+
@data = new_data
|
54
|
+
end
|
55
|
+
|
56
|
+
def hash_string
|
57
|
+
unless @hash_string
|
58
|
+
if @data
|
59
|
+
@hash_string = BinaryStorage.hexdigest(data)
|
60
|
+
else
|
61
|
+
raise "Binary has no data!"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
@hash_string
|
65
|
+
end
|
66
|
+
|
67
|
+
def storage_dir
|
68
|
+
BinaryStorage::Blob.storage_dir(hash_string)
|
69
|
+
end
|
70
70
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
71
|
+
def storage_path
|
72
|
+
BinaryStorage::Blob.storage_path(hash_string)
|
73
|
+
end
|
74
|
+
|
75
|
+
def exists?
|
76
|
+
File.exists?(storage_path)
|
77
|
+
end
|
78
|
+
|
79
|
+
def empty?
|
80
|
+
(hash_string && !exists?) || !data || data.empty?
|
81
|
+
end
|
82
|
+
|
83
|
+
def load
|
84
|
+
raise "File not found" unless exists?
|
85
|
+
@data = File.open(storage_path, "rb") {|io| io.read }
|
86
|
+
end
|
87
|
+
|
88
|
+
def delete
|
89
|
+
if exists?
|
90
|
+
FileUtils.rm(storage_path)
|
91
|
+
end
|
92
|
+
end
|
93
93
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
end
|
94
|
+
def save
|
95
|
+
unless exists?
|
96
|
+
FileUtils.mkdir_p(storage_dir)
|
97
|
+
file = File.new(storage_path, 'wb')
|
98
|
+
file.write(@data)
|
99
|
+
file.close
|
100
|
+
end
|
101
|
+
return true
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
end
|