OpenWrt网页支持升级本地package

来源:互联网 发布:碧欧泉淘宝是正品吗 编辑:程序博客网 时间:2024/05/05 00:47

Install packages via web interface

Trigger to send file

                in file of packages.htm(View), which represent the apperance at Client’s site,  use method of POST , and input of FILE and SUBMIT . the following code can be add to packages.htm

                Note:  .tar.gz file should be used.            

                example:

                <form method="post" action="<%=REQUEST_URI%>" enctype="multipart/form-data">

                                <!-- show feedback ---->

                                <% if (install and next(install)) or (remove and next(remove)) or update or upgrade then %>

                                <div class="cbi-value">

                                                <% if #stdout > 0 then %><pre><%=pcdata(stdout)%></pre><% end %>

                                                <% if #stderr > 0 then %><pre class="error"><%=pcdata(stderr)%></pre><% end %>

                                </div>

                                <% end %>

 

                                <!-- send post ---->

                                <div class="cbi-section-descr"><%:To install a package from local PC, you can choose a package file here.%></div>

                                <div class="cbi-section-node">

                                                <div class="cbi-value cbi-value-last">

                                                                <label class="cbi-value-title" for=" ipk"><%:Transfer and install package%>:</label>

                                                                <div class="cbi-value-field">

                                                                                <input type="file" name="ipk" id=" ipk" size="30"/>

                                                                                <input type="submit" class="cbi-button cbi-input-apply" name="restore" value="<%:Start transfer...%>" />

                                                                </div>

                                                </div>

                                </div>

                </form>

Receive the request

                system.lua(Controller), which defined a structure corresponding to the behavior options on the web page, points out how every operation be handled, typically via methods ofcall or cbi. In this case, call(“action_packages”)will be involved.

                example:

                                entry({"admin", "system", "packages"}, call("action_packages"), _("Software"), 10)

handle packages of file

typical implementation

                Refer to action_flashops in system.lua(Controller), filehandler of http need to be set to inform http that how to handle the packages,and “restore” of form value need to be handle in this function.

So insert the following code inaction_packages:

                local upload_cmd = "tar -xzC/ >/dev/null 2>&1"

                local fp

               -- I don't known why this function doesn't take any effect indeed here,

               -- so set Request.filehandler directly in http.lua

                luci.http.setfilehandler(

                                function(meta, chunk, eof)

                                                if not fp then

                                                                if meta and meta.name == "ipk" then

                                                                                fp = io.popen(upload_cmd, "w")

                                                                end

                                                end

                                                if chunk then

                                                                fp:write(chunk)

                                                end

                                                if eof then

                                                                fp:close()

                                                end

                                end

                )

 

                if luci.http.formvalue("restore") then

                                local upload = luci.http.formvalue("ipk")

                                if upload and #upload > 0 then

                                                install["my.ipk"], out, err = ipkg.install("/my.ipk")

                                                stdout[#stdout+1] = out

                                                stderr[#stderr+1] = err

                                                changes = true

 

                                                os.execute("rm / my.ipk")

                                end

                end

problem solution

                actually, I failed to restore the uploading file by the way introduced in the previous chapter, refer to the comment(yellow color). Anyway, I found another way to fulfill the target, which is a little clumsy – insert the following code into Request.__init__ of http.lua:

local io = require "io"

local fp = nil

local restore_cmd = "tar -xzC/ >/dev/null 2>&1"

local image_tmp   = "/tmp/firmware.img"

 

                                self.filehandler = function(meta, chunk, eof)

                                                if not fp then

                                                                if meta and meta.name == "image" then

                                                                                fp = io.open(image_tmp, "w")

                                                                else

                                                                                fp = io.popen(restore_cmd, "w")

                                                                end

                                                end

                                                if chunk then

                                                                fp:write(chunk)

                                                end

                                                if eof then

                                                                fp:close()

                                                end

                                end

 

0 0
原创粉丝点击