-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.rb
47 lines (36 loc) · 1023 Bytes
/
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
require 'sinatra'
require 'data_mapper'
require 'dm-paperclip'
APP_ROOT = File.expand_path(File.dirname(__FILE__))
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/development.db")
class Image
include DataMapper::Resource
include Paperclip::Resource
property :id, Serial
has_attached_file :file,
:url => "/files/:id.:extension",
:path => "#{APP_ROOT}/public/files/:id.:extension"
end
DataMapper.finalize
DataMapper.auto_upgrade!
def make_paperclip_mash(file_hash)
mash = Mash.new
mash['tempfile'] = file_hash[:tempfile]
mash['filename'] = file_hash[:filename]
mash['content_type'] = file_hash[:type]
mash['size'] = file_hash[:tempfile].size
mash
end
get '/' do
haml :upload
end
get '/print' do
Image.first
haml :pr
end
post '/upload' do
halt 409, "File seems to be emtpy" unless params[:file][:tempfile].size > 0
@resource = Image.new(:file => make_paperclip_mash(params[:file]))
@resource.save
"Complete!"
end