* luci/libs: move upload dispatching from dispatcher to cbi.load()
authorJo-Philipp Wich <jow@openwrt.org>
Tue, 23 Sep 2008 13:30:07 +0000 (13:30 +0000)
committerJo-Philipp Wich <jow@openwrt.org>
Tue, 23 Sep 2008 13:30:07 +0000 (13:30 +0000)
libs/cbi/luasrc/cbi.lua
libs/web/luasrc/dispatcher.lua

index 8cde0a1..296ec81 100644 (file)
@@ -52,6 +52,7 @@ function load(cbimap, ...)
        require("luci.config")
        require("luci.util")
 
+       local upldir = "/lib/uci/upload/"
        local cbidir = luci.util.libpath() .. "/model/cbi/"
        local func, err = loadfile(cbimap) or loadfile(cbidir..cbimap..".lua")
        assert(func, err)
@@ -70,7 +71,9 @@ function load(cbimap, ...)
                        return rawget(tbl, key) or _M[key] or _G[key]
                end}))
 
-       local maps = {func()}
+       local maps       = { func() }
+       local uploads    = { }
+       local has_upload = false
 
        for i, map in ipairs(maps) do
                if not instanceof(map, Node) then
@@ -78,9 +81,59 @@ function load(cbimap, ...)
                        return nil
                else
                        map:prepare()
+                       if map.upload_fields then
+                               has_upload = true
+                               for _, field in ipairs(map.upload_fields) do
+                                       uploads[
+                                               field.config .. '.' ..
+                                               field.section.sectiontype .. '.' ..
+                                               field.option
+                                       ] = true
+                               end
+                       end
                end
        end
 
+       if has_upload then
+               local uci = luci.model.uci.cursor()
+               local prm = luci.http.context.request.message.params
+               local fd, cbid
+
+               luci.http.setfilehandler(
+                       function( field, chunk, eof )
+                               if not field then return end
+                               if field.name and not cbid then
+                                       local c, s, o = field.name:gmatch(
+                                               "cbid%.([^%.]+)%.([^%.]+)%.([^%.]+)"
+                                       )()
+
+                                       if c and s and o then
+                                               local t = uci:get( c, s )
+                                               if t and uploads[c.."."..t.."."..o] then
+                                                       local path = upldir .. field.name
+                                                       fd = io.open(path, "w")
+                                                       if fd then
+                                                               cbid = field.name
+                                                               prm[cbid] = path
+                                                       end
+                                               end
+                                       end
+                               end
+
+                               if field.name == cbid and fd then
+                                       io.stderr:write("*** CHUNK:"..tostring(#chunk).." ***\n")
+                                       fd:write(chunk)
+                               end
+
+                               if eof and fd then
+                                       fd:close()
+                                       fd   = nil
+                                       cbid = nil
+                               end
+                       end
+               )
+       end
+
        return maps
 end
 
index 0665ce4..236bc9c 100644 (file)
@@ -466,63 +466,6 @@ function cbi(model)
 
                maps = luci.cbi.load(model, ...)
 
-               local uploads    = { }
-               local has_upload = false
-
-               for _, map in ipairs(maps) do
-                       if map.upload_fields then
-                               has_upload = true
-                               for _, field in ipairs(map.upload_fields) do
-                                       uploads[
-                                               field.config .. '.' ..
-                                               field.section.sectiontype .. '.' ..
-                                               field.option
-                                       ] = true
-                               end
-                       end
-               end
-
-               if has_upload then
-                       local uci = luci.model.uci.cursor()
-                       local prm = luci.http.context.request.message.params
-                       local fd, cbid
-
-                       luci.http.setfilehandler(
-                               function( field, chunk, eof )
-                                       if not field then return end
-                                       if field.name and not cbid then
-                                               local c, s, o = field.name:gmatch(
-                                                       "cbid%.([^%.]+)%.([^%.]+)%.([^%.]+)"
-                                               )()
-
-                                               if c and s and o then
-                                                       local t = uci:get( c, s )
-                                                       if t and uploads[c.."."..t.."."..o] then
-                                                               local path = "/lib/uci/upload/"..field.name
-                                                               fd = io.open(path, "w")
-                                                               if fd then
-                                                                       cbid = field.name
-                                                                       prm[cbid] = path
-                                                       --      else
-                                                       --              io.stderr:write("E: " .. err .. "\n")
-                                                               end
-                                                       end
-                                               end
-                                       end
-
-                                       if field.name == cbid and fd then
-                                               fd:write(chunk)
-                                       end
-
-                                       if eof and fd then
-                                               fd:close()
-                                               fd   = nil
-                                               cbid = nil
-                                       end
-                               end
-                       )
-               end
-
                for i, res in ipairs(maps) do
                        res:parse()
                end