-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
96 lines (83 loc) · 1.96 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
# frozen_string_literal: true
require 'sinatra'
require 'rdiscount'
require 'ostruct'
require_relative 'quote'
require_relative 'tex'
require_relative 'codes'
require_relative 'posts'
require_relative 'files'
helpers Tex
helpers Codes
helpers Posts
helpers Files
MIME_TYPES = {
'.pdf' => 'application/pdf',
'.png' => 'image/png',
'.jpg' => 'image/jpeg',
'.svg' => 'image/svg+xml'
}.freeze
def mime(file_name)
MIME_TYPES[File.extname(file_name.downcase)] || 'text/plain'
end
get '/' do
erb :index
end
get '/favicon.ico' do
send_file File.join('public', 'pictures', 'favicon.ico'), type: 'image/ico'
end
get '/:post/?' do
# TeX setup
@tex = OpenStruct.new(fig_num: 0, bibs: [])
post = params[:post].downcase
begin
erb :index, {
views: "views/#{post}",
layout: :"../layout",
locals: { post: post }
}
rescue LoadError, Errno::ENOENT => e
puts e.message
raise Sinatra::NotFound
end
end
get '/public/code/:post/:file' do
post = params[:post].downcase
file = params[:file]
begin
send_file File.join('public', 'code', post, file), type: mime(file)
rescue LoadError, Errno::ENOENT => e
puts e.message
raise Sinatra::NotFound
end
end
get '/:post/:file' do
post = params[:post].downcase
file = params[:file]
begin
send_file File.join('views', post, file), type: mime(file)
rescue LoadError, Errno::ENOENT => e
puts e.message
raise Sinatra::NotFound
end
end
get '/public/pictures/:post/:file' do
post = params[:post].downcase
file = params[:file]
begin
send_file File.join('public', 'pictures', post, file), type: mime(file)
rescue LoadError, Errno::ENOENT => e
puts e.message
raise Sinatra::NotFound
end
end
get '/public/files/:post/:file' do
post = params[:post].downcase
file = params[:file]
begin
send_file File.join('public', 'files', post, file), type: mime(file)
rescue LoadError, Errno::ENOENT => e
puts e.message
raise Sinatra::NotFound
end
end