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 path and is_in_dir(path,basepath) and fs.access(path,"r") then
97 for name in fs.dir(path) do
98 basename=fs.realpath(path..'/'..name)
100 fstat=fs.stat(fs.realpath(path..'/'..name))
101 if fstat["type"]=="reg" then
103 elseif fstat["type"]=="dir" then
109 rec={name=name,rights=fstat["modestr"],size=fstat["size"],type=ftype,date=os.date('%Y-%m-%d %H:%M:%S',fstat["mtime"])}
110 table.insert(files,rec)
114 result = { result = files }
116 result = { result = {} }
119 elseif action == "rename" then
121 item = fs.realpath(basepath..'/'..params["item"])
122 newItemPath = fs.realpath(basepath..'/'..fs.dirname(params["newItemPath"]))..'/'..fs.basename(params["newItemPath"])
124 if is_in_dir(item,basepath) and is_in_dir(newItemPath,basepath) and item ~= basepath and newItemPath ~= basepath then
125 result = { result = { success=fs.rename(item,newItemPath), error="" } }
127 result = { result = { success=false, error="Invalid path request" } }
130 elseif action == "move" then
132 items = params["items"]
133 newPath = fs.realpath(basepath..'/'..params["newPath"])
135 if is_in_dir(newPath,basepath) then
139 for key,item in pairs(items) do
141 item = fs.realpath(basepath..'/'..item)
142 basename = fs.basename(item)
144 if is_in_dir(item,basepath) and item ~= basepath then
145 result = fs.move(item,newPath.."/"..basename)
156 result = { result = { success=result, error="" } }
160 result = { result = { success=false, error="Invalid destination request" } }
164 elseif action == "copy" then
166 items = params["items"]
167 newPath = fs.realpath(basepath..'/'..params["newPath"])
168 singleFilename = params["singleFilename"]
169 if singleFilename then
170 singleFilename = fs.basename(singleFilename)
173 if is_in_dir(newPath,basepath) then
177 for key,item in pairs(items) do
179 item = fs.realpath(basepath..'/'..item)
180 if singleFilename then
181 basename = singleFilename
183 basename = fs.basename(item)
186 if is_in_dir(item,basepath) and item ~= basepath then
187 result = fs.copy(item,newPath.."/"..basename)
198 result = { result = { success=result, error="" } }
202 result = { result = { success=false, error="Invalid destination request" } }
206 elseif action == "remove" then
208 items = params["items"]
212 for key,item in pairs(items) do
214 item = fs.realpath(basepath..'/'..item)
216 if is_in_dir(item,basepath) and item ~= basepath then
228 result = { result = { success=result, error="" } }
230 elseif action == "getContent" then
232 item = fs.realpath(basepath..'/'..params["item"])
234 if is_in_dir(item,basepath) and item ~= basepath then
235 content = fs.readfile(item)
236 result = { result = content }
238 result = { result = { success=false, error="Invalid path request" } }
241 elseif action == "edit" then
243 item = fs.realpath(basepath..'/'..params["item"])
244 content = params["content"]
246 if is_in_dir(item,basepath) and item ~= basepath then
247 result = { result = { success=fs.writefile(item,content), error="" } }
249 result = { result = { success=false, error="Invalid path request" } }
252 elseif action == "createFolder" then
254 newPath = fs.realpath(basepath..'/'..fs.dirname(params["newPath"]))..'/'..fs.basename(params["newPath"])
256 if is_in_dir(newPath,basepath) and newPath ~= basepath then
257 result = { result = { success=fs.mkdir(newPath), error="" } }
259 result = { result = { success=false, error="Invalid path request" } }
262 elseif action == "changePermissions" then
264 items = params["items"]
265 if params["perms"] then
266 mode = params["perms"]
268 mode = params["permsCode"]
273 recursive = params["recursive"]
277 for key,item in pairs(items) do
279 item = fs.realpath(basepath..'/'..item)
281 if is_in_dir(item,basepath) and item ~= basepath then
282 result = chmod(item,mode,recursive)
293 result = { result = { success=result, error="" } }
297 result = { result = { success=false, error="No permission requested" } }
301 elseif action == "compress" then
303 items = params["items"]
304 destination = params["destination"]
305 compressedFilename = params["compressedFilename"]
307 newPath = fs.realpath(basepath..'/'..destination)..'/'..fs.basename(params["compressedFilename"])
312 for key,item in pairs(items) do
314 realitem = fs.realpath(basepath..'/'..item)
316 if is_in_dir(realitem,basepath) and realitem ~= basepath then
317 item = item:match("/(.*)")
318 files = files.." "..item
328 command = "cd "..basepath.."; zip -r "..newPath..files
333 result = { result = { success=result, error="" } }
335 elseif action == "extract" then
337 item = fs.realpath(basepath..'/'..params["item"])
338 destination = params["destination"]
339 folderName = params["folderName"]
341 newPath = fs.realpath(basepath..'/'..destination)..'/'..fs.basename(params["folderName"])
346 if is_in_dir(item,basepath) and is_in_dir(newPath,basepath) and item ~= basepath then
347 command = "unzip "..item.." -d "..newPath
349 result = { result = { success=true, error="" } }
351 result = { result = { success=false, error="Invalid path request" } }
357 result = { result = { success=false, error="Operation not impolemented yet" } }
361 print(json.encode(result))