|
| 1 | +#!/usr/bin/lua |
| 2 | + |
| 3 | +local os = require "os" |
| 4 | +local fs = require "nixio.fs" |
| 5 | +local json = require "json" |
| 6 | +local luafm = require("luafm") |
| 7 | + |
| 8 | +print("Content-type: text/html; charset=utf-8") |
| 9 | +print("Cache-control: no-cache") |
| 10 | +print("Pragma: no-cache") |
| 11 | + |
| 12 | +-- prepare the browser for content: |
| 13 | +print("\r") |
| 14 | + |
| 15 | +local result |
| 16 | + |
| 17 | +local http_headers = nixio.getenv() |
| 18 | +local request=io.read("*all") |
| 19 | + |
| 20 | +local params=json.decode(request) |
| 21 | +local action=params["action"] |
| 22 | + |
| 23 | +local path |
| 24 | +local items |
| 25 | +local item |
| 26 | +local newItemPath |
| 27 | +local newPath |
| 28 | +local singleFilename |
| 29 | +local content |
| 30 | +local mode |
| 31 | +local destination |
| 32 | +local compressedFilename |
| 33 | +local recursive |
| 34 | +local folderName |
| 35 | + |
| 36 | +local files |
| 37 | +local fstat |
| 38 | +local ftype |
| 39 | +local basename |
| 40 | +local realitem |
| 41 | +local command |
| 42 | + |
| 43 | +if action == "list" then |
| 44 | + |
| 45 | + path = luafm.make_path(params["path"]) |
| 46 | + |
| 47 | + if path then |
| 48 | + files = {} |
| 49 | + local rec |
| 50 | + for name in fs.dir(path) do |
| 51 | + basename=fs.realpath(path..'/'..name) |
| 52 | + if basename then |
| 53 | + fstat=fs.stat(basename) |
| 54 | + if fstat["type"]=="reg" then |
| 55 | + ftype="file" |
| 56 | + elseif fstat["type"]=="dir" then |
| 57 | + ftype="dir" |
| 58 | + else |
| 59 | + ftype="" |
| 60 | + end |
| 61 | + if ftype 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) |
| 64 | + end |
| 65 | + end |
| 66 | + end |
| 67 | + result = { result = files } |
| 68 | + else |
| 69 | + result = { result = {} } |
| 70 | + end |
| 71 | + |
| 72 | +elseif action == "rename" then |
| 73 | + |
| 74 | + item = luafm.make_path(params["item"]) |
| 75 | + newItemPath = luafm.make_new_path(params["newItemPath"]) |
| 76 | + |
| 77 | + if item and newItemPath and item ~= luafm.basepath and newItemPath ~= luafm.basepath then |
| 78 | + result = fs.rename(item,newItemPath) |
| 79 | + if result then |
| 80 | + result = { result = { success=true, error="" } } |
| 81 | + else |
| 82 | + result = { result = { success=false, error="Cannot rename requested file/directory" } } |
| 83 | + end |
| 84 | + else |
| 85 | + result = { result = { success=false, error="Invalid path request" } } |
| 86 | + end |
| 87 | + |
| 88 | +elseif action == "move" then |
| 89 | + |
| 90 | + items = params["items"] |
| 91 | + newPath = luafm.make_dir_path(params["newPath"]) |
| 92 | + |
| 93 | + if newPath then |
| 94 | + |
| 95 | + result = true |
| 96 | + |
| 97 | + for key,item in pairs(items) do |
| 98 | + |
| 99 | + item = luafm.make_path(item) |
| 100 | + |
| 101 | + if item and item ~= luafm.basepath then |
| 102 | + basename = fs.basename(item) |
| 103 | + result = fs.move(item,newPath.."/"..basename) |
| 104 | + if not result then |
| 105 | + break |
| 106 | + end |
| 107 | + else |
| 108 | + result = false |
| 109 | + break |
| 110 | + end |
| 111 | + |
| 112 | + end |
| 113 | + |
| 114 | + if result then |
| 115 | + result = { result = { success=true, error="" } } |
| 116 | + else |
| 117 | + result = { result = { success=false, error="Cannot move requested file/directory" } } |
| 118 | + end |
| 119 | + |
| 120 | + else |
| 121 | + |
| 122 | + result = { result = { success=false, error="Invalid destination request" } } |
| 123 | + |
| 124 | + end |
| 125 | + |
| 126 | +elseif action == "copy" then |
| 127 | + |
| 128 | + items = params["items"] |
| 129 | + newPath = luafm.make_dir_path(params["newPath"]) |
| 130 | + |
| 131 | + if newPath then |
| 132 | + |
| 133 | + singleFilename = params["singleFilename"] |
| 134 | + if singleFilename then |
| 135 | + singleFilename = fs.basename(singleFilename) |
| 136 | + end |
| 137 | + |
| 138 | + result = true |
| 139 | + |
| 140 | + for key,item in pairs(items) do |
| 141 | + |
| 142 | + item = luafm.make_path(item) |
| 143 | + |
| 144 | + if item and item ~= luafm.basepath then |
| 145 | + if singleFilename then |
| 146 | + basename = singleFilename |
| 147 | + else |
| 148 | + basename = fs.basename(item) |
| 149 | + end |
| 150 | + result = fs.copy(item,newPath.."/"..basename) |
| 151 | + if not result then |
| 152 | + break |
| 153 | + end |
| 154 | + else |
| 155 | + result = false |
| 156 | + break |
| 157 | + end |
| 158 | + |
| 159 | + end |
| 160 | + |
| 161 | + if result then |
| 162 | + result = { result = { success=true, error="" } } |
| 163 | + else |
| 164 | + result = { result = { success=false, error="Cannot copy requested file/directory" } } |
| 165 | + end |
| 166 | + |
| 167 | + else |
| 168 | + |
| 169 | + result = { result = { success=false, error="Invalid destination request" } } |
| 170 | + |
| 171 | + end |
| 172 | + |
| 173 | +elseif action == "remove" then |
| 174 | + |
| 175 | + items = params["items"] |
| 176 | + |
| 177 | + result = true |
| 178 | + |
| 179 | + for key,item in pairs(items) do |
| 180 | + |
| 181 | + item = luafm.make_path(item) |
| 182 | + |
| 183 | + if item and item ~= luafm.basepath then |
| 184 | + result = luafm.rm(item) |
| 185 | + if not result then |
| 186 | + break |
| 187 | + end |
| 188 | + else |
| 189 | + result = false |
| 190 | + break |
| 191 | + end |
| 192 | + |
| 193 | + end |
| 194 | + |
| 195 | + if result then |
| 196 | + result = { result = { success=true, error="" } } |
| 197 | + else |
| 198 | + result = { result = { success=false, error="Cannot remove requested file/directory" } } |
| 199 | + end |
| 200 | + |
| 201 | +elseif action == "getContent" then |
| 202 | + |
| 203 | + item = luafm.make_path(params["item"]) |
| 204 | + |
| 205 | + if item and item ~= luafm.basepath then |
| 206 | + content = fs.readfile(item) |
| 207 | + result = { result = content } |
| 208 | + else |
| 209 | + result = { result = { success=false, error="Invalid path request" } } |
| 210 | + end |
| 211 | + |
| 212 | +elseif action == "edit" then |
| 213 | + |
| 214 | + item = luafm.make_path(params["item"]) |
| 215 | + content = params["content"] |
| 216 | + |
| 217 | + if item and item ~= luafm.basepath then |
| 218 | + result = fs.writefile(item,content) |
| 219 | + if result then |
| 220 | + result = { result = { success=true, error="" } } |
| 221 | + else |
| 222 | + result = { result = { success=false, error="Cannot write requested file content" } } |
| 223 | + end |
| 224 | + else |
| 225 | + result = { result = { success=false, error="Invalid path request" } } |
| 226 | + end |
| 227 | + |
| 228 | +elseif action == "createFolder" then |
| 229 | + |
| 230 | + newPath = luafm.make_new_path(params["newPath"]) |
| 231 | + |
| 232 | + if newPath and newPath ~= luafm.basepath then |
| 233 | + result = fs.mkdir(newPath) |
| 234 | + if result then |
| 235 | + result = { result = { success=true, error="" } } |
| 236 | + else |
| 237 | + result = { result = { success=false, error="Cannot create folder" } } |
| 238 | + end |
| 239 | + else |
| 240 | + result = { result = { success=false, error="Invalid path request" } } |
| 241 | + end |
| 242 | + |
| 243 | +elseif action == "changePermissions" then |
| 244 | + |
| 245 | + items = params["items"] |
| 246 | + |
| 247 | + if params["perms"] then |
| 248 | + mode = params["perms"] |
| 249 | + else |
| 250 | + mode = params["permsCode"] |
| 251 | + end |
| 252 | + |
| 253 | + if mode then |
| 254 | + |
| 255 | + recursive = params["recursive"] |
| 256 | + |
| 257 | + result = true |
| 258 | + |
| 259 | + for key,item in pairs(items) do |
| 260 | + |
| 261 | + item = luafm.make_path(item) |
| 262 | + |
| 263 | + if item and item ~= luafm.basepath then |
| 264 | + result = luafm.chmod(item,mode,recursive) |
| 265 | + if not result then |
| 266 | + break |
| 267 | + end |
| 268 | + else |
| 269 | + result = false |
| 270 | + break |
| 271 | + end |
| 272 | + |
| 273 | + end |
| 274 | + |
| 275 | + if result then |
| 276 | + result = { result = { success=true, error="" } } |
| 277 | + else |
| 278 | + result = { result = { success=false, error="Cannot change permissions" } } |
| 279 | + end |
| 280 | + |
| 281 | + else |
| 282 | + |
| 283 | + result = { result = { success=false, error="No permission requested" } } |
| 284 | + |
| 285 | + end |
| 286 | + |
| 287 | +elseif action == "compress" then |
| 288 | + |
| 289 | + items = params["items"] |
| 290 | + destination = params["destination"] |
| 291 | + compressedFilename = params["compressedFilename"] |
| 292 | + |
| 293 | + newPath = luafm.make_new_path(destination..'/'..fs.basename(params["compressedFilename"])) |
| 294 | + |
| 295 | + result = true |
| 296 | + files = "" |
| 297 | + |
| 298 | + for key,item in pairs(items) do |
| 299 | + |
| 300 | + realitem = luafm.make_path(item) |
| 301 | + |
| 302 | + if realitem and realitem ~= luafm.basepath then |
| 303 | + item = item:match("/(.*)") |
| 304 | + files = files.." "..item |
| 305 | + else |
| 306 | + result = false |
| 307 | + break |
| 308 | + end |
| 309 | + |
| 310 | + end |
| 311 | + |
| 312 | + if files then |
| 313 | + |
| 314 | + command = "cd "..luafm.basepath.."; zip -r "..newPath..files |
| 315 | + result = os.execute(command) |
| 316 | + |
| 317 | + if result then |
| 318 | + result = { result = { success=true, error="" } } |
| 319 | + else |
| 320 | + result = { result = { success=false, error="Archiver returned error" } } |
| 321 | + end |
| 322 | + |
| 323 | + else |
| 324 | + |
| 325 | + result = { result = { success=false, error="No files selected" } } |
| 326 | + |
| 327 | + end |
| 328 | + |
| 329 | +elseif action == "extract" then |
| 330 | + |
| 331 | + item = luafm.make_path(params["item"]) |
| 332 | + destination = params["destination"] |
| 333 | + folderName = params["folderName"] |
| 334 | + |
| 335 | + newPath = luafm.make_new_path(destination..'/'..fs.basename(params["folderName"])) |
| 336 | + |
| 337 | + result = true |
| 338 | + files = "" |
| 339 | + |
| 340 | + if item and newPath and item ~= luafm.basepath then |
| 341 | + command = "unzip "..item.." -d "..newPath |
| 342 | + result = os.execute(command) |
| 343 | + if result then |
| 344 | + result = { result = { success=true, error="" } } |
| 345 | + else |
| 346 | + result = { result = { success=false, error="Archiver returned error" } } |
| 347 | + end |
| 348 | + else |
| 349 | + result = { result = { success=false, error="Invalid path request" } } |
| 350 | + end |
| 351 | + |
| 352 | + |
| 353 | +else |
| 354 | + |
| 355 | + result = { result = { success=false, error="Operation not impolemented yet" } } |
| 356 | + |
| 357 | +end |
| 358 | + |
| 359 | +print(json.encode(result)) |
0 commit comments