Приведение в более приличный вид с выносом используемых базовых функций в отдельную...
[lua-filemanager.git] / www / cgi-bin / fs
index cee5f0f88942f7f038739d2e3d2092c08378983b..81282d7aa0c83b5a7ffc14f557dbf9c20858fba0 100755 (executable)
@@ -3,51 +3,7 @@
 local os = require "os"
 local fs = require "nixio.fs"
 local json = require "json"
-local uci = require("uci")
-
-local function is_in_dir(file, dir)
-  if file == dir then
-    return true
-  else   
-    return file:sub(1, #dir) == dir
-  end  
-end
-
-local function rm(item)
-  local ftype = fs.stat(item,"type")
-  if ftype == "reg" then
-    return fs.remove(item)
-  elseif ftype == "dir" then
-    local dir = fs.dir(item)
-    for file in dir do
-      if not rm(item..'/'..file) then
-        return false
-      end
-    end
-    return fs.rmdir(item)  
-  end
-end
-
-local function chmod(item,mode,recursive)
-  local result = fs.chmod(item,mode)
-  if result and recursive then
-    local dir = fs.dir(item)
-    if dir then
-      for file in dir do
-        local ftype = fs.stat(item..'/'..file,"type")
-        if ftype == "dir" then
-          result = chmod(item..'/'..file,mode,recursive)
-        elseif ftype == "reg" then
-          result = chmod(item..'/'..file,string.gsub(mode,"x","-"),false)  
-        end
-        if not result then
-          break
-        end  
-      end
-    end
-  end
-  return result
-end
+local luafm = require("luafm")
 
 print("Content-type: text/html; charset=utf-8")
 print("Cache-control: no-cache")
@@ -64,9 +20,6 @@ local request=io.read("*all")
 local params=json.decode(request)
 local action=params["action"]
 
-local u_c = uci.cursor()
-local basepath = u_c.get("filemanager","config","basedir")
-
 local path
 local items
 local item
@@ -89,15 +42,15 @@ local command
 
 if action == "list" then
 
-  path = fs.realpath(basepath..'/'..params["path"])
-  
-  if path and is_in_dir(path,basepath) and fs.access(path,"r") then
+  path = luafm.make_path(params["path"])
+
+  if path then
     files = {}
     local rec
     for name in fs.dir(path) do
       basename=fs.realpath(path..'/'..name)
       if basename then
-        fstat=fs.stat(fs.realpath(path..'/'..name))
+        fstat=fs.stat(basename)
         if fstat["type"]=="reg" then
           ftype="file"
         elseif fstat["type"]=="dir" then
@@ -118,11 +71,16 @@ if action == "list" then
 
 elseif action == "rename" then
 
-  item = fs.realpath(basepath..'/'..params["item"])
-  newItemPath = fs.realpath(basepath..'/'..fs.dirname(params["newItemPath"]))..'/'..fs.basename(params["newItemPath"])
+  item = luafm.make_path(params["item"])
+  newItemPath = luafm.make_new_path(params["newItemPath"])
 
-  if is_in_dir(item,basepath) and is_in_dir(newItemPath,basepath) and item ~= basepath and newItemPath ~= basepath then
-    result = { result = { success=fs.rename(item,newItemPath), error="" } }
+  if item and newItemPath and item ~= luafm.basepath and newItemPath ~= luafm.basepath then
+    result = fs.rename(item,newItemPath)
+    if result then
+      result = { result = { success=true, error="" } }
+    else
+      result = { result = { success=false, error="Cannot rename requested file/directory" } }
+    end  
   else
     result = { result = { success=false, error="Invalid path request" } }
   end
@@ -130,18 +88,18 @@ elseif action == "rename" then
 elseif action == "move" then
 
   items = params["items"]
-  newPath = fs.realpath(basepath..'/'..params["newPath"])
+  newPath = luafm.make_dir_path(params["newPath"])
   
-  if is_in_dir(newPath,basepath) then
+  if newPath then
 
     result = true
 
     for key,item in pairs(items) do
 
-      item = fs.realpath(basepath..'/'..item)
-      basename = fs.basename(item)
+      item = luafm.make_path(item)
 
-      if is_in_dir(item,basepath) and item ~= basepath then
+      if item and item ~= luafm.basepath then
+        basename = fs.basename(item)
         result = fs.move(item,newPath.."/"..basename)
         if not result then 
           break
@@ -153,7 +111,11 @@ elseif action == "move" then
       
     end  
 
-    result = { result = { success=result, error="" } }
+    if result then
+      result = { result = { success=true, error="" } }
+    else
+      result = { result = { success=false, error="Cannot move requested file/directory" } }
+    end  
 
   else
 
@@ -164,26 +126,27 @@ elseif action == "move" then
 elseif action == "copy" then
 
   items = params["items"]
-  newPath = fs.realpath(basepath..'/'..params["newPath"])
-  singleFilename = params["singleFilename"]
-  if singleFilename then
-    singleFilename = fs.basename(singleFilename)
-  end
+  newPath = luafm.make_dir_path(params["newPath"])
+
+  if newPath then
 
-  if is_in_dir(newPath,basepath) then
+    singleFilename = params["singleFilename"]
+    if singleFilename then
+      singleFilename = fs.basename(singleFilename)
+    end
 
     result = true
 
     for key,item in pairs(items) do
 
-      item = fs.realpath(basepath..'/'..item)
-      if singleFilename then
-        basename = singleFilename
-      else  
-        basename = fs.basename(item)
-      end
+      item = luafm.make_path(item)
 
-      if is_in_dir(item,basepath) and item ~= basepath then
+      if item and item ~= luafm.basepath then
+        if singleFilename then
+          basename = singleFilename
+        else  
+          basename = fs.basename(item)
+        end
         result = fs.copy(item,newPath.."/"..basename)
         if not result then 
           break
@@ -195,7 +158,11 @@ elseif action == "copy" then
 
     end  
 
-    result = { result = { success=result, error="" } }
+    if result then
+      result = { result = { success=true, error="" } }
+    else
+      result = { result = { success=false, error="Cannot copy requested file/directory" } }
+    end  
 
   else
 
@@ -211,10 +178,10 @@ elseif action == "remove" then
 
   for key,item in pairs(items) do
 
-    item = fs.realpath(basepath..'/'..item)
+    item = luafm.make_path(item)
 
-    if is_in_dir(item,basepath) and item ~= basepath then
-      result = rm(item)
+    if item and item ~= luafm.basepath then
+      result = luafm.rm(item)
       if not result then 
         break
       end
@@ -225,13 +192,17 @@ elseif action == "remove" then
 
   end
 
-  result = { result = { success=result, error="" } }
+  if result then
+    result = { result = { success=true, error="" } }
+  else
+    result = { result = { success=false, error="Cannot remove requested file/directory" } }
+  end  
 
 elseif action == "getContent" then
 
-  item = fs.realpath(basepath..'/'..params["item"])
+  item = luafm.make_path(params["item"])
   
-  if is_in_dir(item,basepath) and item ~= basepath then
+  if item and item ~= luafm.basepath then
     content = fs.readfile(item)
     result = { result = content }
   else
@@ -240,21 +211,31 @@ elseif action == "getContent" then
 
 elseif action == "edit" then
 
-  item = fs.realpath(basepath..'/'..params["item"])
+  item = luafm.make_path(params["item"])
   content = params["content"]
   
-  if is_in_dir(item,basepath) and item ~= basepath then
-    result = { result = { success=fs.writefile(item,content), error="" } }
+  if item and item ~= luafm.basepath then
+    result = fs.writefile(item,content)
+    if result then
+      result = { result = { success=true, error="" } }
+    else
+      result = { result = { success=false, error="Cannot write requested file content" } }
+    end  
   else
     result = { result = { success=false, error="Invalid path request" } }
   end
 
 elseif action == "createFolder" then
 
-  newPath = fs.realpath(basepath..'/'..fs.dirname(params["newPath"]))..'/'..fs.basename(params["newPath"])
+  newPath = luafm.make_new_path(params["newPath"])
   
-  if is_in_dir(newPath,basepath) and newPath ~= basepath then
-    result = { result = { success=fs.mkdir(newPath), error="" } }
+  if newPath and newPath ~= luafm.basepath then
+    result = fs.mkdir(newPath)
+    if result then
+      result = { result = { success=true, error="" } }
+    else
+      result = { result = { success=false, error="Cannot create folder" } }
+    end  
   else
     result = { result = { success=false, error="Invalid path request" } }
   end
@@ -262,6 +243,7 @@ elseif action == "createFolder" then
 elseif action == "changePermissions" then
 
   items = params["items"]
+
   if params["perms"] then
     mode = params["perms"]
   else
@@ -276,10 +258,10 @@ elseif action == "changePermissions" then
 
     for key,item in pairs(items) do
 
-      item = fs.realpath(basepath..'/'..item)
+      item = luafm.make_path(item)
 
-      if is_in_dir(item,basepath) and item ~= basepath then
-        result = chmod(item,mode,recursive)
+      if item and item ~= luafm.basepath then
+        result = luafm.chmod(item,mode,recursive)
         if not result then 
           break
         end
@@ -290,7 +272,11 @@ elseif action == "changePermissions" then
 
     end
 
-    result = { result = { success=result, error="" } }
+    if result then
+      result = { result = { success=true, error="" } }
+    else
+      result = { result = { success=false, error="Cannot change permissions" } }
+    end  
 
   else
   
@@ -304,16 +290,16 @@ elseif action == "compress" then
   destination = params["destination"]
   compressedFilename = params["compressedFilename"]
 
-  newPath = fs.realpath(basepath..'/'..destination)..'/'..fs.basename(params["compressedFilename"])
+  newPath = luafm.make_new_path(destination..'/'..fs.basename(params["compressedFilename"]))
 
   result = true
   files = ""
 
   for key,item in pairs(items) do
 
-    realitem = fs.realpath(basepath..'/'..item)
+    realitem = luafm.make_path(item)
 
-    if is_in_dir(realitem,basepath) and realitem ~= basepath then
+    if realitem and realitem ~= luafm.basepath then
       item = item:match("/(.*)")
       files = files.." "..item
     else
@@ -325,28 +311,40 @@ elseif action == "compress" then
   
   if files then
 
-    command = "cd "..basepath.."; zip -r "..newPath..files
-    os.execute(command)
+    command = "cd "..luafm.basepath.."; zip -r "..newPath..files
+    result = os.execute(command)
+
+    if result then
+      result = { result = { success=true, error="" } }
+    else
+      result = { result = { success=false, error="Archiver returned error" } }
+    end  
+
+  else
+
+      result = { result = { success=false, error="No files selected" } }
   
   end
 
-  result = { result = { success=result, error="" } }
-
 elseif action == "extract" then
 
-  item = fs.realpath(basepath..'/'..params["item"])
+  item = luafm.make_path(params["item"])
   destination = params["destination"]
   folderName = params["folderName"]
 
-  newPath = fs.realpath(basepath..'/'..destination)..'/'..fs.basename(params["folderName"])
+  newPath = luafm.make_new_path(destination..'/'..fs.basename(params["folderName"]))
 
   result = true
   files = ""
 
-  if is_in_dir(item,basepath) and is_in_dir(newPath,basepath) and item ~= basepath then
+  if item and newPath and item ~= luafm.basepath then
     command = "unzip "..item.." -d "..newPath
-    os.execute(command)
-    result = { result = { success=true, error="" } }
+    result = os.execute(command)
+    if result then
+      result = { result = { success=true, error="" } }
+    else
+      result = { result = { success=false, error="Archiver returned error" } }
+    end  
   else
     result = { result = { success=false, error="Invalid path request" } }
   end