OpenWrt网页支持升级本地package

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 of call 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 in action_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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值