· 5 years ago · May 08, 2020, 10:26 PM
1
2local function printUsage()
3 print( "Usages:" )
4 print( "pastebin put <filename>" )
5 print( "pastebin get <code> <filename>" )
6end
7
8local tArgs = { ... }
9if #tArgs < 2 then
10 printUsage()
11 return
12end
13
14if not http then
15 print( "Pastebin requires http API" )
16 print( "Set enableAPI_http to 1 in mod_ComputerCraft.cfg" )
17 return
18end
19
20local sCommand = tArgs[1]
21if sCommand == "put" then
22 -- Upload a file to pastebin.com
23 -- Determine file to upload
24 local sFile = tArgs[2]
25 local sPath = shell.resolve( sFile )
26 if not fs.exists( sPath ) or fs.isDir( sPath ) then
27 print( "No such file" )
28 return
29 end
30
31 -- Read in the file
32 local sName = fs.getName( sPath )
33 local file = fs.open( sPath, "r" )
34 local sText = file.readAll()
35 file.close()
36
37 -- POST the contents to pastebin
38 write( "Connecting to pastebin.com... " )
39 local key = "0ec2eb25b6166c0c27a394ae118ad829"
40 local response = http.post(
41 "http://pastebin.com/api/api_post.php",
42 "api_option=paste&"..
43 "api_dev_key="..key.."&"..
44 "api_paste_format=lua&"..
45 "api_paste_name="..textutils.urlEncode(sName).."&"..
46 "api_paste_code="..textutils.urlEncode(sText)
47 )
48
49 if response then
50 print( "Success." )
51
52 local sResponse = response.readAll()
53 response.close()
54
55 local sCode = string.match( sResponse, "[^/]+$" )
56 print( "Uploaded as "..sResponse )
57 print( "Run \"pastebin get "..sCode.."\" to download anywhere" )
58
59 else
60 print( "Failed." )
61 end
62
63elseif sCommand == "get" then
64 -- Download a file from pastebin.com
65 if #tArgs < 3 then
66 printUsage()
67 return
68 end
69
70 -- Determine file to download
71 local sCode = tArgs[2]
72 local sFile = tArgs[3]
73 local sPath = shell.resolve( sFile )
74 if fs.exists( sPath ) then
75 print( "File already exists" )
76 return
77 end
78
79 -- GET the contents from pastebin
80 write( "Connecting to pastebin.com... " )
81 local response = http.get(
82 "http://pastebin.com/raw.php?i="..textutils.urlEncode( sCode )
83 )
84
85 if response then
86 print( "Success." )
87
88 local sResponse = response.readAll()
89 response.close()
90
91 local file = fs.open( sPath, "w" )
92 file.write( sResponse )
93 file.close()
94
95 print( "Downloaded as "..sFile )
96
97 else
98 print( "Failed." )
99 end
100
101else
102 printUsage()
103 return
104end