-
Notifications
You must be signed in to change notification settings - Fork 1
/
postcards.rb
158 lines (126 loc) · 3.57 KB
/
postcards.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
require 'rubygems'
require 'sinatra'
require 'dm-core'
require 'dm-timestamps'
require 'dm-migrations'
require 'carrierwave/datamapper'
require 'carrierwave'
require 'carrierwave/processing/mini_magick'
#require 'mini_magick'
require 'bundler'
Bundler.require
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/development.db")
class MyUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage:fog
#storage :file
def filename
@name ||= "#{secure_token}.#{file.extension}" if original_filename.present?
end
protected
def secure_token
var = :"@#{mounted_as}_secure_token"
model.instance_variable_get(var) or model.instance_variable_set(var,SecureRandom.base64.tr("+/", "-_"))
end
def extension_white_list
%w(jpg jpeg gif png)
end
process :resize_to_limit =>[800,600]
version :thumb do
process :resize_to_fill => [300,200]
end
def cache_dir
"#{settings.root}/tmp/uploads"
end
end
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS', # required
:aws_access_key_id => 'AKIAIFPEBMII5R3LSUAA', # required
:aws_secret_access_key => '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', # required
:region => 'us-east-1' # optional, defaults to 'us-east-1'
}
config.fog_directory = 'viewthroughthewindow' # required
# config.fog_hos = 'https://assets.example.com' # optional, defaults to nil
# config.fog_public = false # optional, defaults to true
# config.fog_attributes = {'Cache-Control'=>'max-age=315576000'} # optional, defaults to {}
end
class Post
include DataMapper::Resource
property :post_id, Serial
property :city, String
property :country, String
property :thoughts, Text
property :created_at, DateTime
property :user_name, String
property :user_email, String
mount_uploader :image1, MyUploader
mount_uploader :image2, MyUploader
end
class Comment
include DataMapper::Resource
property :comment_id, Serial
property :post_id, Integer
property :text, Text
property :user_name, String
property :created_at, DateTime
end
DataMapper.auto_upgrade!
# Main page: view all posts
get '/' do
@posts = Post.all(:order => [ :post_id.desc ], :limit => 35)
erb :index
end
# Create Post
post '/post/create' do
post = Post.new(:thoughts => params[:thoughts])
post.save
post.city = params[:city]
post.country = params[:country]
post.user_name = params[:user_name]
post.user_email = params[:user_email]
post.image1 = params[:pic1]
post.image2 = params[:pic2]
success = post.save
if success
status 201
redirect '/'
else
status 412
redirect '/posts'
end
end
# Create Comment
post '/comment/create/:post_id' do
comment = Comment.new
comment.post_id = params[:post_id]
comment.user_name=params[:user_name]
comment.text = params[:comment_text]
comment.created_at = Time.now
if comment.save
status 201
redirect '/'
else
status 412
redirect '/'
end
end
#Permalink
get '/:post_id' do
@post=Post.get(params[:post_id])
erb :post
end
post '/:post_id' do
comment = Comment.new
comment.post_id = params[:post_id]
comment.user_name=params[:user_name]
comment.text = params[:comment_text]
comment.created_at = Time.now
if comment.save
status 201
redirect '/'
else
status 412
redirect '/'
end
end