1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-12 19:20:29 +00:00

Merge pull request #536 from Luca0208/ComputerCraft/master

Make wget automatically determine the file name.
This commit is contained in:
SquidDev 2018-04-06 21:08:54 +01:00
parent c8db671409
commit a1d77ab8e7
2 changed files with 21 additions and 15 deletions

View File

@ -1,5 +1,7 @@
wget is a program for downloading files from the internet. This is useful for downloading programs created by other players. wget is a program for downloading files from the internet. This is useful for downloading programs created by other players.
If no filename is specified wget will try to determine the filename from the URL by stripping any anchors, parameters and trailing slashes and then taking everything remaining after the last slash.
The HTTP API must be enabled in ComputerCraft.cfg to use this program. The HTTP API must be enabled in ComputerCraft.cfg to use this program.
ex: ex:
"wget http://pastebin.com/raw/CxaWmPrX test" will download the file from the URL http://pastebin.com/raw/CxaWmPrX, and save it as "test". "wget http://pastebin.com/raw/CxaWmPrX test" will download the file from the URL http://pastebin.com/raw/CxaWmPrX, and save it as "test".
"wget http://example.org/test.lua/?foo=bar#qzu" will download the file from the URL http://example.org/test.lua/?foo=bar#qzu and save it as "test.lua"
"wget http://example.org/" will download the file from the URL http://example.org and save it as "example.org"

View File

@ -1,11 +1,11 @@
local function printUsage() local function printUsage()
print( "Usage:" ) print( "Usage:" )
print( "wget <url> <filename>" ) print( "wget <url> [filename]" )
end end
local tArgs = { ... } local tArgs = { ... }
if #tArgs < 2 then if #tArgs < 1 then
printUsage() printUsage()
return return
end end
@ -16,18 +16,14 @@ if not http then
return return
end end
local function getFilename( sUrl )
sUrl = sUrl:gsub( "[#?].*" , "" ):gsub( "/+$" , "" )
return sUrl:match( "/([^/]+)$" )
end
local function get( sUrl ) local function get( sUrl )
write( "Connecting to " .. sUrl .. "... " ) write( "Connecting to " .. sUrl .. "... " )
local ok, err = http.checkURL( sUrl )
if not ok then
print( "Failed." )
if err then
printError( err )
end
return nil
end
local response = http.get( sUrl , nil , true ) local response = http.get( sUrl , nil , true )
if not response then if not response then
print( "Failed." ) print( "Failed." )
@ -43,7 +39,15 @@ end
-- Determine file to download -- Determine file to download
local sUrl = tArgs[1] local sUrl = tArgs[1]
local sFile = tArgs[2]
--Check if the URL is valid
local ok, err = http.checkURL( sUrl )
if not ok then
printError( err or "Invalid URL." )
return
end
local sFile = tArgs[2] or getFilename( sUrl )
local sPath = shell.resolve( sFile ) local sPath = shell.resolve( sFile )
if fs.exists( sPath ) then if fs.exists( sPath ) then
print( "File already exists" ) print( "File already exists" )