Publish luci.model.ipkg via JSON-RPC
[project/luci.git] / modules / rpc / luasrc / controller / rpc.lua
index aa77a8f..fdfbdb3 100644 (file)
@@ -16,6 +16,8 @@ $Id$
 local require = require
 local pairs = pairs
 local print = print
+local pcall = pcall
+local table = table
 
 module "luci.controller.rpc"
 
@@ -43,6 +45,10 @@ function index()
        fs.sysauth = "root"
        fs.sysauth_authenticator = authenticator
        
+       fs = entry({"rpc", "ipkg"}, call("rpc_ipkg"))
+       fs.sysauth = "root"
+       fs.sysauth_authenticator = authenticator
+       
        uci = entry({"rpc", "auth"}, call("rpc_auth"))
 end
 
@@ -51,6 +57,7 @@ function rpc_auth()
        local sauth   = require "luci.sauth"
        local http    = require "luci.http"
        local sys     = require "luci.sys"
+       local ltn12   = require "luci.ltn12"
        
        http.setfilehandler()
        
@@ -70,35 +77,75 @@ function rpc_auth()
        end
        
        http.prepare_content("application/json")
-       http.write(jsonrpc.handle(server, http.content()))
+       ltn12.pump.all(jsonrpc.handle(server, http.source()), http.write)
 end
 
 function rpc_uci()
        local uci     = require "luci.controller.rpc.uci"
        local jsonrpc = require "luci.jsonrpc"
        local http    = require "luci.http"
+       local ltn12   = require "luci.ltn12"
        
-       http.setfilehandler()
        http.prepare_content("application/json")
-       http.write(jsonrpc.handle(uci, http.content()))
+       ltn12.pump.all(jsonrpc.handle(uci, http.source()), http.write)
 end
 
 function rpc_fs()
-       local fs      = require "luci.fs"
+       local util    = require "luci.util"
+       local io      = require "io"
+       local fs2     = util.clone(require "luci.fs")
        local jsonrpc = require "luci.jsonrpc"
        local http    = require "luci.http"
+       local ltn12   = require "luci.ltn12"
+
+       function fs2.readfile(filename)
+               local stat, mime = pcall(require, "mime")
+               if not stat then
+                       error("Base64 support not available. Please install LuaSocket.")
+               end
+
+               local fp = io.open(filename)
+               if not fp then
+                       return nil
+               end
+
+               local output = {}
+               local sink = ltn12.sink.table(output)
+               local source = ltn12.source.chain(ltn12.source.file(fp), mime.encode("base64"))
+               return ltn12.pump.all(source, sink) and table.concat(output)
+       end
+       
+       function fs2.writefile(filename, data)
+               local stat, mime = pcall(require, "mime")
+               if not stat then
+                       error("Base64 support not available. Please install LuaSocket.")
+               end
+
+               local  file = io.open(filename, "w")
+               local  sink = file and ltn12.sink.chain(mime.decode("base64"), ltn12.sink.file(file))
+               return sink and ltn12.pump.all(ltn12.source.string(data), sink) or false
+       end
        
-       http.setfilehandler()
        http.prepare_content("application/json")
-       http.write(jsonrpc.handle(fs, http.content()))
+       ltn12.pump.all(jsonrpc.handle(fs2, http.source()), http.write)
 end
 
 function rpc_sys()
        local sys     = require "luci.sys"
        local jsonrpc = require "luci.jsonrpc"
        local http    = require "luci.http"
+       local ltn12   = require "luci.ltn12"
        
-       http.setfilehandler()
        http.prepare_content("application/json")
-       http.write(jsonrpc.handle(sys, http.content()))
-end
\ No newline at end of file
+       ltn12.pump.all(jsonrpc.handle(sys, http.source()), http.write)
+end
+
+function rpc_ipkg()
+       local ipkg    = require "luci.model.ipkg"
+       local jsonrpc = require "luci.jsonrpc"
+       local http    = require "luci.http"
+       local ltn12   = require "luci.ltn12"
+
+       http.prepare_content("application/json")
+       ltn12.pump.all(jsonrpc.handle(ipkg, http.source()), http.write)
+end