上传文件至 /

This commit is contained in:
2025-11-18 23:42:24 +08:00
commit 9c5c2dea64

236
filecodebox.lua Normal file
View File

@@ -0,0 +1,236 @@
-- FileCodeBox工具 for ComputerCraft
-- 类似于pastebin但用于自定义文件共享API
-- API: POST https://file.liulikeji.cn/share/file/ 用于上传
-- POST https://file.liulikeji.cn/share/select/ 用于获取文件信息
local args = {...}
local command = args[1] or "help"
-- API配置
local BASE_URL = "https://file.liulikeji.cn"
local UPLOAD_URL = BASE_URL .. "/share/file/"
local SELECT_URL = BASE_URL .. "/share/select/"
-- 辅助函数发送HTTP请求
local function httpRequest(url, method, data, headers)
local response
if method == "POST" then
if headers and headers["Content-Type"] == "application/json" then
response = http.post(url, data, headers)
else
response = http.post(url, data, headers or {})
end
else
response = http.get(url, headers or {})
end
if response then
local statusCode = response.getResponseCode()
local responseData = response.readAll()
response.close()
return statusCode, responseData
else
return nil, "Network error"
end
end
-- 辅助函数解析JSON响应
local function parseJSON(response)
local ok, data = pcall(textutils.unserialiseJSON, response)
if ok and data then
return data
end
return nil
end
-- 上传文件到FileCodeBox
local function uploadFile(filename, expireDays)
if not filename then
print("Error: Please specify filename")
return false
end
if not fs.exists(filename) then
print("Error: File '" .. filename .. "' not found")
return false
end
expireDays = expireDays or 1 -- 默认1天
local file = fs.open(filename, "r")
if not file then
print("Error: Cannot open file '" .. filename .. "'")
return false
end
local content = file.readAll()
file.close()
-- 准备multipart表单数据
local boundary = "----ComputerCraftBoundary" .. os.epoch("utc")
local body = ""
body = body .. "--" .. boundary .. "\r\n"
body = body .. "Content-Disposition: form-data; name=\"expire\"\r\n\r\n"
body = body .. tostring(expireDays * 86400) .. "\r\n"
body = body .. "--" .. boundary .. "\r\n"
body = body .. "Content-Disposition: form-data; name=\"file\"; filename=\"" .. filename .. "\"\r\n"
body = body .. "Content-Type: application/octet-stream\r\n\r\n"
body = body .. content .. "\r\n"
body = body .. "--" .. boundary .. "--\r\n"
local headers = {
["Content-Type"] = "multipart/form-data; boundary=" .. boundary,
["Content-Length"] = #body
}
-- 发送上传请求
local status, response = httpRequest(UPLOAD_URL, "POST", body, headers)
if status == 200 then
local result = parseJSON(response)
if result and result.code == 200 then
print("Upload successful")
print("Code: " .. result.detail.code)
print("Expire: " .. expireDays .. " days")
return result.detail.code
else
print("Upload failed: Invalid response")
end
else
print("Upload failed: HTTP " .. (status or "unknown"))
end
return false
end
-- 从FileCodeBox下载文件
local function downloadFile(code, outputFilename)
if not code then
print("Error: Please specify file code")
return false
end
-- 获取文件信息
local postData = textutils.serialiseJSON({code = code})
local headers = {
["Content-Type"] = "application/json",
["Content-Length"] = #postData
}
local status, response = httpRequest(SELECT_URL, "POST", postData, headers)
if status == 200 then
local result = parseJSON(response)
if result and result.code == 200 then
local fileInfo = result.detail
local downloadUrl = BASE_URL .. fileInfo.text
-- 使用原始文件名
outputFilename = outputFilename or fileInfo.name
-- 下载文件内容
local downloadStatus, content = httpRequest(downloadUrl, "GET")
if downloadStatus == 200 then
local file = fs.open(outputFilename, "w")
if file then
file.write(content)
file.close()
print("Download successful")
print("Saved as: " .. outputFilename)
return true
else
print("Error: Cannot write file")
end
else
print("Download failed: HTTP " .. (downloadStatus or "unknown"))
end
else
print("Error: File not found")
end
else
print("Request failed: HTTP " .. (status or "unknown"))
end
return false
end
-- 从FileCodeBox下载并运行代码
local function runCode(code)
if not code then
print("Error: Please specify file code")
return false
end
-- 获取文件信息
local postData = textutils.serialiseJSON({code = code})
local headers = {
["Content-Type"] = "application/json",
["Content-Length"] = #postData
}
local status, response = httpRequest(SELECT_URL, "POST", postData, headers)
if status == 200 then
local result = parseJSON(response)
if result and result.code == 200 then
local fileInfo = result.detail
local downloadUrl = BASE_URL .. fileInfo.text
-- 下载文件内容并直接执行
local downloadStatus, content = httpRequest(downloadUrl, "GET")
if downloadStatus == 200 then
local chunk, err = load(content, "downloaded_code", "t", _ENV)
if chunk then
local success, result = pcall(chunk)
if not success then
print("Runtime error: " .. tostring(result))
end
return success
else
print("Load error: " .. tostring(err))
end
else
print("Download failed: HTTP " .. (downloadStatus or "unknown"))
end
else
print("Error: File not found")
end
else
print("Request failed: HTTP " .. (status or "unknown"))
end
return false
end
-- 显示帮助信息
local function showHelp()
print("FileCodeBox Tool")
print("Usage:")
print("filecodebox put <filename> [expire_days]")
print("filecodebox get <code> [output_filename]")
print("filecodebox run <code>")
print("filecodebox help")
end
-- 主程序逻辑
if command == "put" or command == "upload" then
local filename = args[2]
local expireDays = tonumber(args[3])
uploadFile(filename, expireDays)
elseif command == "get" or command == "download" then
local code = args[2]
local outputFile = args[3]
downloadFile(code, outputFile)
elseif command == "run" or command == "execute" then
local code = args[2]
runCode(code)
else
showHelp()
end