1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-09-28 15:08:47 +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.
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.
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://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()
print( "Usage:" )
print( "wget <url> <filename>" )
print( "wget <url> [filename]" )
end
local tArgs = { ... }
if #tArgs < 2 then
if #tArgs < 1 then
printUsage()
return
end
@ -15,19 +15,15 @@ if not http then
printError( "Set http_enable to true in ComputerCraft.cfg" )
return
end
local function getFilename( sUrl )
sUrl = sUrl:gsub( "[#?].*" , "" ):gsub( "/+$" , "" )
return sUrl:match( "/([^/]+)$" )
end
local function get( 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 )
if not response then
print( "Failed." )
@ -40,10 +36,18 @@ local function get( sUrl )
response.close()
return sResponse
end
-- Determine file to download
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 )
if fs.exists( sPath ) then
print( "File already exists" )