summaryrefslogtreecommitdiffhomepage
path: root/modules/luci-base/luasrc/view/cbi/filebrowser.htm
blob: 806b1b5f409d1aad43ff471eed30a8eeb8c0d9a3 (plain)
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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Filebrowser - LuCI</title>
	<style type="text/css">
		#path, #listing {
			font-size: 85%;
		}

		ul {
			padding-left: 0;
			list-style-type: none;
		}

		li img {
			vertical-align: bottom;
			margin-right: 0.2em;
		}
	</style>

	<script type="text/javascript">
		function callback(path) {
			if( window.opener ) {
				var input = window.opener.document.getElementById(decodeURIComponent('<%=luci.http.urlencode(luci.http.formvalue('field'))%>'));
				if( input ) {
					input.value = decodeURIComponent(path);
					window.close();
				}
			}
		}
	</script>
</head>
<body>
	<%
		require("nixio.fs")
		require("nixio.util")
		require("luci.http")
		require("luci.dispatcher")

		local field   = luci.http.formvalue('field')
		local request = luci.dispatcher.context.args
		local path    = { '' }

		for i = 1, #request do
			if request[i] ~= '..' and #request[i] > 0 then
				path[#path+1] = request[i]
			end
		end

		local filestat = nixio.fs.stat(table.concat(path, '/'))
		local baseurl  = { 'admin', 'filebrowser' }

		if filestat and filestat.type == "reg" then
			path[#path] = ''
		elseif not (filestat and filestat.type == "dir") then
			path = { '', '' }
		else
			path[#path+1] = ''
		end

		filepath = table.concat(path, '/')

		local entries = {}
		local _, e
		for _, e in luci.util.vspairs(nixio.util.consume((nixio.fs.dir(filepath)))) do
			local p = filepath .. e
			local s = nixio.fs.stat(p)
			if s then
				entries[#entries+1] = {
					name = e,
					path = p,
					type = s.type
				}
			end
		end
	-%>
	<div id="path">
		Location:
		<% for i, dir in ipairs(path) do %>
			<% if i == 1 then %>
				<a href="<%=url(unpack(baseurl))%>?field=<%=luci.http.urlencode(field)%>">(root)</a>
			<% elseif next(path, i) then %>
				<% baseurl[#baseurl+1] = luci.http.urlencode(dir) %>
				/ <a href="<%=url(unpack(baseurl))%>?field=<%=luci.http.urlencode(field)%>"><%=pcdata(dir)%></a>
			<% else %>
				<% baseurl[#baseurl+1] = luci.http.urlencode(dir) %>
				/ <%=pcdata(dir)%>
			<% end %>
		<% end %>
	</div>

	<hr />

	<div id="listing">
		<ul>
			<% for _, e in ipairs(entries) do if e.type == 'dir' then -%>
				<li class="dir">
					<img src="<%=resource%>/cbi/folder.gif" alt="<%:Directory%>" />
					<a href="<%=url(unpack(baseurl))%>/<%=luci.http.urlencode(e.name)%>?field=<%=luci.http.urlencode(field)%>"><%=pcdata(e.name)%>/</a>
				</li>
			<% end end -%>

			<% for _, e in ipairs(entries) do if e.type ~= 'dir' then -%>
				<li class="file">
					<img src="<%=resource%>/cbi/file.gif" alt="<%:File%>" />
					<a href="#" onclick="callback('<%=luci.http.urlencode(e.path)%>')"><%=pcdata(e.name)%></a>
				</li>
			<% end end -%>
		</ul>
	</div>
</body>
</html>