forked from marfarma/Ruby-File-Manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
74 lines (62 loc) · 1.95 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
require 'rubygems'
require 'sinatra'
require "sinatra/config_file"
enable :sessions
config_file 'config.yml'
# Set utf-8 for outgoing
before do
headers "Content-Type" => "text/html; charset=utf-8"
end
# Helpers
helpers do
def site_title
'FileManager'
end
end
# Upload file
post '/upload' do
if params[:file]
filename = params[:file][:filename]
tempfile = params[:file][:tempfile]
target = settings.file_root + params[:dir] + filename # TODO: params[:dir] maybe insecure
File.open(target, 'wb') {|f| f.write tempfile.read }
end
redirect params[:dir]
end
# List Directory
get '/*?' do |dir|
# Build Path from URL parameters
path = '/' + dir.to_s.strip
path << '/' unless path[-1, 1] == '/'
@dir = path
# Path to the Parent of the directory
@parent = File.dirname(path)
# Generate breadcrumb style links from the path
@path = [] # Array to store the links
paths = path.split('/') # Array of path steps to iterate through
paths.each_with_index do |item, index|
if index == paths.length-1
@path[index] = item
else
@path[index] = "<a href=\"#{paths[0..index].join('/')}\">#{item}</a>" unless item.to_s.length == 0
end
end
@path = path == '/' ? '[root]' : '<a href="/">[root]</a>' + @path.join('/')
# Get a list of files and directories in the current directory
@directories = ""
@files = ""
Dir.foreach("#{settings.file_root + path}") do |x|
full_path = settings.file_root + path + '/' + x
if x != '.' && x != '..'
if( (x[0, 1] == '.' && settings.show_hidden == true) || x[0, 1] != '.' ) # if it starts with a dot (.) don't show it unless show_hidden = true
if File.directory?(full_path)
@directories << "\n<li class=\"dir\"><a href=\"#{path + x}\">#{x}</a></li>"
else
ext = File.extname(full_path)
@files << "\n<li class=\"#{ ext[1..ext.length-1]}\"><a href=\"#{path + x}\">#{x}</a></li>"
end
end
end
end
erb :index
end