{"id":1224,"date":"2024-06-08T17:21:33","date_gmt":"2024-06-08T09:21:33","guid":{"rendered":"http:\/\/www.luwl.net\/?p=1224"},"modified":"2024-06-08T17:21:33","modified_gmt":"2024-06-08T09:21:33","slug":"nodemcu-%e7%94%a8lfs%e5%a2%9e%e5%8a%a0%e5%86%85%e5%ad%98%e7%a9%ba%e9%97%b4","status":"publish","type":"post","link":"http:\/\/www.luwl.net\/?p=1224","title":{"rendered":"NodeMCU \u7528LFS\u589e\u52a0\u5185\u5b58\u7a7a\u95f4"},"content":{"rendered":"\n<p>http:\/\/192.168.99.10\/test\/ \u5185\u7f51\u670d\u52a1\u5668\u4e0a\uff0c\u6709\u4e2a\u670d\u52a1\uff0c\u53ef\u4ee5\u5c06lua\u4ee3\u7801\u538b\u7f29\u540e\u751f\u6210zip\u6587\u4ef6\u4e0a\u4f20\u751f\u6210lfs.img\u6587\u4ef6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\r\nob_start(null, 0, PHP_OUTPUT_HANDLER_STDFLAGS);\r\nprint_r($_FILES); \/\/ Debug\r\n$dbgErrLog = \"web-luac-cross.log\";\r\n$zipFile = $_FILES&#91;'name']&#91;'tmp_name'];\r\n$upLoadName = $_FILES&#91;'name']&#91;'name'];\r\n$zipName = basename($upLoadName);\r\n$entries = &#91;];\r\n\/*\r\n* Clean up and exit on error\r\n*\/\r\nfunction abort($error) {\r\n  global $zipFile, $zip, $entry, $tempDir;\r\n  if (is_uploaded_file($zipFile)) unlink($zipFile);\r\n  if (is_resource($zip)) zip_close($zip);\r\n  if (is_resource($entry)) zip_entry_close($entry);\r\n  $dbgOP = ob_get_contents();\r\n  file_put_contents($dbgErrLog, $dbgOP);\r\n  ob_end_clean();\r\n  $resp = htmlentities($error, ENT_QUOTES | ENT_IGNORE, \"UTF-8\");\r\n  $resp = &lt;&lt;&lt;EOD\r\n&lt;html xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\" xml:lang=\"en\" lang=\"en\">\r\n&lt;head>&lt;title>Luac.cross compile error&lt;\/title>&lt;\/head>\r\n&lt;body>&lt;pre>$resp&lt;\/pre>&lt;\/body>&lt;\/html>\r\nEOD;\r\n  http_response_code(404);\r\n  header('Cache-Control : no-cache, no-store, must-revalidate, max-age=0');\r\n  header('Content-type : text\/html;charset=UTF-8');\r\n  header('Content-Length: ' . strlen($resp));\r\n  echo $resp;\r\n  exit();\r\n}\r\n\/\/ Open uploaded ZIPfile, aborting on error\r\nif (!preg_match('\/^(&#91;&#91;:alpha:]_]\\w{1,32}).zip$\/i', $zipName,$matches)) {\r\n    abort(\"Zip file must conform to Lua variable name convention with .zip extention\");\r\n} else {\r\n    $imageName = $matches&#91;1].'.img';\r\n}\r\nif (!is_uploaded_file($zipFile)) abort(\"No zip file uploaded\");\r\n$zip = zip_open($zipFile);\r\nif (!is_resource($zip)) abort(\"File is not a valid ZIP format\");\r\n\/\/ Unpack the uploaded ZIP file into a tmpdir\r\n$tempDir = tempnam(sys_get_temp_dir(), 'luac_dir');\r\nif (file_exists($tempDir)) unlink($tempDir);\r\nmkdir($tempDir);\r\nwhile (is_resource($entry = zip_read($zip))) {\r\n  $name = basename(zip_entry_name($entry));\r\n  $entryName = \"${tempDir}\/${name}\";\r\n  \/\/ Filenames must have a valid Lua name root\r\n  if (preg_match('\/^&#91;&#91;:alpha:]_]\\w*.lua$\/', $name)) {\r\n    echo \"$entryName found\\n\"; \/\/ Debug\r\n    $entries&#91;] = $entryName;\r\n    $contents = '';\r\n    while (($chunk = zip_entry_read($entry,8192))) {\r\n      $contents .= $chunk;\r\n    }\r\n    zip_entry_close($entry); $entry = NULL;\r\n    file_put_contents($entryName, $contents);\r\n  }\r\n}\r\nzip_close($zip); $zip = NULL;\r\nif (count($entries)==0) abort(\"ZIP contains no Lua files\");\r\n$luaFiles = implode (\" \", $entries);\r\n$outLFS = \"${tempDir}\/lfs.img\";\r\nexec(\"luac.cross -f -o ${outLFS} ${luaFiles}\", $output, $status);\r\nif ($status != 0) abort(\"luac.cross compile error: \\n\" . implode (\"\\n\", $output) );\r\n$LFSimage = file_get_contents($outLFS);\r\nfile_put_contents(time().\".img\",$LFSimage);\r\nexec(\"rm -rf ${tempDir}\");\r\nfile_put_contents($dbgErrLog, ob_get_contents());\r\nob_end_clean();\r\nhttp_response_code(200);\r\nheader('Content-Type: application\/octet-stream');\r\nheader(\"Content-Disposition: attachment; filename=LFS.img\");\r\nheader('Content-Transfer-Encoding: binary');\r\nheader('Content-Length: ' . strlen($LFSimage));\r\necho $LFSimage;<\/code><\/pre>\n\n\n\n<p>\u5c31\u662f\u91c7\u7528luac.cross\u5c06lua\u6587\u4ef6\u6253\u5305\u3002\u5bf9\u4e8eesp8266\u5c0f\u5185\u5b58mcu\u6548\u679c\u826f\u597d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>http:\/\/192.168.99.10\/test\/ \u5185\u7f51\u670d\u52a1\u5668\u4e0a\uff0c\u6709\u4e2a\u670d\u52a1\uff0c\u53ef &hellip; <a href=\"http:\/\/www.luwl.net\/?p=1224\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1224","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/www.luwl.net\/index.php?rest_route=\/wp\/v2\/posts\/1224","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.luwl.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.luwl.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.luwl.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.luwl.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1224"}],"version-history":[{"count":1,"href":"http:\/\/www.luwl.net\/index.php?rest_route=\/wp\/v2\/posts\/1224\/revisions"}],"predecessor-version":[{"id":1225,"href":"http:\/\/www.luwl.net\/index.php?rest_route=\/wp\/v2\/posts\/1224\/revisions\/1225"}],"wp:attachment":[{"href":"http:\/\/www.luwl.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.luwl.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1224"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.luwl.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}