-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
executable file
·62 lines (44 loc) · 1.13 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
require 'sinatra'
require 'haml'
require 'json'
helpers do
def get_entries(path)
Dir["#{path}/*"]
end
def query_path(path)
path.gsub(settings.file_root + '/', '')
end
def back_path(path)
size = path.split("/").size
result = (size >= 1 ? path.split("/").take(size - 1).join("/") : "")
end
def generate_content(url)
directories = []
files = []
result = []
result_hash = {}
full_path = "#{settings.file_root}/#{url}"
result_hash.store(:current, full_path)
path = query_path(full_path)
parent = back_path(path)
result_hash.store(:parent, parent)
get_entries(full_path).each do |ent|
if(File.directory?(ent))
directories << { path: query_path(ent), name: File.basename(ent) }
else
files << { name: File.basename(ent) }
end
end
result_hash.store(:dir, directories)
result_hash.store(:file, files)
result << result_hash
content_type :json
result.to_json
end
end
get '/entries/*' do |entry|
generate_content entry
end
get '/' do
haml :index
end