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