-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.rb
113 lines (91 loc) · 2.79 KB
/
app.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
require "bundler/setup"
Bundler.require(:default)
require File.expand_path("../lib/watermark", __FILE__)
require File.expand_path("../lib/redis_keys", __FILE__)
require "sinatra/base"
require "sinatra/assetpack"
require "sinatra/support"
require "sinatra/redis"
require "compass"
require "compass-h5bp"
require "mustache/sinatra"
class App < Sinatra::Base
base = File.dirname(__FILE__)
set :root, base
configure do
uri = URI.parse(ENV["REDISTOGO_URL"])
Resque.redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
Resque.redis.namespace = "resque:example"
# This line errors out on Heroku.
# Works okay locally.
#set :redis, ENV["REDISTOGO_URL"]
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
end
register Sinatra::AssetPack
register Sinatra::CompassSupport
register Mustache::Sinatra
set :sass, Compass.sass_engine_options
set :sass, { :load_paths => sass[:load_paths] + [ "#{base}/app/css" ] }
assets do
serve '/js', from: 'app/js'
serve '/css', from: 'app/css'
serve '/img', from: 'app/img'
css :app_css, [ '/css/*.css' ]
js :app_js, [
'/js/*.js',
'/js/vendor/jquery-1.9.1.min.js',
]
js :app_js_modernizr, [ '/js/vendor/modernizr-2.6.2.min.js' ]
end
require "#{base}/app/helpers"
require "#{base}/app/views/layout"
set :mustache, {
:templates => "#{base}/app/templates",
:views => "#{base}/app/views",
:namespace => App
}
before do
@css = css :app_css
@js = js :app_js
@js_modernizr = js :app_js_modernizr
end
helpers do
end
# Function allows both get / post.
def self.get_or_post(path, opts={}, &block)
get(path, opts, &block)
post(path, opts, &block)
end
get "/" do
@local_uploads = REDIS.get(local_uploads_key)
@s3_originals = REDIS.get(s3_originals_key)
@s3_watermarked = REDIS.get(s3_watermarked_key)
@watermarked_urls = REDIS.lrange(watermarked_url_list, 0, 4)
@working = Resque.working
mustache :index
end
post '/upload' do
unless params['file'][:tempfile].nil?
tmpfile = params['file'][:tempfile]
name = params['file'][:filename]
REDIS.incr(local_uploads_key)
file_token = send_to_s3(tmpfile, name)
Resque.enqueue(Watermark, file_token.key)
end
end
def send_to_s3(tmpfile, name)
connection = Fog::Storage.new({
:provider => 'AWS',
:aws_access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:aws_secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
})
directory = connection.directories.get(ENV['AWS_S3_BUCKET_ORIGINALS'])
file_token = directory.files.create(
:key => name,
:body => File.open(tmpfile),
:public => true
)
REDIS.incr(s3_originals_key)
file_token
end
end