3 local os = require "os"
4 local fs = require "nixio.fs"
5 local json = require "json"
6 local uci = require("uci")
8 local function is_in_dir(file, dir)
12 return file:sub(1, #dir) == dir
16 local function rm(item)
17 local ftype = fs.stat(item,"type")
18 if ftype == "reg" then
19 return fs.remove(item)
20 elseif ftype == "dir" then
21 local dir = fs.dir(item)
23 if not rm(item..'/'..file) then
31 local function chmod(item,mode,recursive)
32 local result = fs.chmod(item,mode)
33 if result and recursive then
34 local dir = fs.dir(item)
37 local ftype = fs.stat(item..'/'..file,"type")
38 if ftype == "dir" then
39 result = chmod(item..'/'..file,mode,recursive)
40 elseif ftype == "reg" then
41 result = chmod(item..'/'..file,string.gsub(mode,"x","-"),false)
52 print("Content-type: text/html; charset=utf-8")
53 print("Cache-control: no-cache")
54 print("Pragma: no-cache")
56 -- prepare the browser for content:
61 local http_headers = nixio.getenv()
62 local request=io.read("*all")
64 local params=json.decode(request)
65 local action=params["action"]
67 local u_c = uci.cursor()
68 local basepath = u_c.get("filemanager","config","basedir")
79 local compressedFilename
90 if action == "list" then
92 path = fs.realpath(basepath..'/'..params["path"])
94 if is_in_dir(path,basepath) then
97 for name in fs.dir(path) do
98 fstat=fs.stat(fs.realpath(path..'/'..name))
99 if fstat["type"]=="reg" then
101 elseif fstat["type"]=="dir" then
107 rec={name=name,rights=fstat["modestr"],size=fstat["size"],type=ftype,date=os.date('%Y-%m-%d %H:%M:%S',fstat["mtime"])}
108 table.insert(files,rec)
111 result = { result = files }
113 result = { result = { success=false, error="Invalid path request" } }
116 elseif action == "rename" then
118 item = fs.realpath(basepath..'/'..params["item"])
119 newItemPath = fs.realpath(basepath..'/'..fs.dirname(params["newItemPath"]))..'/'..fs.basename(params["newItemPath"])
121 if is_in_dir(item,basepath) and is_in_dir(newItemPath,basepath) and item ~= basepath and newItemPath ~= basepath then
122 result = { result = { success=fs.rename(item,newItemPath), error="" } }
124 result = { result = { success=false, error="Invalid path request" } }
127 elseif action == "move" then
129 items = params["items"]
130 newPath = fs.realpath(basepath..'/'..params["newPath"])
132 if is_in_dir(newPath,basepath) then
136 for key,item in pairs(items) do
138 item = fs.realpath(basepath..'/'..item)
139 basename = fs.basename(item)
141 if is_in_dir(item,basepath) and item ~= basepath then
142 result = fs.move(item,newPath.."/"..basename)
153 result = { result = { success=result, error="" } }
157 result = { result = { success=false, error="Invalid destination request" } }
161 elseif action == "copy" then
163 items = params["items"]
164 newPath = fs.realpath(basepath..'/'..params["newPath"])
165 singleFilename = params["singleFilename"]
166 if singleFilename then
167 singleFilename = fs.basename(singleFilename)
170 if is_in_dir(newPath,basepath) then
174 for key,item in pairs(items) do
176 item = fs.realpath(basepath..'/'..item)
177 if singleFilename then
178 basename = singleFilename
180 basename = fs.basename(item)
183 if is_in_dir(item,basepath) and item ~= basepath then
184 result = fs.copy(item,newPath.."/"..basename)
195 result = { result = { success=result, error="" } }
199 result = { result = { success=false, error="Invalid destination request" } }
203 elseif action == "remove" then
205 items = params["items"]
209 for key,item in pairs(items) do
211 item = fs.realpath(basepath..'/'..item)
213 if is_in_dir(item,basepath) and item ~= basepath then
225 result = { result = { success=result, error="" } }
227 elseif action == "getContent" then
229 item = fs.realpath(basepath..'/'..params["item"])
231 if is_in_dir(item,basepath) and item ~= basepath then
232 content = fs.readfile(item)
233 result = { result = content }
235 result = { result = { success=false, error="Invalid path request" } }
238 elseif action == "edit" then
240 item = fs.realpath(basepath..'/'..params["item"])
241 content = params["content"]
243 if is_in_dir(item,basepath) and item ~= basepath then
244 result = { result = { success=fs.writefile(item,content), error="" } }
246 result = { result = { success=false, error="Invalid path request" } }
249 elseif action == "createFolder" then
251 newPath = fs.realpath(basepath..'/'..fs.dirname(params["newPath"]))..'/'..fs.basename(params["newPath"])
253 if is_in_dir(newPath,basepath) and newPath ~= basepath then
254 result = { result = { success=fs.mkdir(newPath), error="" } }
256 result = { result = { success=false, error="Invalid path request" } }
259 elseif action == "changePermissions" then
261 items = params["items"]
262 if params["perms"] then
263 mode = params["perms"]
265 mode = params["permsCode"]
270 recursive = params["recursive"]
274 for key,item in pairs(items) do
276 item = fs.realpath(basepath..'/'..item)
278 if is_in_dir(item,basepath) and item ~= basepath then
279 result = chmod(item,mode,recursive)
290 result = { result = { success=result, error="" } }
294 result = { result = { success=false, error="No permission requested" } }
298 elseif action == "compress" then
300 items = params["items"]
301 destination = params["destination"]
302 compressedFilename = params["compressedFilename"]
304 newPath = fs.realpath(basepath..'/'..destination)..'/'..fs.basename(params["compressedFilename"])
309 for key,item in pairs(items) do
311 realitem = fs.realpath(basepath..'/'..item)
313 if is_in_dir(realitem,basepath) and realitem ~= basepath then
314 item = item:match("/(.*)")
315 files = files.." "..item
325 command = "cd "..basepath.."; zip -r "..newPath..files
330 result = { result = { success=result, error="" } }
332 elseif action == "extract" then
334 item = fs.realpath(basepath..'/'..params["item"])
335 destination = params["destination"]
336 folderName = params["folderName"]
338 newPath = fs.realpath(basepath..'/'..destination)..'/'..fs.basename(params["folderName"])
343 if is_in_dir(item,basepath) and is_in_dir(newPath,basepath) and item ~= basepath then
344 command = "unzip "..item.." -d "..newPath
346 result = { result = { success=true, error="" } }
348 result = { result = { success=false, error="Invalid path request" } }
354 result = { result = { success=false, error="Operation not impolemented yet" } }
358 print(json.encode(result))