+ 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