-
Notifications
You must be signed in to change notification settings - Fork 1
/
img_jitsu.rb
171 lines (156 loc) · 4.97 KB
/
img_jitsu.rb
1
2
3
4
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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
require 'rubygems'
require 'uri'
require 'open-uri'
require 'digest/md5'
require 'right_aws'
require 'yaml'
require 'ftools'
require 'RMagick'
class Media
# @@@@@@@@;) marge simpson.
@@s3_config = YAML.load_file('./config/amazon_s3.yml')
@@s3_connection = RightAws::S3.new(@@s3_config['access_key_id'], @@s3_config['secret_access_key'])
@@sqs_connection = RightAws::Sqs.new(@@s3_config['access_key_id'], @@s3_config['secret_access_key'])
@@bucket = @@s3_connection.bucket(@@s3_config['bucket'])
# @@in = @@sqs_connection.queue(@@s3_config['in_queue'])
TMP_DIR = './'
SIZES = {
:small => '140x140>',
:medium => '320x320>'
}
attr_accessor :url
def self.process_media
in_queue = @@sqs_connection.queue(@@s3_config['in_queue'])
while in_queue.size > 0
if i = YAML.load(in_queue.pop.to_s)
m = Media.new(i[:image][:url], i[:image][:id])
m.process
end
end
end
def initialize(url, media_id)
@url = url
@media_id = media_id
@children = []
@height = 0
@width = 0
@mime = ''
@size = 0
@md5 = Digest::MD5.hexdigest(@url)
end
def process
download && resize && sync_to_primary_storage && report
end
protected
def download
begin
file_extension = @url.split('.').last
if file_extension.size > 3
file_extension = ""
end
@file_name = Digest::MD5.hexdigest(@url + Time.now.to_s) + ".#{file_extension}"
open(@url) { |img| File.open(TMP_DIR + @file_name, "wb") { |f| f.puts img.read } }
rescue Timeout::Error, OpenURI::HTTPError, URI::InvalidURIError, Errno::ENOENT
return false
end
true
end
def resize
resize_small
resize_medium
end
def resize_small
if @file_name && img = Magick::Image::read(TMP_DIR + @file_name).first
old_fn = @file_name
tmp_fn = @file_name.split(".")[0]
@file_name = tmp_fn + ".#{img.format.gsub('JPEG', 'JPG')}"
File.mv(TMP_DIR + old_fn, TMP_DIR+ @file_name)
@mime = img.format
@height = img.rows
return false if img.rows < 80
return false if img.columns < 80
@width = img.columns
@size = 31337
small_fn = @file_name.split('.')[0] + '-small.' + 'jpg'
thumb_s = img.change_geometry!(SIZES[:small]) { |cols, rows, i|
i.resize!(cols, rows)
}
thumb_s.write TMP_DIR + small_fn
@children ||= []
@children << { :file_name => small_fn,
:size => 31337,
:mime => thumb_s.format,
:height => thumb_s.rows,
:width => thumb_s.columns }
return true
end
end
def resize_medium
if @file_name && img = Magick::Image::read(TMP_DIR + @file_name).first
old_fn = @file_name
tmp_fn = @file_name.split(".")[0]
@file_name = tmp_fn + ".#{img.format.gsub('JPEG', 'JPG')}"
File.mv(TMP_DIR + old_fn, TMP_DIR+ @file_name)
@mime = img.format
@height = img.rows
return false if img.rows < 80
return false if img.columns < 80
@width = img.columns
@size = 31337
medium_fn = @file_name.split('.')[0] + '-medium.' + 'jpg'
thumb_m = img.change_geometry!(SIZES[:medium]) { |cols, rows, i|
i.resize!(cols, rows)
}
thumb_m.write TMP_DIR + medium_fn
@children ||= []
@children << { :file_name => medium_fn,
:size => 31337,
:mime => thumb_m.format,
:height => thumb_m.rows,
:width => thumb_m.columns }
return true
end
end
def sync_to_primary_storage
# sync the original
open(TMP_DIR + @file_name, "r") do |file|
key = RightAws::S3::Key.create(@@bucket, @file_name)
key.put(file.read, 'public-read')
end
File.delete(TMP_DIR + @file_name)
# sync the children
@children.each do |child|
open(TMP_DIR + child[:file_name], "r") do |file|
key = RightAws::S3::Key.create(@@bucket, child[:file_name])
key.put(file.read, 'public-read')
end
File.delete(TMP_DIR + child[:file_name])
end
true
end
def final_check?
key = RightAws::S3::Key.create(@@bucket, @file_name)
key.exists?
end
def report
if final_check?
rpt = {'image' => {'id' => @media_id, 'bucket' => @@s3_config['bucket'], 'md5' => @md5, 'original' => {
'file_name' => @file_name, 'height' => @height, 'width' => @width, 'mime-type' => @mime, 'size' => @size
} } }
%w(small medium).each_with_index do |size, idx|
rpt['image'][size] = {
'file_name' => @children[idx][:file_name],
'height' => @children[idx][:height],
'width' => @children[idx][:width],
'size' => @children[idx][:size],
'mime' => @children[idx][:mime]
}
end
@out = @@sqs_connection.queue(@@s3_config['out_queue'])
@out.send_message(rpt.to_yaml)
#puts rpt.to_yaml
true
end
end
end
Media.process_media