download file url fix

This commit is contained in:
2026-08-21 23:52:06 +02:00
parent 7403d1a1f4
commit 9286509d31
+22 -17
View File
@@ -67,7 +67,8 @@ local protocols = { "http://", "https://" }
local ending = ".git"
---@param url string
local function parseGiteaURL(url)
local info = {
---@class urlinfo
local urlInfo = {
protocol = "",
origin = "",
owner = "",
@@ -79,33 +80,37 @@ local function parseGiteaURL(url)
if not string.endsWith(lowerUrl, ending) then error("A gitea link needs to end with " .. ending) end
for _, proto in pairs(protocols) do
if string.beginsWith(lowerUrl, proto) then
info.protocol = proto
urlInfo.protocol = proto
break
end
end
local urlLen = string.len(url)
local protoLen = string.len(info.protocol)
local protoLen = string.len(urlInfo.protocol)
local endLen = string.len(ending)
local cleaned = string.sub(url, protoLen + 1, urlLen - endLen)
local urlParts = string.split(cleaned, "/")
if not (#urlParts) == 3 then error("Unexpected link length.") end
info.origin, info.owner, info.repo = urlParts[1], urlParts[2], urlParts[3]
urlInfo.origin, urlInfo.owner, urlInfo.repo = urlParts[1], urlParts[2], urlParts[3]
info.treeUrl =
urlInfo.treeUrl =
("%s%s/api/v1/repos/%s/%s/git/trees/%s?recursive=1")
:format(info.protocol, info.origin, info.owner, info.repo, info.branch)
for k, v in pairs(info) do
:format(urlInfo.protocol, urlInfo.origin, urlInfo.owner, urlInfo.repo, urlInfo.branch)
for k, v in pairs(urlInfo) do
if v == "" or v == nil then
error("Could not parse " .. k .. " from link.")
end
end
print("treeUrl: ", info.treeUrl)
return info
print("treeUrl: ", urlInfo.treeUrl)
return urlInfo
end
local function downloadFile(base, owner, repo, branch, path, target)
local url = ("%s/api/v1/repos/%s/%s/raw/%s/%s"):format(base, owner, repo, branch, path)
---@param urlInfo urlinfo
---@param path string
---@param target string
local function downloadFile(urlInfo, path, target)
local url = ("%s%s/api/v1/repos/%s/%s/raw/%s/%s"):format(urlInfo.protocol, urlInfo.origin, urlInfo.owner,
urlInfo.repo, urlInfo.branch, path)
local req = { url = url }
if GIT_TOKEN ~= "" then
@@ -128,14 +133,14 @@ local function downloadFile(base, owner, repo, branch, path, target)
end
local function cloneRepo(url)
local info = parseGiteaURL(url)
local urlInfo = parseGiteaURL(url)
-- if not info then error(err) end
print("Cloning: " .. info.owner .. "/" .. info.repo .. "@" .. info.branch)
print("Cloning: " .. urlInfo.owner .. "/" .. urlInfo.repo .. "@" .. urlInfo.branch)
-- local treeUrl = ("%s/api/v1/repos/%s/%s/git/trees/%s?recursive=1"):format(info.base, info.owner, info.repo,info.branch)
local req = { url = info.treeUrl }
local req = { url = urlInfo.treeUrl }
if GIT_TOKEN ~= "" then
req.headers = { Authorization = "token " .. GIT_TOKEN }
end
@@ -152,17 +157,17 @@ local function cloneRepo(url)
local count = 0
for _, node in ipairs(tree.tree) do
if node.type == "blob" then
local localPath = ("git/%s/%s/%s"):format(info.owner, info.repo, node.path)
local localPath = ("git/%s/%s/%s"):format(urlInfo.owner, urlInfo.repo, node.path)
local dir = fs.getDir(localPath)
if not fs.isDir(dir) then fs.makeDir(dir) end
downloadFile(info.base, info.owner, info.repo, info.branch, node.path, localPath)
downloadFile(urlInfo, node.path, localPath)
count = count + 1
end
end
print(("Cloned %d files to /home/git/%s/%s"):format(count, info.owner, info.repo))
print(("Cloned %d files to /home/git/%s/%s"):format(count, urlInfo.owner, urlInfo.repo))
end
local args = { ... }