-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.rb
129 lines (106 loc) · 2.91 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
require 'rubygems'
require 'bundler/setup'
require 'sinatra/base'
require 'mustache/sinatra'
require 'fileutils'
require 'pp'
require 'mongo'
require 'json'
require 'mini_exiftool'
class RouletteService < Sinatra::Base
register Mustache::Sinatra
require File.expand_path('../views/layout', __FILE__)
set :mustache, {
:views => File.expand_path("../views", __FILE__),
:templates => File.expand_path("../templates", __FILE__)
}
get '/' do
redirect '/roulette'
end
get '/upload' do
mustache :upload
end
get '/search/:key/:value' do | key, value |
queryjson = {
key => value
}
result = settings.imagecollection.find queryjson
@images = []
result.each do | image |
@images.push image['path']+"/"+image['filename']
end
mustache :result
end
get '/slideshow' do
queryjson = {
:wins => {"$gt" => 0}
}
result = settings.imagecollection.find queryjson
@images = []
result.each do | image |
@images.push "/images/"+image['file']
end
mustache :result
end
get '/roulette' do
result = settings.imagecollection.find
rnd1 = rand(result.count)
rnd2 = rand(result.count)
if rnd1 == rnd2
redirect '/roulette'
end
@image1 = result.to_a[rnd1]
result = settings.imagecollection.find
@image2 = result.to_a[rnd2]
mustache :roulette
end
get '/win/*' do
file = params[:splat].first
pp file
json = { :file => file }
image = settings.imagecollection.find_one json
if image
image["wins"] = image["wins"] + 1
settings.imagecollection.update({"_id" => image["_id"]}, image)
end
redirect '/roulette'
end
post '/upload' do
tempfile = params['file'][:tempfile]
filename = params['file'][:filename]
time = Time.new
path="./public/images/uploads/#{time.year}/#{time.month}/#{time.day}"
imagepath="/images/uploads/#{time.year}/#{time.month}/#{time.day}"
received_image = {
"file" => "uploads/#{time.year}/#{time.month}/#{time.day}/#{filename}",
"type" => "image"
}
image = settings.imagecollection.find_one received_image
if image
"Image exist"
else
if ! File.directory? path
FileUtils.mkdir_p(path)
end
FileUtils.cp tempfile.path, "#{path}/#{filename}"
received_image.merge! :wins => 0
received_image.merge! "exif" => get_exif("#{path}/#{filename}")
settings.imagecollection.insert(received_image)
@imageurl = "#{imagepath}/#{filename}"
mustache :upload
end
end
configure do
set :connection, Mongo::Connection.new
set :db, connection["roulette"]
set :imagecollection, db["images"]
end
def get_exif(filename)
exif = MiniExiftool.new "#{filename}"
exif_data = {}
exif.to_hash.each do |key,value|
exif_data.merge! key => value.to_s
end
exif_data
end
end