-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
64 lines (57 loc) · 1.69 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
get '/' do
erb :index
end
get '/about' do
erb :about
end
post '/get_folders.json' do
content_type :json
get_content(params[:node]).to_json
end
def get_content(path=".")
{:folders => get_dirs(path), :files => get_files(path)}
end
def get_dirs(path)
@path = File.join(File.expand_path(File.dirname(__FILE__)), path)
@dirs = []
if File.exists?(@path)
Dir.entries(@path).each do |dir|
if File.directory?(File.join(@path, dir)) && dir[0,1]!="."
el = {:id => File.join(path, dir),:text => dir, :leaf => false}
# если текущая папка не содержит вложенных папок, то присваиваем пустой массив:
# el[:folders] = [] if Dir.entries(File.join(@path, dir)).select {|entry| File.directory? File.join(File.join(@path, dir), entry) and !(entry =='.' || entry == '..') }.empty?
# другой способ:
# require "pathname"
# el[:folders] = [] Pathname.new('./').children.select { |c| c.directory? }.collect { |p| p.to_s }.empty?
@dirs << el
end
end
end
@dirs
end
def get_files(path)
@path = File.join(File.expand_path(File.dirname(__FILE__)), path)
@files = []
if File.exists?(@path)
Dir.entries(@path).each do |file|
if File.file?(File.join(@path, file))
@files << {
:filename => file,
:filesize => file_size(File.join(@path, file)),
:filedate => File.mtime(File.join(@path, file))
}
end
end
end
@files
end
def file_size(path)
size = File.size(path)
if (size > 1_000_000) then
"#{size / 1_000_000} MB"
elsif (size > 1_000) then
"#{size / 1_000} KB"
else
"#{size} B"
end
end