3 local os = require "os"
4 local fs = require "nixio.fs"
5 local json = require "json"
6 local luafm = require("luafm")
8 print("Content-type: text/html; charset=utf-8")
9 print("Cache-control: no-cache")
10 print("Pragma: no-cache")
12 -- prepare the browser for content:
17 local http_headers = nixio.getenv()
18 local request=io.read("*all")
20 local params=json.decode(request)
21 local action=params["action"]
32 local compressedFilename
43 if action == "list" then
45 path = luafm.make_path(params["path"])
50 for name in fs.dir(path) do
51 basename=fs.realpath(path..'/'..name)
53 fstat=fs.stat(basename)
54 if fstat["type"]=="reg" then
56 elseif fstat["type"]=="dir" then
62 rec={name=name,rights=fstat["modestr"],size=fstat["size"],type=ftype,date=os.date('%Y-%m-%d %H:%M:%S',fstat["mtime"])}
63 table.insert(files,rec)
67 result = { result = files }
69 result = { result = {} }
72 elseif action == "rename" then
74 item = luafm.make_path(params["item"])
75 newItemPath = luafm.make_new_path(params["newItemPath"])
77 if item and newItemPath and item ~= luafm.basepath and newItemPath ~= luafm.basepath then
78 result = fs.rename(item,newItemPath)
80 result = { result = { success=true, error="" } }
82 result = { result = { success=false, error="Cannot rename requested file/directory" } }
85 result = { result = { success=false, error="Invalid path request" } }
88 elseif action == "move" then
90 items = params["items"]
91 newPath = luafm.make_dir_path(params["newPath"])
97 for key,item in pairs(items) do
99 item = luafm.make_path(item)
101 if item and item ~= luafm.basepath then
102 basename = fs.basename(item)
103 result = fs.move(item,newPath.."/"..basename)
115 result = { result = { success=true, error="" } }
117 result = { result = { success=false, error="Cannot move requested file/directory" } }
122 result = { result = { success=false, error="Invalid destination request" } }
126 elseif action == "copy" then
128 items = params["items"]
129 newPath = luafm.make_dir_path(params["newPath"])
133 singleFilename = params["singleFilename"]
134 if singleFilename then
135 singleFilename = fs.basename(singleFilename)
140 for key,item in pairs(items) do
142 item = luafm.make_path(item)
144 if item and item ~= luafm.basepath then
145 if singleFilename then
146 basename = singleFilename
148 basename = fs.basename(item)
150 result = fs.copy(item,newPath.."/"..basename)
162 result = { result = { success=true, error="" } }
164 result = { result = { success=false, error="Cannot copy requested file/directory" } }
169 result = { result = { success=false, error="Invalid destination request" } }
173 elseif action == "remove" then
175 items = params["items"]
179 for key,item in pairs(items) do
181 item = luafm.make_path(item)
183 if item and item ~= luafm.basepath then
184 result = luafm.rm(item)
196 result = { result = { success=true, error="" } }
198 result = { result = { success=false, error="Cannot remove requested file/directory" } }
201 elseif action == "getContent" then
203 item = luafm.make_path(params["item"])
205 if item and item ~= luafm.basepath then
206 content = fs.readfile(item)
207 result = { result = content }
209 result = { result = { success=false, error="Invalid path request" } }
212 elseif action == "edit" then
214 item = luafm.make_path(params["item"])
215 content = params["content"]
217 if item and item ~= luafm.basepath then
218 result = fs.writefile(item,content)
220 result = { result = { success=true, error="" } }
222 result = { result = { success=false, error="Cannot write requested file content" } }
225 result = { result = { success=false, error="Invalid path request" } }
228 elseif action == "createFolder" then
230 newPath = luafm.make_new_path(params["newPath"])
232 if newPath and newPath ~= luafm.basepath then
233 result = fs.mkdir(newPath)
235 result = { result = { success=true, error="" } }
237 result = { result = { success=false, error="Cannot create folder" } }
240 result = { result = { success=false, error="Invalid path request" } }
243 elseif action == "changePermissions" then
245 items = params["items"]
247 if params["perms"] then
248 mode = params["perms"]
250 mode = params["permsCode"]
255 recursive = params["recursive"]
259 for key,item in pairs(items) do
261 item = luafm.make_path(item)
263 if item and item ~= luafm.basepath then
264 result = luafm.chmod(item,mode,recursive)
276 result = { result = { success=true, error="" } }
278 result = { result = { success=false, error="Cannot change permissions" } }
283 result = { result = { success=false, error="No permission requested" } }
287 elseif action == "compress" then
289 items = params["items"]
290 destination = params["destination"]
291 compressedFilename = params["compressedFilename"]
293 newPath = luafm.make_new_path(destination..'/'..fs.basename(params["compressedFilename"]))
298 for key,item in pairs(items) do
300 realitem = luafm.make_path(item)
302 if realitem and realitem ~= luafm.basepath then
303 item = item:match("/(.*)")
304 files = files.." "..item
314 command = "cd "..luafm.basepath.."; zip -r "..newPath..files
315 result = os.execute(command)
318 result = { result = { success=true, error="" } }
320 result = { result = { success=false, error="Archiver returned error" } }
325 result = { result = { success=false, error="No files selected" } }
329 elseif action == "extract" then
331 item = luafm.make_path(params["item"])
332 destination = params["destination"]
333 folderName = params["folderName"]
335 newPath = luafm.make_new_path(destination..'/'..fs.basename(params["folderName"]))
340 if item and newPath and item ~= luafm.basepath then
341 command = "unzip "..item.." -d "..newPath
342 result = os.execute(command)
344 result = { result = { success=true, error="" } }
346 result = { result = { success=false, error="Archiver returned error" } }
349 result = { result = { success=false, error="Invalid path request" } }
355 result = { result = { success=false, error="Operation not impolemented yet" } }
359 print(json.encode(result))