This repository has been archived by the owner on Oct 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gifster.rb
131 lines (99 loc) · 2.53 KB
/
gifster.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
require 'sinatra'
require 'data_mapper'
require 'dm-sqlite-adapter'
require 'mini_magick'
require 'digest'
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/dev.db")
class Gif
include DataMapper::Resource
property :id, Serial
property :caption, Text
property :source, Text
property :file, String, :required => true
property :tags, Text
property :created_at, DateTime
def tag_list
@tags.split(',')
end
end
DataMapper.finalize.auto_upgrade!
class Submission
attr_reader :url, :filename, :image
def initialize(url)
@url = url
end
def duplicate?
Gif.count(:source => @url) > 0
end
def valid?
@image = MiniMagick::Image.open(@url)
@image.mime_type == "image/gif"
end
def save
@filename = "#{gen_name}.gif"
@image.write("public/catalog/#{@filename}") if valid?
end
def gen_name
md5_hash = Digest::MD5.new
md5_hash << @url
end
end
###########
# Routes
###########
get '/' do
@recent = Gif.all(:limit => 12, :order => [ :created_at.desc ])
erb :index
end
get '/submit' do
if params['update']
@gif = Gif.get(params['update'])
@submission = Submission.new(@gif.source)
else
@submission = Submission.new(params['url'])
return "This URL was already imported before." if @submission.duplicate?
return "Invalid image type." unless @submission.valid?
end
erb :submission
end
post '/create' do
submission = Submission.new(params['gif_url'])
gif = Gif.first_or_new({ :source => submission.url })
if params['gif_id']
gif.update(:caption => params['caption'], :tags => params['tags'])
else
return "This URL was already imported before." if submission.duplicate?
return "Invalid image type." unless submission.valid?
submission.save
gif.attributes = {
:caption => params['caption'],
:source => submission.url,
:tags => params['tags'],
:file => submission.filename,
:created_at => Time.now
}
gif.save
end
redirect to("/gif/#{gif.id}") if gif.saved?
"An error occurred while trying to save your submission: #{gif.errors.inspect}"
end
get '/gif/:id' do
@gif = Gif.get(params['id'])
erb :gif
end
get '/search' do
@search = params['search']
@results = []
@results = Gif.all(:tags.like => "%#{@search}%") if @search
erb :search
end
get '/gifs' do
@results = Gif.all(:order => [ :created_at.desc ])
erb :mygifs
end
get '/delete' do
gif = Gif.get(params['id'])
File.delete("public/catalog/#{gif.file}")
gif.destroy
redirect to("/")
end