mirror of
				https://github.com/DigitalDevices/octonet.git
				synced 2023-10-10 11:36:52 +00:00 
			
		
		
		
	up and download new JSON channel and transponder lists
Also some improvement on boundary handling on old uploader
This commit is contained in:
		@@ -118,7 +118,7 @@ function Legacy2JSON()
 | 
			
		||||
   return data
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function CreateJSON(host)
 | 
			
		||||
function CreateJSON()
 | 
			
		||||
   local data = nil
 | 
			
		||||
   local file = io.open("/config/ChannelList.json")
 | 
			
		||||
   if file then
 | 
			
		||||
@@ -130,9 +130,26 @@ function CreateJSON(host)
 | 
			
		||||
   return data
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function TransponderList(user)
 | 
			
		||||
   local data = nil
 | 
			
		||||
   local file
 | 
			
		||||
   if user then
 | 
			
		||||
      file = io.open("/config/TransponderList.json","r")
 | 
			
		||||
   end
 | 
			
		||||
   if not file then
 | 
			
		||||
      file = io.open("/var/channels/TransponderList.json","r")
 | 
			
		||||
   end
 | 
			
		||||
   if file then
 | 
			
		||||
      data = file:read("*a")
 | 
			
		||||
      file:close()
 | 
			
		||||
   end
 | 
			
		||||
   return data
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function LoadSourceList()
 | 
			
		||||
   local db = require("DataBase")
 | 
			
		||||
   local SourceList = {}
 | 
			
		||||
   local f,c
 | 
			
		||||
 | 
			
		||||
   for _,f in ipairs(db.SourceList) do
 | 
			
		||||
     f.ChannelList = {}
 | 
			
		||||
@@ -160,7 +177,15 @@ if method == "GET" then
 | 
			
		||||
  elseif string.match(query,"select=json") then
 | 
			
		||||
    filename = "ChannelList.json"
 | 
			
		||||
    contenttype = "application/json; charset=utf-8"
 | 
			
		||||
    data = CreateJSON(host)
 | 
			
		||||
    data = CreateJSON()
 | 
			
		||||
  elseif string.match(query,"select=stl") then
 | 
			
		||||
    filename = "TransponderList.json"
 | 
			
		||||
    contenttype = "application/json; charset=utf-8"
 | 
			
		||||
    data = TransponderList(false)
 | 
			
		||||
  elseif string.match(query,"select=tl") then
 | 
			
		||||
    filename = "TransponderList.json"
 | 
			
		||||
    contenttype = "application/json; charset=utf-8"
 | 
			
		||||
    data = TransponderList(true)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  if data then
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 | 
			
		||||
<!DOCTYPE HTML>
 | 
			
		||||
<html>
 | 
			
		||||
<head>
 | 
			
		||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 | 
			
		||||
@@ -107,7 +107,9 @@ function DisableButtons(disabled)
 | 
			
		||||
   document.getElementById("ScanButton").disabled = disabled;
 | 
			
		||||
   document.getElementById("StatusButton").disabled = disabled;
 | 
			
		||||
   document.getElementById("DeleteButton").disabled = disabled;
 | 
			
		||||
   document.getElementById("DeleteAllButton").disabled = disabled;
 | 
			
		||||
   document.getElementById("RestoreButton").disabled = disabled;
 | 
			
		||||
   document.getElementById("UploadButton").disabled = disabled;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -169,6 +171,16 @@ function ScanStatus(response)
 | 
			
		||||
         else
 | 
			
		||||
            document.getElementById("scantext").firstChild.nodeValue = "Previous channel list restored";
 | 
			
		||||
      }
 | 
			
		||||
      else if( s.status == "error" )
 | 
			
		||||
      {
 | 
			
		||||
         document.getElementById("scancount").firstChild.nodeValue = "Error";
 | 
			
		||||
         document.getElementById("scantext").firstChild.nodeValue = s.msg;
 | 
			
		||||
      }
 | 
			
		||||
      else if( s.status == "updated" )
 | 
			
		||||
      {
 | 
			
		||||
         document.getElementById("scancount").firstChild.nodeValue = "\u00A0";
 | 
			
		||||
         document.getElementById("scantext").firstChild.nodeValue = s.msg + " updated";
 | 
			
		||||
      }
 | 
			
		||||
      else if( s.status == "retry" )
 | 
			
		||||
      {
 | 
			
		||||
         done = false;
 | 
			
		||||
@@ -236,11 +248,12 @@ function PollStatus()
 | 
			
		||||
   GetStatus();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function DeleteScan()
 | 
			
		||||
function DeleteScan(all)
 | 
			
		||||
{
 | 
			
		||||
   DisableButtons(true);
 | 
			
		||||
   ScanReq.open("POST", "/channelscan.lua", true);
 | 
			
		||||
   ScanReq.setRequestHeader("Content-type","application/x-www-form-urlencoded");
 | 
			
		||||
   ScanReq.send("select=delete");
 | 
			
		||||
   ScanReq.send("select=delete" + (all ? "&all=true" : ""));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function RestoreScan()
 | 
			
		||||
@@ -251,15 +264,32 @@ function RestoreScan()
 | 
			
		||||
   ScanReq.send("select=restore");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
function Uploader(event)
 | 
			
		||||
{
 | 
			
		||||
   event.preventDefault();
 | 
			
		||||
   document.getElementById("scancount").firstChild.nodeValue = "\u00A0";
 | 
			
		||||
   document.getElementById("scantext").firstChild.nodeValue = "\u00A0";
 | 
			
		||||
   var fileSelect = document.getElementById("UploadList");
 | 
			
		||||
   if( fileSelect.files.length > 0 )
 | 
			
		||||
   {
 | 
			
		||||
      var formData = new FormData();
 | 
			
		||||
      formData.append('filename', fileSelect.files[0], fileSelect.files[0].name)
 | 
			
		||||
      DisableButtons(true);
 | 
			
		||||
      ScanReq.open("POST", "/channelscan.lua", true);
 | 
			
		||||
      ScanReq.send(formData);
 | 
			
		||||
   }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
</script>
 | 
			
		||||
 | 
			
		||||
</head>
 | 
			
		||||
<body onload="OnLoad()">
 | 
			
		||||
 | 
			
		||||
<table class="maintable" align="center">
 | 
			
		||||
<table class="maintable">
 | 
			
		||||
  <colgroup>
 | 
			
		||||
    <col width="182px"/>
 | 
			
		||||
    <col width="728px"/>
 | 
			
		||||
    <col style="width: 182px"/>
 | 
			
		||||
    <col style="width: 728px"/>
 | 
			
		||||
  </colgroup>
 | 
			
		||||
  <tr><td class="maintd" colspan="2">
 | 
			
		||||
    <a href="http://www.digitaldevices.de"><img src="/BannerDD.jpg" alt="DD" width="910" height="130" /></a>
 | 
			
		||||
@@ -270,7 +300,7 @@ function RestoreScan()
 | 
			
		||||
    <td class="content">
 | 
			
		||||
      <div>
 | 
			
		||||
        <!-- Begin Content -->
 | 
			
		||||
        <table cellpadding="2px" align="center">
 | 
			
		||||
        <table cellpadding="2px" class="center-div">
 | 
			
		||||
          <tr id="trCable" style="display:none">
 | 
			
		||||
            <td>Cable</td>
 | 
			
		||||
            <td style="text-align:right">
 | 
			
		||||
@@ -342,10 +372,34 @@ function RestoreScan()
 | 
			
		||||
               </form>
 | 
			
		||||
            </td>
 | 
			
		||||
          </tr>
 | 
			
		||||
          <tr>
 | 
			
		||||
            <td colspan="2" style="text-align:right">
 | 
			
		||||
              <form action="" method="post" enctype="multipart/form-data" onsubmit="Uploader(event)">
 | 
			
		||||
                <input id="UploadList" type="file" name="filename" value="*.json" size="30" accept=".json">
 | 
			
		||||
                <input id="UploadButton" type="submit" value="Upload">
 | 
			
		||||
              </form>
 | 
			
		||||
            </td>
 | 
			
		||||
          </tr>
 | 
			
		||||
        </table>
 | 
			
		||||
        <hr/>
 | 
			
		||||
        <div style="text-align: center">
 | 
			
		||||
            <h4>Downloads</h4>
 | 
			
		||||
            Channel List:
 | 
			
		||||
                <a href="/channellist.lua?select=json">JSON</a>
 | 
			
		||||
                <a href="/channellist.lua?select=m3u">M3U</a>
 | 
			
		||||
            <br/>
 | 
			
		||||
            Transponder List:
 | 
			
		||||
                <a href="/channellist.lua?select=tl">Current</a>
 | 
			
		||||
                <a href="/channellist.lua?select=stl">Default</a>
 | 
			
		||||
               <br/>
 | 
			
		||||
               <form action="">
 | 
			
		||||
                 <input id="DeleteAllButton" type="Button" value="Delete All" onclick="DeleteScan(true)" >
 | 
			
		||||
               </form>
 | 
			
		||||
        </div>
 | 
			
		||||
 | 
			
		||||
        <hr/>
 | 
			
		||||
        <table cellpadding="2px" align="center">
 | 
			
		||||
        <h4 style="text-align: center">Old Format Lists</h4>
 | 
			
		||||
        <table cellpadding="2px" class="center-div">
 | 
			
		||||
          <tr>
 | 
			
		||||
            <td>System Channel Database</td>
 | 
			
		||||
            <td>
 | 
			
		||||
 
 | 
			
		||||
@@ -34,6 +34,11 @@ function SendError(err,desc)
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function perr(s)
 | 
			
		||||
   io.stderr:write(tostring(s).."\n")
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
--
 | 
			
		||||
local newdecoder = require("lunajson.decoder")
 | 
			
		||||
local newencoder = require("lunajson.encoder")
 | 
			
		||||
@@ -55,6 +60,75 @@ local function LoadTransponderList()
 | 
			
		||||
   return tl
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local function CheckTransponderList(tl)
 | 
			
		||||
   local i,Source
 | 
			
		||||
   for i,Source in ipairs(tl.SourceList) do
 | 
			
		||||
      if not Source.Title then
 | 
			
		||||
         error("SourceList["..i.."].Title missing",0)
 | 
			
		||||
      end
 | 
			
		||||
      if not Source.Key then
 | 
			
		||||
         error("SourceList["..i.."].Key missing",0)
 | 
			
		||||
      end
 | 
			
		||||
      if not Source.DVBType then
 | 
			
		||||
         error("SourceList["..i.."].DVBType missing",0)
 | 
			
		||||
      end
 | 
			
		||||
      if not Source.TransponderList then
 | 
			
		||||
         error("SourceList["..i.."].TransponderList missing",0)
 | 
			
		||||
      end
 | 
			
		||||
      local j,Transponder
 | 
			
		||||
      for j,Transponder in ipairs(Source.TransponderList) do
 | 
			
		||||
         if not Transponder.Request then
 | 
			
		||||
            error("SourceList["..i.."].TransponderList["..j.."].Request missing",0)
 | 
			
		||||
         end
 | 
			
		||||
      end
 | 
			
		||||
   end
 | 
			
		||||
   if tl.CIMapList then
 | 
			
		||||
      local CIMap
 | 
			
		||||
      for i,CIMap in ipairs(tl.CIMapList) do
 | 
			
		||||
         if not CIMap.Slot then
 | 
			
		||||
            error("CIMapList["..i.."].Slot missing",0)
 | 
			
		||||
         end
 | 
			
		||||
      end
 | 
			
		||||
   end
 | 
			
		||||
   if tl.ChannelOverwriteList then
 | 
			
		||||
      local ChannelOverwrite
 | 
			
		||||
      for i,ChannelOverwrite in ipairs(tl.ChannelOverwriteList) do
 | 
			
		||||
         if not ChannelOverwrite.ID then
 | 
			
		||||
            error("ChannelOverwriteList["..i.."].ID missing",0)
 | 
			
		||||
         end
 | 
			
		||||
      end
 | 
			
		||||
   end
 | 
			
		||||
   if tl.GroupList then
 | 
			
		||||
      local Group
 | 
			
		||||
      for i,Group in ipairs(tl.GroupList) do
 | 
			
		||||
         if not Group.Title then
 | 
			
		||||
            error("GroupList["..i.."].Title missing",0)
 | 
			
		||||
         end
 | 
			
		||||
      end
 | 
			
		||||
   end
 | 
			
		||||
   return "TransponderList.json"
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local function CheckChannelList(cl)
 | 
			
		||||
   local i,Group
 | 
			
		||||
   for i,Group in ipairs(cl.GroupList) do
 | 
			
		||||
      if not Group.Title then
 | 
			
		||||
         error("GroupList["..i.."].Title missing",0)
 | 
			
		||||
      end
 | 
			
		||||
      local j,Channel
 | 
			
		||||
      for j,Channel in ipairs(Group.ChannelList) do
 | 
			
		||||
         if not Channel.Title then
 | 
			
		||||
            error("GroupList["..i.."].ChannelList["..j.."].Title missing",0)
 | 
			
		||||
         end
 | 
			
		||||
         if not Channel.Request then
 | 
			
		||||
            error("GroupList["..i.."].ChannelList["..j.."].Request missing",0)
 | 
			
		||||
         end
 | 
			
		||||
      end
 | 
			
		||||
   end
 | 
			
		||||
   return "ChannelList.json"
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
local function GetCMD(s)
 | 
			
		||||
   local q,v
 | 
			
		||||
   local params = ""
 | 
			
		||||
@@ -139,7 +213,7 @@ local function Status()
 | 
			
		||||
   return data
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local function Delete()
 | 
			
		||||
local function Delete(params)
 | 
			
		||||
   local data = nil
 | 
			
		||||
   local rc = os.execute("mkdir /tmp/doscan.lock")
 | 
			
		||||
   if rc ~= 0 then
 | 
			
		||||
@@ -147,6 +221,9 @@ local function Delete()
 | 
			
		||||
   else
 | 
			
		||||
      data = '{"status":"deleted"}'
 | 
			
		||||
      os.execute("rm /config/ChannelList.json");
 | 
			
		||||
      if params == " all=true" then
 | 
			
		||||
         os.execute("rm /config/TransponderList.json");
 | 
			
		||||
      end
 | 
			
		||||
      os.execute("rm /tmp/doscan.msg");
 | 
			
		||||
      os.execute("rm -rf /tmp/doscan.lock");
 | 
			
		||||
   end
 | 
			
		||||
@@ -171,6 +248,70 @@ local function Restore()
 | 
			
		||||
   return data
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local function Upload()
 | 
			
		||||
   local data = nil
 | 
			
		||||
   local rc = os.execute("mkdir /tmp/doscan.lock")
 | 
			
		||||
   if rc ~= 0 then
 | 
			
		||||
      data = '{"status":"busy"}'
 | 
			
		||||
   else
 | 
			
		||||
      local boundary = string.match(ctype,"boundary=(.*)")
 | 
			
		||||
      if boundary then
 | 
			
		||||
         while true do
 | 
			
		||||
           local line = io.stdin:read()
 | 
			
		||||
           line = string.gsub(line,"\r","")
 | 
			
		||||
           if line == "" then break end
 | 
			
		||||
         end
 | 
			
		||||
 | 
			
		||||
         local filedata = io.stdin:read("*a")
 | 
			
		||||
         local i = filedata:find("--"..boundary,1,true)
 | 
			
		||||
         if i then
 | 
			
		||||
            filedata = filedata:sub(1,i-1)
 | 
			
		||||
         else
 | 
			
		||||
            filedata = "{}"
 | 
			
		||||
         end
 | 
			
		||||
 | 
			
		||||
         local decode = newdecoder()
 | 
			
		||||
         local rc,json = pcall(decode,filedata)
 | 
			
		||||
 | 
			
		||||
         if rc then
 | 
			
		||||
            rc = false
 | 
			
		||||
            local msg = "invalid list"
 | 
			
		||||
            if json.SourceList then
 | 
			
		||||
               rc,msg = pcall(CheckTransponderList,json)
 | 
			
		||||
               data = '{"status":"updated", "msg":"Transponder list"}'
 | 
			
		||||
            elseif json.GroupList then
 | 
			
		||||
               rc,msg = pcall(CheckChannelList,json)
 | 
			
		||||
            end
 | 
			
		||||
            if rc then
 | 
			
		||||
               local f = io.open("/config/"..msg,"w+")
 | 
			
		||||
               if f then
 | 
			
		||||
                  f:write(filedata)
 | 
			
		||||
                  f:close()
 | 
			
		||||
                  data = '{"status":"updated", "msg":"'..msg..'"}'
 | 
			
		||||
               else
 | 
			
		||||
                  data = '{"status":"error", "msg":"'..msg..' not saved"}'
 | 
			
		||||
               end
 | 
			
		||||
            else
 | 
			
		||||
               data = '{"status":"error", "msg":"'..msg..'"}'
 | 
			
		||||
            end
 | 
			
		||||
         else
 | 
			
		||||
            local msg = "unknown error"
 | 
			
		||||
            if json then
 | 
			
		||||
               msg = json:match(".-: (.*)")
 | 
			
		||||
            end
 | 
			
		||||
            data = '{"status":"error", "msg":"'..msg..'"}'
 | 
			
		||||
         end
 | 
			
		||||
 | 
			
		||||
      else
 | 
			
		||||
        data = '{"status":"error","msg":"malformed request"}'
 | 
			
		||||
      end
 | 
			
		||||
      os.execute("rm /tmp/doscan.msg");
 | 
			
		||||
      os.execute("rm -rf /tmp/doscan.lock");
 | 
			
		||||
   end
 | 
			
		||||
   return data
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
local filename = nil
 | 
			
		||||
local contenttype = "application/json; charset=utf-8"
 | 
			
		||||
local data = nil
 | 
			
		||||
@@ -184,6 +325,8 @@ elseif method == "POST" and clength and ctype then
 | 
			
		||||
      local query = io.read(tonumber(clength))
 | 
			
		||||
      query = string.gsub(query,"\r","")
 | 
			
		||||
      cmd,params = GetCMD(query)
 | 
			
		||||
   elseif ctype:match("multipart/form%-data") then
 | 
			
		||||
      cmd = "upload"
 | 
			
		||||
   end
 | 
			
		||||
else
 | 
			
		||||
   SendError("500","What")
 | 
			
		||||
@@ -197,9 +340,11 @@ elseif cmd == "scan" then
 | 
			
		||||
elseif cmd == "status" then
 | 
			
		||||
   data = Status()
 | 
			
		||||
elseif cmd == "delete" then
 | 
			
		||||
   data = Delete()
 | 
			
		||||
   data = Delete(params)
 | 
			
		||||
elseif cmd == "restore" then
 | 
			
		||||
   data = Restore()
 | 
			
		||||
elseif cmd == "upload" then
 | 
			
		||||
   data = Upload()
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
if data then
 | 
			
		||||
 
 | 
			
		||||
@@ -48,7 +48,7 @@ if not string.match(ctype,"multipart/form%-data") then
 | 
			
		||||
  return
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local boundary = string.match(ctype,"boundary=(.*)") 
 | 
			
		||||
local boundary = string.match(ctype,"boundary=(.*)")
 | 
			
		||||
if not boundary then
 | 
			
		||||
  SendError("404","???")
 | 
			
		||||
  return
 | 
			
		||||
@@ -66,25 +66,32 @@ while true do
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
data = io.stdin:read("*a")
 | 
			
		||||
data = string.sub(data,1,#data - #boundary - 4)
 | 
			
		||||
 | 
			
		||||
local file = io.open("/tmp/"..filename,"w")
 | 
			
		||||
if file then
 | 
			
		||||
  file:write(data)
 | 
			
		||||
  file:close()
 | 
			
		||||
local i = data:find("--"..boundary,1,true)
 | 
			
		||||
if i then
 | 
			
		||||
   data = data:sub(1,i-1)
 | 
			
		||||
else
 | 
			
		||||
   data = nil
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
if string.match(filename,"%.tar%.gz$") then
 | 
			
		||||
  os.execute("rm -rf /config/channels;mkdir /config/channels;cd /config/channels;gunzip -c /tmp/"..filename.."|tar -xf -");
 | 
			
		||||
elseif string.match(filename,"%.zip$") then
 | 
			
		||||
  os.execute("rm -rf /config/channels;mkdir /config/channels;cd /config/channels;unzip -q /tmp/"..filename);
 | 
			
		||||
end
 | 
			
		||||
if data then
 | 
			
		||||
   local file = io.open("/tmp/"..filename,"w")
 | 
			
		||||
   if file then
 | 
			
		||||
     file:write(data)
 | 
			
		||||
     file:close()
 | 
			
		||||
   end
 | 
			
		||||
 | 
			
		||||
os.remove("/tmp/"..filename)
 | 
			
		||||
   if string.match(filename,"%.tar%.gz$") then
 | 
			
		||||
     os.execute("rm -rf /config/channels;mkdir /config/channels;cd /config/channels;gunzip -c /tmp/"..filename.."|tar -xf -");
 | 
			
		||||
   elseif string.match(filename,"%.zip$") then
 | 
			
		||||
     os.execute("rm -rf /config/channels;mkdir /config/channels;cd /config/channels;unzip -q /tmp/"..filename);
 | 
			
		||||
   end
 | 
			
		||||
 | 
			
		||||
   os.remove("/tmp/"..filename)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
-- TODO validate
 | 
			
		||||
 | 
			
		||||
http_print(proto.." 303")
 | 
			
		||||
http_print("Location: http://"..host.."/reboot.html")
 | 
			
		||||
http_print()
 | 
			
		||||
 
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user