This repository has been archived by the owner on Nov 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
126 lines (100 loc) · 3.18 KB
/
main.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
require 'sinatra'
require 'aws-sdk-s3'
require 'guid'
require 'json'
require 'mini_magick'
require 'pry'
set :bind, '0.0.0.0'
before do
content_type :json
headers 'Access-Control-Allow-Origin' => 'http://localhost:8080',
'Access-Control-Allow-Methods' => ['OPTIONS', 'POST'],
'Access-Control-Allow-Headers' => 'Content-Type'
end
access_key_id = ENV['AWS_ACCESS_KEY_ID']
secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
client = Aws::S3::Client.new(
region: 'eu-central-1',
access_key_id: access_key_id,
secret_access_key: secret_access_key
)
post '/book' do
file = JSON.parse(request.body.read)
extension = File.extname(file["name"])
g = Guid.new
g_filename = "#{g.to_s}#{extension}"
@filename = file["name"]
File.open("./public/#{@filename}", 'wb') do |f|
decoded_file = file["image"].split(',').last()
f.write(Base64.decode64(decoded_file))
end
image = MiniMagick::Image.open("./public/#{@filename}")
image.resize('200x200')
image.write("./resized/#{@filename}")
client.put_object({
acl: "public-read",
bucket: 'booker-cover',
key: "book/cover/#{g_filename}",
body: IO.read("./public/#{@filename}")
})
client.put_object({
acl: "public-read",
bucket: 'booker-cover',
key: "book/resized/#{g_filename}",
body: IO.read("./resized/#{@filename}")
})
{
object_url: "https://s3.eu-central-1.amazonaws.com/booker-cover/book/cover/#{g_filename}",
cover_url: "https://s3.eu-central-1.amazonaws.com/booker-cover/book/resized/#{g_filename}",
filename: @filename
}.to_json
end
post '/cover/movie' do
@filename = params[:file][:filename]
file = params[:file][:tempfile]
extension = File.extname(@filename)
g = Guid.new
g_filename = "#{g.to_s}#{extension}"
File.open("./public/#{@filename}", 'wb') do |f|
f.write(file.read)
end
image = MiniMagick::Image.open("./public/#{@filename}")
image.resize('200x200')
image.write("./resized/#{@filename}")
# obj = client.put_object({
# acl: "public-read",
# bucket: 'cmsrental',
# key: "movies/covers/#{g_filename}",
# body: IO.read("./public/#{@filename}")
# })
# client.put_object({
# acl: "public-read",
# bucket: 'cmsrental',
# key: "movies/thumbnail/#{g_filename}",
# body: IO.read("./resized/#{@filename}")
# })
# {
# object_url: "https://s3.eu-central-1.amazonaws.com/cmsrental/movies/covers/#{g_filename}",
# cover_url: "https://s3.eu-central-1.amazonaws.com/cmsrental/movies/thumbnail/#{g_filename}"
# }.to_json
end
post '/user/avatar' do
# @filename = params[:file][:filename]
# file = params[:file][:tempfile]
# extension = File.extname(@filename)
# g = Guid.new
# g_filename = "#{g.to_s}#{extension}"
# File.open("./public/#{@filename}", 'wb') do |f|
# f.write(file.read)
# end
# obj = client.put_object({
# acl: "public-read",
# bucket: 'cmsrental-avatars',
# key: g_filename,
# body: IO.read("./public/#{@filename}")
# })
# {
# object_url: "https://s3.eu-central-1.amazonaws.com/cmsrental-avatars/#{g_filename}",
# filename: @filename
# }.to_json
end