cloud_magick 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/cloud_magick.gemspec +27 -0
- data/exe/cloud_magick +6 -0
- data/lib/cloud_magick.rb +7 -0
- data/lib/cloud_magick/builder.rb +323 -0
- data/lib/cloud_magick/version.rb +3 -0
- data/templates/index.js +94 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 26cb165095cd7622c3e76f89c30fbb6a0d6343b6
|
4
|
+
data.tar.gz: c515221ade5d1e2d6b2f5f869939d9dd49bb778a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f6125ebe9ad6f5f739da6c509c16ed65c6d4e8894e942eba82b84f4c34701d6f6cc9cd2f1b549a1074961a3b64f3db05bdb813104088962190c42045b113a893
|
7
|
+
data.tar.gz: d37081454628cca3b939ff879a0e4a24183c3cc9535236fb3d8f48fa1eea14d737031d5bdfc0ab2be3cf119c30c1ebee6014ef7849939aa53501db964ff850cc
|
data/.gitignore
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.3.1
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 pataiji
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# CloudMagick
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cloud_magick`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'cloud_magick'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install cloud_magick
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cloud_magick.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'cloud_magick'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'cloud_magick/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'cloud_magick'
|
8
|
+
spec.version = CloudMagick::VERSION
|
9
|
+
spec.authors = ['pataiji']
|
10
|
+
spec.email = ['pataiji@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'Build image resizing server on AWS'
|
13
|
+
spec.description = 'Build image resizing server on AWS'
|
14
|
+
spec.homepage = "https://github.com/pataiji/cloud_magick"
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = 'exe'
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_dependency 'aws-sdk-core', '~> 2.6'
|
23
|
+
spec.add_dependency 'rubyzip', '>= 1.0.0'
|
24
|
+
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.12'
|
26
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
27
|
+
end
|
data/exe/cloud_magick
ADDED
data/lib/cloud_magick.rb
ADDED
@@ -0,0 +1,323 @@
|
|
1
|
+
require 'zip'
|
2
|
+
require 'securerandom'
|
3
|
+
require 'aws-sdk-core'
|
4
|
+
|
5
|
+
module CloudMagick
|
6
|
+
class Builder
|
7
|
+
DEFAULT_REGION = 'ap-northeast-1'.freeze
|
8
|
+
DEFAULT_STAGE = 'test'.freeze
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
create_s3_bucket
|
12
|
+
build_lambda
|
13
|
+
build_api_gateway
|
14
|
+
update_s3_bucket_redirection
|
15
|
+
puts endpoint
|
16
|
+
end
|
17
|
+
|
18
|
+
def build_lambda
|
19
|
+
lambda_client.get_function(function_name: app_name)
|
20
|
+
rescue Aws::Lambda::Errors::ResourceNotFoundException
|
21
|
+
role = build_lambda_role
|
22
|
+
sleep 15 # FIXME
|
23
|
+
lambda_client.create_function(
|
24
|
+
function_name: app_name,
|
25
|
+
runtime: 'nodejs4.3',
|
26
|
+
role: role.arn,
|
27
|
+
handler: 'index.handler',
|
28
|
+
code: { zip_file: File.read(build_zip) },
|
29
|
+
description: 'image resizing function',
|
30
|
+
timeout: 30,
|
31
|
+
memory_size: 1024,
|
32
|
+
publish: true,
|
33
|
+
)
|
34
|
+
end
|
35
|
+
|
36
|
+
def build_zip
|
37
|
+
folder = File.join(__dir__, '..', '..', 'templates')
|
38
|
+
input_filenames = ['index.js']
|
39
|
+
|
40
|
+
tmp_dir = File.join(__dir__, '..', '..', 'tmp')
|
41
|
+
Dir.mkdir(tmp_dir) unless Dir.exist?(tmp_dir)
|
42
|
+
filepath = File.join(tmp_dir, 'lambda.zip')
|
43
|
+
File.delete(filepath) if File.exist?(filepath)
|
44
|
+
Zip::File.open(filepath, Zip::File::CREATE) do |zipfile|
|
45
|
+
input_filenames.each do |filename|
|
46
|
+
zipfile.add(filename, File.join(folder, filename))
|
47
|
+
end
|
48
|
+
end
|
49
|
+
filepath
|
50
|
+
end
|
51
|
+
|
52
|
+
def create_s3_bucket
|
53
|
+
s3_client.create_bucket(
|
54
|
+
bucket: bucket_name,
|
55
|
+
create_bucket_configuration: {
|
56
|
+
location_constraint: region,
|
57
|
+
},
|
58
|
+
)
|
59
|
+
bucket_policy = <<-EOS
|
60
|
+
{
|
61
|
+
"Version": "2012-10-17",
|
62
|
+
"Id": "allow access processed data",
|
63
|
+
"Statement": [
|
64
|
+
{
|
65
|
+
"Sid": "allow access all",
|
66
|
+
"Effect": "Allow",
|
67
|
+
"Principal": "*",
|
68
|
+
"Action": "s3:GetObject",
|
69
|
+
"Resource": "arn:aws:s3:::#{bucket_name}/*"
|
70
|
+
}
|
71
|
+
]
|
72
|
+
}
|
73
|
+
EOS
|
74
|
+
s3_client.put_bucket_policy(
|
75
|
+
bucket: bucket_name,
|
76
|
+
policy: bucket_policy,
|
77
|
+
)
|
78
|
+
end
|
79
|
+
|
80
|
+
def bucket_name
|
81
|
+
@bucket_name ||= app_name.gsub('_', '-')
|
82
|
+
end
|
83
|
+
|
84
|
+
def update_s3_bucket_redirection
|
85
|
+
s3_client.put_bucket_website(
|
86
|
+
bucket: bucket_name,
|
87
|
+
website_configuration: {
|
88
|
+
index_document: {
|
89
|
+
suffix: 'index.html',
|
90
|
+
},
|
91
|
+
routing_rules: [
|
92
|
+
{
|
93
|
+
condition: {
|
94
|
+
http_error_code_returned_equals: '404',
|
95
|
+
},
|
96
|
+
redirect: {
|
97
|
+
host_name: api_gateway_domain,
|
98
|
+
http_redirect_code: '302',
|
99
|
+
replace_key_prefix_with: "#{stage}/",
|
100
|
+
},
|
101
|
+
},
|
102
|
+
],
|
103
|
+
},
|
104
|
+
)
|
105
|
+
end
|
106
|
+
|
107
|
+
def build_lambda_role
|
108
|
+
iam_client.get_role(role_name: "lambda-#{app_name}").role
|
109
|
+
rescue Aws::IAM::Errors::NoSuchEntity
|
110
|
+
role_policy_document = <<-EOS
|
111
|
+
{
|
112
|
+
"Version": "2012-10-17",
|
113
|
+
"Statement": [
|
114
|
+
{
|
115
|
+
"Effect": "Allow",
|
116
|
+
"Principal": {
|
117
|
+
"Service": "lambda.amazonaws.com"
|
118
|
+
},
|
119
|
+
"Action": "sts:AssumeRole"
|
120
|
+
}
|
121
|
+
]
|
122
|
+
}
|
123
|
+
EOS
|
124
|
+
role = iam_client.create_role(
|
125
|
+
role_name: "lambda-#{app_name}",
|
126
|
+
assume_role_policy_document: role_policy_document,
|
127
|
+
).role
|
128
|
+
policy_document = <<-EOS
|
129
|
+
{
|
130
|
+
"Version": "2012-10-17",
|
131
|
+
"Statement": [
|
132
|
+
{
|
133
|
+
"Effect": "Allow",
|
134
|
+
"Action": [
|
135
|
+
"s3:PutObject",
|
136
|
+
"s3:GetAccelerateConfiguration",
|
137
|
+
"s3:GetBucketAcl",
|
138
|
+
"s3:GetBucketCORS",
|
139
|
+
"s3:GetBucketLocation",
|
140
|
+
"s3:GetBucketLogging",
|
141
|
+
"s3:GetBucketNotification",
|
142
|
+
"s3:GetBucketPolicy",
|
143
|
+
"s3:GetBucketRequestPayment",
|
144
|
+
"s3:GetBucketTagging",
|
145
|
+
"s3:GetBucketVersioning",
|
146
|
+
"s3:GetBucketWebsite",
|
147
|
+
"s3:GetLifecycleConfiguration",
|
148
|
+
"s3:GetObject",
|
149
|
+
"s3:GetObjectAcl",
|
150
|
+
"s3:GetObjectTorrent",
|
151
|
+
"s3:GetObjectVersion",
|
152
|
+
"s3:GetObjectVersionAcl",
|
153
|
+
"s3:GetObjectVersionTorrent",
|
154
|
+
"s3:GetReplicationConfiguration",
|
155
|
+
"s3:ListAllMyBuckets",
|
156
|
+
"s3:ListBucket",
|
157
|
+
"s3:ListBucketMultipartUploads",
|
158
|
+
"s3:ListBucketVersions",
|
159
|
+
"s3:ListMultipartUploadParts"
|
160
|
+
],
|
161
|
+
"Resource": "arn:aws:s3:::#{bucket_name}/*"
|
162
|
+
},
|
163
|
+
{
|
164
|
+
"Effect": "Allow",
|
165
|
+
"Action": [
|
166
|
+
"logs:CreateLogGroup",
|
167
|
+
"logs:CreateLogStream",
|
168
|
+
"logs:PutLogEvents"
|
169
|
+
],
|
170
|
+
"Resource": "arn:aws:logs:*:*:*"
|
171
|
+
}
|
172
|
+
]
|
173
|
+
}
|
174
|
+
EOS
|
175
|
+
iam_client.put_role_policy(
|
176
|
+
role_name: role.role_name,
|
177
|
+
policy_name: "lambda-#{app_name}-policy",
|
178
|
+
policy_document: policy_document,
|
179
|
+
)
|
180
|
+
role
|
181
|
+
end
|
182
|
+
|
183
|
+
def build_api_gateway
|
184
|
+
@api = api_gateway_client.create_rest_api(
|
185
|
+
name: app_name,
|
186
|
+
)
|
187
|
+
root_resource = api_gateway_client.get_resources({
|
188
|
+
rest_api_id: @api.id,
|
189
|
+
limit: 1,
|
190
|
+
}).items.first
|
191
|
+
parameter_resource = api_gateway_client.create_resource(
|
192
|
+
rest_api_id: @api.id,
|
193
|
+
parent_id: root_resource.id,
|
194
|
+
path_part: '{parameter}',
|
195
|
+
)
|
196
|
+
filename_resource = api_gateway_client.create_resource(
|
197
|
+
rest_api_id: @api.id,
|
198
|
+
parent_id: parameter_resource.id,
|
199
|
+
path_part: '{filename}',
|
200
|
+
)
|
201
|
+
api_gateway_client.put_method(
|
202
|
+
rest_api_id: @api.id,
|
203
|
+
resource_id: filename_resource.id,
|
204
|
+
http_method: 'GET',
|
205
|
+
authorization_type: 'NONE',
|
206
|
+
)
|
207
|
+
json_template = <<-EOS
|
208
|
+
{
|
209
|
+
"bucket_name": "#{bucket_name}",
|
210
|
+
"parameter": "$input.params('parameter')",
|
211
|
+
"filename": "$input.params('filename')"
|
212
|
+
}
|
213
|
+
EOS
|
214
|
+
api_gateway_client.put_integration(
|
215
|
+
rest_api_id: @api.id,
|
216
|
+
resource_id: filename_resource.id,
|
217
|
+
http_method: 'GET',
|
218
|
+
type: 'AWS',
|
219
|
+
integration_http_method: 'POST',
|
220
|
+
uri: "arn:aws:apigateway:#{region}:lambda:path/2015-03-31/functions/arn:aws:lambda:#{region}:#{aws_account_id}:function:#{app_name}/invocations",
|
221
|
+
request_templates: {
|
222
|
+
'application/json' => json_template,
|
223
|
+
},
|
224
|
+
passthrough_behavior: 'WHEN_NO_TEMPLATES',
|
225
|
+
)
|
226
|
+
api_gateway_client.put_method_response(
|
227
|
+
rest_api_id: @api.id,
|
228
|
+
resource_id: filename_resource.id,
|
229
|
+
http_method: 'GET',
|
230
|
+
status_code: '302',
|
231
|
+
response_parameters: {
|
232
|
+
'method.response.header.Location' => true,
|
233
|
+
},
|
234
|
+
)
|
235
|
+
api_gateway_client.put_integration_response(
|
236
|
+
rest_api_id: @api.id,
|
237
|
+
resource_id: filename_resource.id,
|
238
|
+
http_method: 'GET',
|
239
|
+
status_code: '302',
|
240
|
+
response_parameters: {
|
241
|
+
'method.response.header.Location' => 'integration.response.body.location',
|
242
|
+
},
|
243
|
+
)
|
244
|
+
|
245
|
+
lambda_client.add_permission(
|
246
|
+
function_name: app_name,
|
247
|
+
statement_id: SecureRandom.uuid,
|
248
|
+
action: 'lambda:InvokeFunction',
|
249
|
+
principal: 'apigateway.amazonaws.com',
|
250
|
+
source_arn: "arn:aws:execute-api:#{region}:#{aws_account_id}:#{@api.id}/*/GET/{parameter}/{filename}",
|
251
|
+
)
|
252
|
+
|
253
|
+
api_gateway_client.create_deployment(
|
254
|
+
rest_api_id: @api.id,
|
255
|
+
stage_name: stage,
|
256
|
+
)
|
257
|
+
end
|
258
|
+
|
259
|
+
def endpoint
|
260
|
+
"http://#{bucket_name}.s3-website-#{region}.amazonaws.com/"
|
261
|
+
end
|
262
|
+
|
263
|
+
def api_gateway_domain
|
264
|
+
"#{@api.id}.execute-api.ap-northeast-1.amazonaws.com"
|
265
|
+
end
|
266
|
+
|
267
|
+
def aws_account_id
|
268
|
+
@aws_account_id ||= sts_client.get_caller_identity.account
|
269
|
+
end
|
270
|
+
|
271
|
+
def region
|
272
|
+
@region ||= DEFAULT_REGION
|
273
|
+
end
|
274
|
+
|
275
|
+
def lambda_client
|
276
|
+
@lambda_client ||= Aws::Lambda::Client.new(
|
277
|
+
region: region,
|
278
|
+
access_key_id: ENV['ACCESS_KEY_ID'],
|
279
|
+
secret_access_key: ENV['SECRET_ACCESS_KEY'],
|
280
|
+
)
|
281
|
+
end
|
282
|
+
|
283
|
+
def api_gateway_client
|
284
|
+
@api_gateway_client ||= Aws::APIGateway::Client.new(
|
285
|
+
region: region,
|
286
|
+
access_key_id: ENV['ACCESS_KEY_ID'],
|
287
|
+
secret_access_key: ENV['SECRET_ACCESS_KEY'],
|
288
|
+
)
|
289
|
+
end
|
290
|
+
|
291
|
+
def sts_client
|
292
|
+
@sts_client ||= Aws::STS::Client.new(
|
293
|
+
region: region,
|
294
|
+
access_key_id: ENV['ACCESS_KEY_ID'],
|
295
|
+
secret_access_key: ENV['SECRET_ACCESS_KEY'],
|
296
|
+
)
|
297
|
+
end
|
298
|
+
|
299
|
+
def iam_client
|
300
|
+
@iam_client ||= Aws::IAM::Client.new(
|
301
|
+
region: region,
|
302
|
+
access_key_id: ENV['ACCESS_KEY_ID'],
|
303
|
+
secret_access_key: ENV['SECRET_ACCESS_KEY'],
|
304
|
+
)
|
305
|
+
end
|
306
|
+
|
307
|
+
def s3_client
|
308
|
+
@s3_client ||= Aws::S3::Client.new(
|
309
|
+
region: region,
|
310
|
+
access_key_id: ENV['ACCESS_KEY_ID'],
|
311
|
+
secret_access_key: ENV['SECRET_ACCESS_KEY'],
|
312
|
+
)
|
313
|
+
end
|
314
|
+
|
315
|
+
def app_name
|
316
|
+
@app_name ||= ENV['APP_NAME']
|
317
|
+
end
|
318
|
+
|
319
|
+
def stage
|
320
|
+
@stage ||= DEFAULT_STAGE
|
321
|
+
end
|
322
|
+
end
|
323
|
+
end
|
data/templates/index.js
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
var AWS = require('aws-sdk');
|
2
|
+
var fs = require('fs');
|
3
|
+
var im = require('imagemagick');
|
4
|
+
var s3 = new AWS.S3();
|
5
|
+
|
6
|
+
const ORIGIN_DIR_NAME = 'origin';
|
7
|
+
const CONVERTED_TMPFILE_NAME = '/tmp/converted_tmpfile';
|
8
|
+
|
9
|
+
exports.handler = function (event, context, callback) {
|
10
|
+
const BUCKET = event.bucket_name;
|
11
|
+
|
12
|
+
var getParams = {
|
13
|
+
Bucket: BUCKET,
|
14
|
+
Key : ORIGIN_DIR_NAME + '/' + event.filename
|
15
|
+
};
|
16
|
+
|
17
|
+
var params = event.parameter.split('-');
|
18
|
+
var size = '';
|
19
|
+
var crop = '';
|
20
|
+
var gravity = '';
|
21
|
+
var match = '';
|
22
|
+
|
23
|
+
for (var i = 0; i < params.length; i++) {
|
24
|
+
if (!size) {
|
25
|
+
match = params[i].match(/^\d*%?x?\d*%?[\^!<>@]?$/i);
|
26
|
+
if (match) {
|
27
|
+
size = match[0];
|
28
|
+
continue;
|
29
|
+
}
|
30
|
+
}
|
31
|
+
if (!crop) {
|
32
|
+
match = params[i].match(/^crop(\d+x\d+\+\d+\+\d+)$/i);
|
33
|
+
if (match) {
|
34
|
+
crop = match[1];
|
35
|
+
continue;
|
36
|
+
}
|
37
|
+
}
|
38
|
+
if (!gravity) {
|
39
|
+
match = params[i].match(/^(NorthWest|North|NorthEast|West|Center|East|SouthWest|South|SouthEast)$/i);
|
40
|
+
if (match) {
|
41
|
+
gravity = match[0];
|
42
|
+
continue;
|
43
|
+
}
|
44
|
+
}
|
45
|
+
}
|
46
|
+
console.log(size);
|
47
|
+
console.log(crop);
|
48
|
+
console.log(gravity);
|
49
|
+
|
50
|
+
convertParams = [];
|
51
|
+
convertParams.push('-');
|
52
|
+
if (size) {
|
53
|
+
convertParams.push('-resize');
|
54
|
+
convertParams.push(size);
|
55
|
+
}
|
56
|
+
if (gravity) {
|
57
|
+
convertParams.push('-gravity');
|
58
|
+
convertParams.push(gravity);
|
59
|
+
}
|
60
|
+
if (crop) {
|
61
|
+
convertParams.push('-crop');
|
62
|
+
convertParams.push(crop);
|
63
|
+
convertParams.push("+repage");
|
64
|
+
}
|
65
|
+
convertParams.push(CONVERTED_TMPFILE_NAME);
|
66
|
+
|
67
|
+
s3.getObject(getParams, function (err, data) {
|
68
|
+
if (err) { callback(err, 's3 getObject'); }
|
69
|
+
|
70
|
+
var proc = im.convert(convertParams, function (err, stdout, stderr) {
|
71
|
+
if (err) { callback(err, 'im convert'); }
|
72
|
+
|
73
|
+
fs.readFile(CONVERTED_TMPFILE_NAME, function (err, converted_data) {
|
74
|
+
if (err) { callback(err, converted_data, 'converted tempfile read'); }
|
75
|
+
|
76
|
+
var key = event.parameter + '/' + event.filename;
|
77
|
+
s3.putObject({
|
78
|
+
Bucket: BUCKET,
|
79
|
+
Key : key,
|
80
|
+
Body : new Buffer(converted_data, 'binary'),
|
81
|
+
ContentType: data.ContentType
|
82
|
+
}, function (err, res) {
|
83
|
+
if (err) { callback(err, 's3 putObject'); }
|
84
|
+
|
85
|
+
callback(null, { location: 'http://' + BUCKET + '.s3-website-ap-northeast-1.amazonaws.com/' + key });
|
86
|
+
});
|
87
|
+
});
|
88
|
+
});
|
89
|
+
|
90
|
+
proc.stdin.setEncoding('binary');
|
91
|
+
proc.stdin.write(data.Body, 'binary');
|
92
|
+
proc.stdin.end();
|
93
|
+
});
|
94
|
+
};
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cloud_magick
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- pataiji
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: aws-sdk-core
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.6'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rubyzip
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.0.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.0.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.12'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.12'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
description: Build image resizing server on AWS
|
70
|
+
email:
|
71
|
+
- pataiji@gmail.com
|
72
|
+
executables:
|
73
|
+
- cloud_magick
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".ruby-version"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- cloud_magick.gemspec
|
86
|
+
- exe/cloud_magick
|
87
|
+
- lib/cloud_magick.rb
|
88
|
+
- lib/cloud_magick/builder.rb
|
89
|
+
- lib/cloud_magick/version.rb
|
90
|
+
- templates/index.js
|
91
|
+
homepage: https://github.com/pataiji/cloud_magick
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata: {}
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 2.5.1
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: Build image resizing server on AWS
|
115
|
+
test_files: []
|