I am trying to implement a facility in LuCI to allow a user to upload certificate files into a specified directory, keeping the original name of the file.
I am completely new to Lua, so I have tried to re-use/adapt the code that implements the firmware sysupgrade facility (in luci.controller.admin.system - snippet shown below).
I have it working OK except that the existing code uploads the file and saves it with a fixed name - firmware.img - whereas I need to retain the original filename.
When the required file is selected with the Browse button, the original filename is displayed adjacent to the Upload button.
My question is how can I get the real filename and use that to save the file instead of the fixed name?
Thanks in advance for help and suggestions.
module("luci.controller.admin.system", package.seeall)
function index()
local fs = require "nixio.fs"
...
-- call() instead of post() due to upload handling!
entry({"admin", "system", "flashops", "sysupgrade"}, call("action_sysupgrade"))
...
end
function action_sysupgrade()
local fs = require "nixio.fs"
local http = require "luci.http"
local image_tmp = "/tmp/firmware.img"
local fp
http.setfilehandler(
function(meta, chunk, eof)
if not fp and meta and meta.name == "image" then
fp = io.open(image_tmp, "w")
end
if fp and chunk then
fp:write(chunk)
end
if fp and eof then
fp:close()
end
end
)