chopstyix

Untitled

Feb 5th, 2022
1,457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.73 KB | None | 0 0
  1. if dropbox then os.unloadAPI("dropbox") end
  2. if not dropbox then os.loadAPI("dropbox") end
  3. if not dropbox then error("Cannot find dropbox", 0) end
  4.  
  5. --Prints out the usage for the function
  6. local commands = {
  7.     upload = '<LOCAL>  [REMOTE]',
  8.     download = '<REMOTE> [LOCAL]',
  9.     delete = '<REMOTE>',
  10.     move = '<SRC> <DEST>',
  11.     copy = '<SRC> <DEST>',
  12.     mkdir = '<REMOTE_DIR>',
  13.     list = '[REMOTE_DIR]',
  14. }
  15.  
  16. local function usage(command)
  17.     if command then
  18.         error("Incorrect usage: " .. command .. " " .. commands[command], 0)
  19.     end
  20.     print([[
  21. Dropbox Uploader 1.0
  22. Usage: COMMAND [PARAMETERS]...
  23. Commands:
  24.  upload   <LOCAL>  [REMOTE]
  25.  download <REMOTE> [LOCAL]
  26.  delete   <REMOTE>
  27.  move     <SRC> <DEST>
  28.  copy     <SRC> <DEST>
  29.  mkdir    <REMOTE_DIR>
  30.  list     [REMOTE_DIR]
  31. ]])
  32.     error("Incorrect usage", 0)
  33. end
  34.  
  35. --Setup variables
  36. local function setup()
  37.     write("App key >")
  38.     local key = read()
  39.     write("App Secret >")
  40.     local secret = read()
  41.  
  42.     local app = {
  43.         app_key = key,
  44.         app_secret = secret,
  45.     }
  46.  
  47.     -- These requests a token from online
  48.     local tokens = dropbox.requestToken(app)
  49.     if not tokens then error("Failed, check app key and secret", 0) end
  50.  
  51.     print("Please open the following url and allow the uploader")
  52.     print(tokens.url) -- URL to visit, same as before
  53.     print("Press any key to continue")
  54.     os.pullEvent("key")
  55.  
  56.     -- This gets an access token
  57.     config = dropbox.accessToken(app, tokens)
  58.     if not config then error("Failed, please try again", 0) end
  59.  
  60.     return config
  61. end
  62.  
  63. local config_file = '/.dropbox'
  64. local config
  65.  
  66. if not fs.exists(config_file) then
  67.     config = setup()
  68.  
  69.     -- Save the data
  70.     local handle = fs.open(config_file, "w")
  71.     handle.write(textutils.serialize(config))
  72.     handle.close()
  73. else
  74.     local handle = fs.open(config_file, "r")
  75.     config = textutils.unserialize(handle.readAll())
  76.     handle.close()
  77. end
  78.  
  79. -- Create a VFS with the specified config
  80. local drop = dropbox.create(config)
  81.  
  82. local args = {...}
  83.  
  84. local action = table.remove(args, 1)
  85. if action == "upload" then
  86.     -- This is a slightly overcomplicated upload
  87.     if #args < 1 or #args > 2 then
  88.         usage("upload")
  89.     end
  90.  
  91.     local from = shell.resolve(args[1])
  92.     if not fs.exists(from) then error("No such file", 0) end
  93.  
  94.     local to = args[2] or fs.combine("", args[1]):gsub("%.%./?", "")
  95.  
  96.     -- Here we check if the file is a directory. If we are a local file and
  97.     -- are uploading to a directory then add the file name to the end
  98.     if not fs.isDir(from) and drop.isDir(to) then
  99.         to = fs.combine(to, fs.getName(from))
  100.     end
  101.  
  102.     local queue = { { from, to } }
  103.     local todo = {}
  104.     while #queue > 0 do
  105.         local from, to = unpack(table.remove(queue, 1))
  106.  
  107.         if fs.isDir(from) then
  108.             -- Add additional files to the check queue
  109.             for _, v in pairs(fs.list(from)) do
  110.                 table.insert(queue, { fs.combine(from, v), fs.combine(to, v)})
  111.             end
  112.         else
  113.             -- Add additional files to the download queue
  114.             -- we execute them in parallel to speed everything up - the limiting
  115.             -- factor is network speed, so start them as soon as possible
  116.             table.insert(todo, function()
  117.                 local handle = fs.open(from, "r")
  118.                 if not handle then
  119.                     printError("Cannot upload " .. from)
  120.                     return
  121.                 end
  122.  
  123.                 local remote = drop.open(to, "w")
  124.                 remote.write(handle.readAll())
  125.  
  126.                 handle.close()
  127.                 remote.close()
  128.             end)
  129.         end
  130.     end
  131.  
  132.     parallel.waitForAll(unpack(todo))
  133. elseif action == "download" then
  134.     -- Pretty much the same as above
  135.     if #args < 1 or #args > 2 then
  136.         usage("download")
  137.     end
  138.  
  139.     local from = fs.combine("", args[1]):gsub("%.%./?", "")
  140.     local to = shell.resolve(args[2] or args[1])
  141.  
  142.     local isDir = drop.isDir(from)
  143.     if fs.isDir(to) and isDir then
  144.         to = fs.combine(to, fs.getName(from))
  145.     end
  146.  
  147.     local queue = { { from, to, isDir } }
  148.     local todo = {}
  149.     while #queue > 0 do
  150.         local from, to, isDir = unpack(table.remove(queue, 1))
  151.  
  152.         -- This call could be optimised by downloading files
  153.         -- straight away rather than gathering a folder tree and then downloading
  154.         if isDir then
  155.             local meta = drop.getMetadata(from)
  156.             if not meta then
  157.                 printError("Cannot read " .. from)
  158.             end
  159.  
  160.             for _, v in pairs(meta.contents) do
  161.                 -- We pass the is_dir flag as a minor optimisation so we don't check
  162.                 -- for every file
  163.                 table.insert(queue, { v.path, fs.combine(to, fs.getName(v.path)), v.is_dir})
  164.             end
  165.         else
  166.             table.insert(todo, function()
  167.                 local remote = drop.open(from, "r")
  168.                 if not remote then
  169.                     printError("Cannot download " .. from)
  170.                     return
  171.                 end
  172.                 local handle = fs.open(to, "w")
  173.                 handle.write(remote.readAll())
  174.  
  175.                 handle.close()
  176.                 remote.close()
  177.             end)
  178.         end
  179.     end
  180.  
  181.     if #todo == 0 then error("Nothing to download", 0) end
  182.     parallel.waitForAll(unpack(todo))
  183. elseif action == "list" or action == "ls" or action == "dir" then
  184.     -- Pretty basic, only difference being that
  185.     -- we get additional information to
  186.     local rows = drop.list(args[1] or "/", true)
  187.  
  188.     local files, dirs = {}, {}
  189.     for _, file in pairs(rows) do
  190.         table.insert(file.is_dir and dirs or files, fs.getName(file.path))
  191.     end
  192.  
  193.     table.sort(files)
  194.     table.sort(dirs)
  195.  
  196.     if term.isColour() then
  197.         textutils.pagedTabulate(colors.green, dirs, colors.white, files)
  198.     else
  199.         textutils.pagedTabulate(dirs, files)
  200.     end
  201. -- The reset of these are really obvious
  202. elseif action == "copy" or action == "cp" then
  203.     if #args ~= 2 then
  204.         usage("copy")
  205.     end
  206.     drop.copy(args[1], args[2])
  207. elseif action == "move" or action == "rename" or action == "mv"then
  208.     if #args ~= 2 then
  209.         usage("move")
  210.     end
  211.     drop.move(args[1], args[2])
  212. elseif action == "delete" or action == "remove" then
  213.     if #args ~= 1 then
  214.         usage("delete")
  215.     end
  216.     drop.delete(args[1])
  217. elseif action == "mkdir" then
  218.     if #args ~= 1 then
  219.         usage("mkdir")
  220.     end
  221.     drop.makeDir(args[1])
  222. else
  223.     usage()
  224. end
Advertisement
Add Comment
Please, Sign In to add comment