-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroom.rb
177 lines (140 loc) · 4.13 KB
/
room.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
172
173
174
175
176
177
require 'rubygems'
require 'sinatra'
require 'mongo'
require 'bson'
require 'mongo/gridfs/grid'
require 'open-uri'
require 'aviary_fx'
require 'form'
require 'photo'
connection = Mongo::Connection.new
db = connection.db("mydb")
grid = Mongo::Grid.new(db)
afx = AviaryFX::API.new("93aa546bf", "644814880")
$photos = {}
effects = afx.get_filters()
effects_hash = {}
effects.each do |e|
effects_hash[e.uid] = e
end
get '/' do
@title = "Feather Room"
# @footer = "Created by Vivek Bhagwat"
erb :index
end
get '/hi' do
"Hello, World!"
end
get '/upload/?' do
@title = "Feather Room - Upload Photos"
@body = '<h2>Upload Photos</h2>'
@body += '<h3>Enter a bunch of URLs of photos to "upload"</h3>'
@body += Form.new('', 'POST', [
{:content => '', :type => 'file', :multiple=>"true", :name=>'urls[]'}
], {:value => 'Upload!'}, true).to_html
# @footer = "Created by Vivek Bhagwat"
erb :index
end
post '/upload/?' do
urls = params[:urls]
# url_array = urls.split(/\s|\r/)
# @body = ''
# url_array.each {|url| @body += '<img src="' + url.to_s + '" /><br />'}
urls.each do |url|
pic = Photo.new(url[:tempfile], url[:filename])
pic.aviary_response = afx.upload(url[:tempfile].path)
pic.id = grid.put(pic.data, :filename=>pic.filename)
$photos[pic.id.to_s] = pic
end
redirect('/photos/edit')
end
get '/photos/edit/?' do
f = Form.new('/photos/edit', 'POST', [], {:value=>'EDIT!'})
$photos.each do |id, p|
f.content << { :content => p.to_html,
:type => 'checkbox', :name=> id.to_s
}
end
f.content << [nil, nil]
effects.each do |e|
f.content << {:content => '<br />' + e.label,
:type => 'checkbox', :name => e.uid}
end
@body = '<h2>Select which filters to apply to which photos</h2>' + f.to_html
erb :index
end
post '/photos/edit/?' do
selected_photos = []
updated_photos = []
selected_effects = []
params.keys.each do |param|
if BSON::ObjectId.legal?(param)
selected_photos << $photos[param]
# afx.upload()
else
selected_effects << param.to_s
end
end
backgroundcolor = "0xFFFFFFFF"
format = "png"
quality = "100"
scale = "1"
width = "0"
height = "0"
selected_photos.each do |pic|
filepath = pic.aviary_response[:url]
selected_effects.each do |effect_id|
rpc = AviaryFX::RenderParameterCollection.new({
:parameters => effects_hash[effect_id].parameters
})
p backgroundcolor
p format
p quality
p scale
puts '
'
p filepath #something is wrong with this response I'm getting.
puts '
'
p effect_id
p width
p height
p rpc
# rpc = AviaryFX::RenderParameterCollection.new_from_json(render_parameters = '{
# "parameters": [
# { "id" : "Text Top", "value" : "asdfasdfasdf"},
# { "id" : "Text Bottom", "value" : "OR JUST STUPID"}
# ]
# }')
# puts ''
# p rpc
filepath = 'http://images.aviary.com/imagesv5/aviary_button.png'
updated_photos << afx.render(backgroundcolor, format, quality, scale, filepath, effect_id, width, height, rpc)[:url]
end
end
(selected_photos + selected_effects).inspect + "<br />" + updated_photos.inspect
end
get '/photos/:id/?' do
grid.get(BSON::ObjectId.from_string(params[:id])).read
end
class Hash
def slice(*keys)
{}.tap{ |h| keys.each{ |k| h[k] = self[k] } }
end
end
ENV_COPY = %w[ REQUEST_METHOD HTTP_COOKIE rack.request.cookie_string
rack.session rack.session.options rack.input]
# Returns the response body after simulating a request to a particular URL
# Maintains the session of the current user.
# Pass custom headers if you want to set or change them, e.g.
#
# # Spoof a GET request, even if we happen to be inside a POST
# html = spoof_request "/partial/assignedto/#{@bug.id}", 'REQUEST_METHOD'=>'GET'
def spoof_request( uri, headers=nil )
new_env = env.slice(*ENV_COPY).merge({
"PATH_INFO" => uri.to_s,
"HTTP_REFERER" => env["REQUEST_URI"]
})
new_env.merge!(headers) if headers
call( new_env ).last.join
end