View difference between Paste ID: shuZ5yeD and WZR1WHRx
SHOW: | | - or go back to the newest paste.
1
-- File Transfer Server over CRFv3.1
2
3
-- Packet structure:
4
-- [1] : Ident string
5
-- [2] : Action
6
-- [3] : Target
7
-- [4] : Parameter 1
8
-- [5] : Parameter 2
9
10
local args = {...}
11
12-
local filePath = "server/"
12+
local filePath = "disk/[email protected]/"
13
14
-- Standard error messages:
15
local e_noexist = textutils.serialize({"FileTransfer", "Error", "File does not exist"})
16
local e_exist = textutils.serialize({"FileTransfer", "Error", "File already exists"})
17
local e_isdir = textutils.serialize({"FileTransfer", "Error", "File is a directory"})
18
local e_hasdata = textutils.serialize({"FileTransfer", "Error", "Directory contains files"})
19
-- Not an error message:
20
local m_okay = textutils.serialize({"FileTransfer", "Okay"})
21
22-
if not LANClient then
22+
if not fs.exists("system/apis/LAN/client.exec") then
23-
	error("LAN Client API not loaded.")
23+
        print("KV LAN Software is not present on this system, attempting to download...")
24
        if http then
25
                local wHandle = http.get("http://pastebin.com/raw.php?i=NGw26QBr")
26
                if wHandle then
27
                        local fHandle = fs.open("system/apis/LAN/client.exec", "w")
28
                        if fHandle then
29
                                fHandle.write(wHandle.readAll())
30
                                fHandle.close()
31
                        else
32
                                print("Could not open executable for writing.")
33
                        end
34
                        wHandle.close()
35
                else
36
                        print("Could not connect to pastebin.")
37
                end
38
        else
39
                print("HTTP is disabled.")
40
        end
41
end
42
43
if (not ((client.exec ~= nil) or os.loadAPI("system/apis/LAN/client.exec"))) then
44
        print("LAN Software could not be found, program will now error")
45
end
46
47
while true do
48
	local sender, data = LANClient.receive()
49
	data = textutils.unserialize(data)
50
	if type(data) == "table" then
51
		if data[1] == "FileTransfer" then
52
			if data[2] == "read" then
53
				if not fs.exists(data[3]) then
54
					LANClient.send(sender, e_noexist)
55
				else
56
					if data[4] then -- Binary flag
57
						local f = fs.open(data[3], "rb")
58
						local bytes = {}
59
						local lastPause = os.clock()
60
						while true do
61
							local byte = f.read()
62
							if not byte then
63
								break
64
							else
65
								table.insert(bytes, byte)
66
							end
67
							if (os.clock() - lastPause) >= 2.90 then
68
								os.queueEvent("")
69
								os.pullEvent("")
70
								lastPause = os.clock()
71
							end
72
						end
73
						f.close()
74
						LANClient.send(sender, textutils.serialize({"FileTransfer", "data", bytes}))
75
					else
76
						local f = fs.open(data[3], "r")
77
						local text = f.readAll()
78
						f.close()
79
						LANClient.send(sender, textutils.serialize({"FileTransfer", "data", text}))
80
					end
81
				end
82
			elseif data[2] == "write" then
83
				if data[5] then
84
					local f = fs.open(data[3], "wb")
85
					local lastPause = os.clock()
86
					for i=1, #data[4] do
87
						f.write(data[4][i])
88
						if (os.clock() - lastPause) >= 2.90 then
89
							os.queueEvent("")
90
							os.pullEvent("")
91
							lastPause = os.clock()
92
						end
93
					end
94
					f.close()
95
					LANClient.send(sender, m_okay)
96
				else
97
					local f = fs.open(data[3], "w")
98
					f.write(data[4])
99
					f.close()
100
					LANClient.send(sender, m_okay)
101
				end
102
			elseif data[2] == "mkdir" then
103
				if fs.exists(data[3]) then
104
					LANClient.send(sender, e_exist)
105
				else
106
					fs.makeDir(data[3])
107
					LANClient.send(sender, m_okay)
108
				end
109
			elseif data[2] == "delete" then
110
				if not fs.exists(data[3]) then
111
					LANClient.send(sender, e_noexist)
112
				else
113
					fs.delete(data[3])
114
					LANClient.send(sender, m_okay)
115
				end
116
			elseif data[2] == "list" then
117
				if not fs.exists(data[3]) then
118
					LANClient.send(sender, e_noexist)
119
				else
120
					LANClient.send(sender, textutils.serialize({"FileTransfer", "list", fs.list(data[3])}))
121
				end
122
			end
123
		end
124
	end
125
end