Forráskód Böngészése

add file maint function

moononournation 7 éve
szülő
commit
dcf674d33f
13 módosított fájl, 257 hozzáadás és 136 törlés
  1. 2 2
      bin/config.lua
  2. BIN
      bin/css.js.gz
  3. 27 4
      bin/file-api.lua
  4. 16 0
      bin/hello_world.lua
  5. 1 0
      bin/httpserver-static.lua
  6. 1 1
      bin/httpserver.lua
  7. BIN
      bin/index.css.gz
  8. BIN
      bin/index.html.gz
  9. BIN
      bin/index.js.gz
  10. 18 0
      bin/init.lua
  11. 0 108
      index.css
  12. 123 1
      index.html
  13. 69 20
      index.js

+ 2 - 2
bin/config.lua

@@ -7,8 +7,8 @@ local conf = {}
 local auth = {}
 auth.enabled = true
 auth.realm = "nodemcu-httpserver" -- displayed in the login dialog users get
-auth.user = "user"
-auth.password = "password" -- PLEASE change this
+auth.user = "YourLogin"
+auth.password = "Y0urPassw0rd" -- PLEASE change this
 conf.auth = auth
 
 return conf

BIN
bin/css.js.gz


+ 27 - 4
bin/file-api.lua

@@ -34,23 +34,46 @@ return function (connection, req, args)
          end
          file.close()
       elseif rd['action'] == 'save' then
-         print('save file: '..rd['filename'])
+         --print('save file: '..rd['filename'])
          local data = rd['data']
          file.open(rd['filename'], 'w+')
          file.write(data)
          file.close()
          connection:send('initial write: ' .. string.len(data))
       elseif rd['action'] == 'append' then
-         print('append file: '..rd['filename'])
+         --print('append file: '..rd['filename'])
          local data = rd['data']
          file.open(rd['filename'], 'a+')
          file.seek('end')
          file.write(data)
          file.close()
-         connection:send('append: ' .. string.len(data))
+         connection:send('Append: '..string.len(data))
       elseif rd['action'] == 'compile' then
-         print('compile file: '..rd['filename'])
+         --print('compile file: '..rd['filename'])
          node.compile(rd['filename'])
+         local compiledfile = string.sub(rd['filename'], 1, -5)..'.lc'
+         connection:send('Compiled file: <a href="'..compiledfile..'" target="_blank">'..compiledfile..'</a>')
+      elseif rd['action'] == 'new' then
+         --print('create new file')
+         local i = 1
+         local f = 'new'..i..'.txt'
+         -- find a new file name
+         while file.open(f, 'r') do
+            file.close()
+            i = i + 1
+            f = 'new'..i..'.txt'
+         end
+         file.open(f, 'w+')
+         file.close()
+         connection:send('Created file: '..f)
+      elseif rd['action'] == 'rename' then
+         --print('rename file from "'..rd['filename']..'" to "'..rd['newfilename']..'"')
+         file.rename(rd['filename'], rd['newfilename'])
+         connection:send('Renamed file from "'..rd['filename']..'" to "'..rd['newfilename']..'"')
+      elseif rd['action'] == 'delete' then
+         --print('deleted file: '..rd['filename'])
+         file.remove(rd['filename'])
+         connection:send('Deleted file: '..rd['filename'])
       end
    end
    collectgarbage()

+ 16 - 0
bin/hello_world.lua

@@ -0,0 +1,16 @@
+return function (connection, req, args)
+   dofile('httpserver-header.lc')(connection, 200, 'html')
+
+   connection:send([===[
+<!DOCTYPE html>
+<html lang="en">
+<head>
+<meta charset="utf-8">
+<title>Hello World!</title>
+</head>
+<body>
+<h1>Hello World!</h1>
+</body>
+</html>
+]===])
+end

+ 1 - 0
bin/httpserver-static.lua

@@ -6,6 +6,7 @@ return function (connection, req, args)
    --print("Begin sending:", args.file)
    --print("node.heap(): ", node.heap())
    if args.isGzipped and req.isCheckModifiedRequest() then
+      -- todo: really check if file updated
       dofile("httpserver-header.lc")(connection, 304, args.ext, args.isGzipped)
    else
       dofile("httpserver-header.lc")(connection, 200, args.ext, args.isGzipped)

+ 1 - 1
bin/httpserver.lua

@@ -102,7 +102,7 @@ return function (port)
 
             -- parse payload and decide what to serve.
             local req = dofile("httpserver-request.lc")(payload)
-            --print(req.method .. ": " .. req.request)
+            print(req.method .. ": " .. req.request)
             if conf.auth.enabled then
                auth = dofile("httpserver-basicauth.lc")
                user = auth.authenticate(payload) -- authenticate returns nil on failed auth

BIN
bin/index.css.gz


BIN
bin/index.html.gz


BIN
bin/index.js.gz


+ 18 - 0
bin/init.lua

@@ -74,6 +74,23 @@ for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end
 
 compileAndRemoveIfNeeded = nil
 serverFiles = nil
+i = nil
+f = nil
+collectgarbage()
+
+-- pre compile other lua files
+local l, f, s
+l = file.list();
+for f, s in pairs(l) do
+  if ((string.sub(f, -4) == '.lua') and (f ~= 'config.lua') and (f ~= 'init.lua')) then
+    print('Compiling:', f)
+    node.compile(f)
+    collectgarbage()
+  end
+end
+l = nil
+f = nil
+s = nil
 collectgarbage()
 
 -- Connect to the WiFi access point.
@@ -104,4 +121,5 @@ end
 -- Uncomment to automatically start the server in port 80
 if (not not wifi.sta.getip()) or (not not wifi.ap.getip()) then
     dofile("httpserver.lc")(80)
+    collectgarbage()
 end

A különbségek nem kerülnek megjelenítésre, a fájl túl nagy
+ 0 - 108
index.css


A különbségek nem kerülnek megjelenítésre, a fájl túl nagy
+ 123 - 1
index.html


+ 69 - 20
index.js

@@ -57,13 +57,12 @@ function handleFileClick(item) {
 }
 
 function loadFilelist() {
-  setLocalStatus("<span class=\"icon icon-loading\"></span> Loading file list");
-
-  if (!xhr) xhr = new XMLHttpRequest();
+  xhr = new XMLHttpRequest();
   xhr.open("POST", "file-api.lc", true);
   xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   xhr.onreadystatechange = function () {
     if (isXhrSuccess(xhr)) {
+      //setRemoteStatus("");
       var filelistHtml = "";
       var json = JSON.parse(xhr.responseText);
       var files = new Array(json.length);
@@ -85,8 +84,11 @@ function loadFilelist() {
       }
 
       setLocalStatus("");
+    } else {
+      //setRemoteStatus(xhr.responseText);
     }
   };
+  setLocalStatus("<span class=\"icon icon-loading\"></span> Loading file list");
   xhr.send("action=list");
 }
 
@@ -96,51 +98,55 @@ function handleSaveCallback() {
 
     savingFileOffset += blockSize;
     if (savingFileOffset < savingText.length) {
-      setLocalStatus("<span class=\"icon icon-loading\"></span> Saving file: " + savingFilename + " " + savingFileOffset + "/" + savingText.length + " bytes");
-
       var params = "action=append&filename=" + savingFilename + "&data=" + encodeURIComponent(savingText.substring(savingFileOffset, savingFileOffset + blockSize));
-      if (!xhr) xhr = new XMLHttpRequest();
+      xhr = new XMLHttpRequest();
       xhr.open("POST", "file-api.lc", true);
       xhr.onreadystatechange = handleSaveCallback;
       xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
+      setLocalStatus("<span class=\"icon icon-loading\"></span> Saving file: " + savingFilename + " " + savingFileOffset + "/" + savingText.length + " bytes");
       xhr.send(params);
     } else {
-      if ((savingFilename.split(".").pop() == "lua") && (savingFilename != "config.lua") && (savingFilename != "config.lua")) {
-        setLocalStatus("<span class=\"icon icon-loading\"></span> Compiling file: " + savingFilename);
-
+      if ((savingFilename.split(".").pop() == "lua") && (savingFilename != "config.lua") && (savingFilename != "init.lua")) {
         params = "action=compile&filename=" + savingFilename;
-        if (!xhr) xhr = new XMLHttpRequest();
+        xhr = new XMLHttpRequest();
         xhr.open("POST", "file-api.lc", true);
         xhr.onreadystatechange = handleCompileCallback;
         xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
+        setLocalStatus("<span class=\"icon icon-loading\"></span> Compiling file: " + savingFilename);
         xhr.send(params);
       } else {
         setLocalStatus("Saved file: " + savingFilename + " " + savingText.length + " bytes");
       }
     }
   } else {
-    setRemoteStatus("");
     setRemoteStatus(xhr.responseText);
   }
 }
 
 function handleCompileCallback() {
   if (isXhrSuccess(xhr)) {
-    setLocalStatus("Compiled file: " + savingFilename);
-    setRemoteStatus("");
+    setLocalStatus("");
   }
+  setRemoteStatus(xhr.responseText);
 }
 
+function handleFileCallback() {
+  setRemoteStatus(xhr.responseText);
+  if (isXhrSuccess(xhr)) {
+    loadFilelist();
+    setLocalStatus("");
+  }
+}
+
+
 function loadFile() {
   var filename = curFileItem.id;
-  setLocalStatus("Loading: " + filename);
-  setLocalStatus("<span class=\"icon icon-loading\"></span> Loading file: " + filename);
-
   var params = "action=load&filename=" + filename;
-  if (!xhr) xhr = new XMLHttpRequest();
+  xhr = new XMLHttpRequest();
   xhr.open("POST", "file-api.lc", true);
   xhr.onreadystatechange = function () {
     if (isXhrSuccess(xhr)) {
+      setRemoteStatus("");
       editor.setValue(xhr.responseText);
       var extension = filename.split(".").pop();
       switch (extension) {
@@ -164,13 +170,17 @@ function loadFile() {
       }
 
       setLocalStatus("");
+    } else {
+      //setRemoteStatus(xhr.responseText);
     }
   }
   xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
+  setLocalStatus("<span class=\"icon icon-loading\"></span> Loading file: " + filename);
   xhr.send(params);
 }
 
 function undo() {
+  setLocalStatus("undo");
   editor.undo();
 }
 
@@ -178,24 +188,60 @@ function save() {
   savingText = editor.getValue();
   savingFilename = curFileItem.id;
   savingFileOffset = 0;
-  setLocalStatus("<span class=\"icon icon-loading\"></span> Saving file: " + savingFilename + " " + savingFileOffset + "/" + savingText.length + " bytes");
-  setRemoteStatus("");
   var params = "action=save&filename=" + savingFilename + "&data=" + encodeURIComponent(savingText.substring(savingFileOffset, savingFileOffset + blockSize));
-  if (!xhr) xhr = new XMLHttpRequest();
+  xhr = new XMLHttpRequest();
   xhr.open("POST", "file-api.lc", true);
   xhr.onreadystatechange = handleSaveCallback;
   xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
+  setLocalStatus("<span class=\"icon icon-loading\"></span> Saving file: " + savingFilename + " " + savingFileOffset + "/" + savingText.length + " bytes");
   xhr.send(params);
 }
 
 function preview() {
   if (curFileItem) {
     var url = curFileItem.id;
+    setLocalStatus("Preview: "+url);
     var win = window.open(url, '_blank');
     win.focus();
   }
 }
 
+function new_file() {
+  xhr = new XMLHttpRequest();
+  xhr.open("POST", "file-api.lc", true);
+  xhr.onreadystatechange = handleFileCallback;
+  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
+  setLocalStatus("<span class=\"icon icon-loading\"></span> Creating new file");
+  xhr.send("action=new");
+}
+
+function rename_file() {
+  if (curFileItem) {
+    var filename = curFileItem.id;
+    var newfilename = prompt("Rename " + filename + " to:", filename);
+    if (newfilename != null) {
+      xhr = new XMLHttpRequest();
+      xhr.open("POST", "file-api.lc", true);
+      xhr.onreadystatechange = handleFileCallback;
+      xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
+      setLocalStatus("<span class=\"icon icon-loading\"></span> Renaming file from \""+filename+"\" to \""+newfilename+"\"");
+      xhr.send("action=rename&filename="+escape(filename)+"&newfilename="+escape(newfilename));
+    }
+  }
+}
+
+function delete_file() {
+  if (curFileItem) {
+    var filename = curFileItem.id;
+    xhr = new XMLHttpRequest();
+    xhr.open("POST", "file-api.lc", true);
+    xhr.onreadystatechange = handleFileCallback;
+    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
+    setLocalStatus("<span class=\"icon icon-loading\"></span> Deleting file: \""+filename+"\"");
+    xhr.send("action=delete&filename="+escape(filename));
+  }
+}
+
 function init() {
   editor = CodeMirror(document.getElementById("editor"), {
     styleActiveLine: true, // activeline
@@ -212,6 +258,9 @@ function init() {
   document.getElementById("undo").addEventListener("click", undo);
   document.getElementById("save").addEventListener("click", save);
   document.getElementById("preview").addEventListener("click", preview);
+  document.getElementById("new").addEventListener("click", new_file);
+  document.getElementById("rename").addEventListener("click", rename_file);
+  document.getElementById("delete").addEventListener("click", delete_file);
 }
 
 // load large size script in sequence to avoid NodeMCU overflow

Nem az összes módosított fájl került megjelenítésre, mert túl sok fájl változott