Advertisement
jesusthekiller

Backup

Jul 25th, 2013
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.24 KB | None | 0 0
  1. --jBackup by Jesusthekiller
  2. --License: GNU GPL
  3. --More here: www.jesusthekiller.com/license-gpl.html
  4. assert(http, "Enable HTTP API!")
  5. local api = {}
  6. api.clear = function()
  7.     term.setBackgroundColor(colors.black)
  8.     term.setTextColor(colors.white)
  9.     term.clear()
  10.     term.setCursorPos(1, 1)
  11. end
  12. api.cprint = function(t, mode)
  13.     term.setCursorPos((52-#t)/2, ({term.getCursorPos()})[2])
  14.     write(t)
  15.     if not mode then write("\n") end
  16. end
  17. api.key = function(a, b)
  18.     local r
  19.     repeat
  20.         e, r = os.pullEvent("key")
  21.     until r == keys[a] or r == keys[b]
  22.         coroutine.yield()
  23.     return r == keys[a] and true or false
  24. end
  25. api.zip = function(p, r)
  26.     local files = fs.list(p)
  27.     for _,v in pairs(files) do
  28.         os.queueEvent("fake")
  29.         coroutine.yield()
  30.         print(p.."/"..v)
  31.         if fs.isDir(v) and v ~= "rom" then
  32.             table.insert(r.dir, p.."/"..v)
  33.             r = api.zip(p.."/"..v, r)
  34.         elseif (not fs.isDir(v)) then
  35.             local file = fs.open(v, "r") or error("Can not open "..p.."/"..v, 0)
  36.             r.file[p.."/"..v] = file.readAll()
  37.             file.close()
  38.         end
  39.     end
  40.     return r
  41. end
  42. api.unzip = function(t)
  43.     for _,v in ipairs(t.dir) do
  44.         print(v)
  45.         fs.makeDir(v)
  46.     end
  47.     for k,v in pairs(t.file) do
  48.         print(k)
  49.         local file = fs.open(k, "w") or error("Can not open "..v, 0)
  50.         file.write(v)
  51.         file.close()
  52.     end
  53. end
  54. api.pastebin_put = function(t)
  55.     print("Uploading...")
  56.    
  57.     local f= fs.open("oo", "w")
  58.     f.write(t)
  59.     f.close()
  60.    
  61.     local r = http.post("http://pastebin.com/api/api_post.php", "api_option=paste&api_dev_key=0ec2eb25b6166c0c27a394ae118ad829&api_paste_format=lua&api_paste_name=Backup&api_paste_code="..textutils.urlEncode(t))
  62.     assert(r, "Could not upload to pastebin!")
  63.     return string.match(r.readAll(), "[^/]+$")
  64. end
  65. api.pastebin_get = function(c)
  66.     print("Downloading...")
  67.     local r = http.get("http://pastebin.com/raw.php?i="..c)
  68.     assert(r, "Could not download from pastebin!")
  69.     return r.readAll()
  70. end
  71. api.clear()
  72. api.cprint("Welcome to jBackup v0.1!")
  73. api.cprint("Press 'p' for pack mode or 'u' for unpack mode")
  74. if api.key('p', 'u') then
  75.     api.cprint("File uploaded, code: "..api.pastebin_put(textutils.serialize(api.zip("", {['dir'] = {}, ['file'] = {}}))))
  76. else
  77.     api.cprint("Enter code:", true)
  78.     api.unzip(textutils.unserialize(api.pastebin_get(read())))
  79.     print("Done!")
  80. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement