Skip to content

Commit a1d77ab

Browse files
committed
Merge pull request dan200#536 from Luca0208/ComputerCraft/master
Make wget automatically determine the file name.
1 parent c8db671 commit a1d77ab

File tree

2 files changed

+21
-15
lines changed
  • src/main/resources/assets/computercraft/lua/rom

2 files changed

+21
-15
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
wget is a program for downloading files from the internet. This is useful for downloading programs created by other players.
2+
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.
23
The HTTP API must be enabled in ComputerCraft.cfg to use this program.
3-
44
ex:
55
"wget http://pastebin.com/raw/CxaWmPrX test" will download the file from the URL http://pastebin.com/raw/CxaWmPrX, and save it as "test".
6+
"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"
7+
"wget http://example.org/" will download the file from the URL http://example.org and save it as "example.org"

src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

22
local function printUsage()
33
print( "Usage:" )
4-
print( "wget <url> <filename>" )
4+
print( "wget <url> [filename]" )
55
end
66

77
local tArgs = { ... }
8-
if #tArgs < 2 then
8+
if #tArgs < 1 then
99
printUsage()
1010
return
1111
end
@@ -15,19 +15,15 @@ if not http then
1515
printError( "Set http_enable to true in ComputerCraft.cfg" )
1616
return
1717
end
18-
18+
19+
local function getFilename( sUrl )
20+
sUrl = sUrl:gsub( "[#?].*" , "" ):gsub( "/+$" , "" )
21+
return sUrl:match( "/([^/]+)$" )
22+
end
23+
1924
local function get( sUrl )
2025
write( "Connecting to " .. sUrl .. "... " )
2126

22-
local ok, err = http.checkURL( sUrl )
23-
if not ok then
24-
print( "Failed." )
25-
if err then
26-
printError( err )
27-
end
28-
return nil
29-
end
30-
3127
local response = http.get( sUrl , nil , true )
3228
if not response then
3329
print( "Failed." )
@@ -40,10 +36,18 @@ local function get( sUrl )
4036
response.close()
4137
return sResponse
4238
end
43-
39+
4440
-- Determine file to download
4541
local sUrl = tArgs[1]
46-
local sFile = tArgs[2]
42+
43+
--Check if the URL is valid
44+
local ok, err = http.checkURL( sUrl )
45+
if not ok then
46+
printError( err or "Invalid URL." )
47+
return
48+
end
49+
50+
local sFile = tArgs[2] or getFilename( sUrl )
4751
local sPath = shell.resolve( sFile )
4852
if fs.exists( sPath ) then
4953
print( "File already exists" )

0 commit comments

Comments
 (0)