upload.lua 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. return function (connection, req, args)
  2. dofile("httpserver-header.lc")(connection, 200, 'json')
  3. connection:send('{')
  4. local mbOffset = nil
  5. local mbLen = nil
  6. local mbData = nil
  7. local mbCmd = nil
  8. local mbFilename = nil
  9. local fieldsCount = 0
  10. local fileSize = 0
  11. local i = 0
  12. local binaryData = ''
  13. local currentByte = nil
  14. for name, value in pairs(args) do
  15. if (name == "offset") then
  16. mbOffset = tonumber(value, 10)
  17. fieldsCount = fieldsCount + 1
  18. end
  19. if (name == "len") then
  20. mbLen = tonumber(value, 10)
  21. fieldsCount = fieldsCount + 1
  22. end
  23. if (name == "data") then
  24. mbData = value
  25. fieldsCount = fieldsCount + 1
  26. end
  27. if (name == "filename") then
  28. mbFilename = value
  29. fieldsCount = fieldsCount + 1
  30. end
  31. if (name == "filesize") then
  32. fileSize = tonumber(value, 10)
  33. fieldsCount = fieldsCount + 1
  34. end
  35. if (name == "cmd") then
  36. mbCmd = value
  37. fieldsCount = fieldsCount + 1
  38. end
  39. end
  40. if (mbCmd == 'upload') then
  41. if (fieldsCount > 5) then
  42. if (mbFilename ~= 'http/upload.lua') then
  43. connection:send('"offset":"' .. mbOffset .. '",')
  44. connection:send('"len":"' .. mbLen .. '",')
  45. connection:send('"filename":"' .. mbFilename .. '"')
  46. for i=1,string.len(mbData),2 do
  47. currentByte = tonumber(string.sub(mbData, i, i + 1), 16)
  48. binaryData = binaryData .. string.char(currentByte)
  49. end
  50. local mbTmpFilename = string.sub(mbFilename, 0, 27) .. '.dnl'
  51. if (mbOffset > 0) then
  52. file.open(mbTmpFilename,'a+')
  53. else
  54. file.remove(mbTmpFilename)
  55. file.open(mbTmpFilename,'w+')
  56. end
  57. file.seek("set", mbOffset)
  58. file.write(binaryData)
  59. file.close()
  60. binaryData = nil
  61. if (fileSize == mbLen + mbOffset) then
  62. file.remove(mbFilename)
  63. file.rename(mbTmpFilename, mbFilename)
  64. file.remove(mbTmpFilename)
  65. if (string.sub(mbFilename, -4) == '.lua') then
  66. file.remove(string.sub(mbFilename, 0, -3) .. "lc")
  67. node.compile(mbFilename)
  68. file.remove(mbFilename)
  69. end
  70. end
  71. end
  72. end
  73. elseif (mbCmd == 'list') then
  74. local remaining, used, total=file.fsinfo()
  75. local headerExist = 0
  76. connection:send('"files":{')
  77. for name, size in pairs(file.list()) do
  78. if (headerExist > 0) then
  79. connection:send(',')
  80. end
  81. local url = string.match(name, ".*/(.*)")
  82. url = name
  83. connection:send('"' .. url .. '":"' .. size .. '"')
  84. headerExist = 1
  85. end
  86. connection:send('},')
  87. connection:send('"total":"' .. total .. '",')
  88. connection:send('"used":"' .. used .. '",')
  89. connection:send('"free":"' .. remaining .. '"')
  90. elseif (mbCmd == 'remove') then
  91. if (fieldsCount > 1) then
  92. if (mbFilename ~= 'http/upload.lua') and (mbFilename ~= 'http/upload.lc') and (mbFilename ~= 'http/upload.html.gz') then
  93. file.remove(mbFilename)
  94. end
  95. end
  96. end
  97. connection:send('}')
  98. collectgarbage()
  99. end