1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-31 18:31:04 +00:00

ContentDB redesign: Add package dialog

Co-authored-by: Gregor Parzefall <gregor.parzefall@posteo.de>
This commit is contained in:
rubenwardy 2024-03-31 19:24:27 +01:00
parent 05cbd84ae0
commit 78aab8c95d
12 changed files with 529 additions and 130 deletions

View file

@ -23,23 +23,40 @@ local screenshot_downloading = {}
local screenshot_downloaded = {}
local function get_filename(path)
local parts = path:split("/")
return parts[#parts]
end
local function get_file_extension(path)
local parts = path:split(".")
return parts[#parts]
end
function get_screenshot(package)
if not package.thumbnail then
function get_screenshot(package, screenshot_url, level)
if not screenshot_url then
return defaulttexturedir .. "no_screenshot.png"
elseif screenshot_downloading[package.thumbnail] then
end
-- Minetest only supports png and jpg
local ext = get_file_extension(screenshot_url)
if ext ~= "png" and ext ~= "jpg" then
screenshot_url = screenshot_url:sub(0, -#ext - 1) .. "png"
end
-- Set thumbnail level
screenshot_url = screenshot_url:gsub("/thumbnails/[0-9]+/", "/thumbnails/" .. level .. "/")
screenshot_url = screenshot_url:gsub("/uploads/", "/thumbnails/" .. level .. "/")
if screenshot_downloading[screenshot_url] then
return defaulttexturedir .. "loading_screenshot.png"
end
-- Get tmp screenshot path
local ext = get_file_extension(package.thumbnail)
local filepath = screenshot_dir .. DIR_DELIM ..
("%s-%s-%s.%s"):format(package.type, package.author, package.name, ext)
("%s-%s-%s-l%d-%s"):format(package.type, package.author, package.name,
level, get_filename(screenshot_url))
-- Return if already downloaded
local file = io.open(filepath, "r")
@ -49,7 +66,7 @@ function get_screenshot(package)
end
-- Show error if we've failed to download before
if screenshot_downloaded[package.thumbnail] then
if screenshot_downloaded[screenshot_url] then
return defaulttexturedir .. "error_screenshot.png"
end
@ -59,16 +76,16 @@ function get_screenshot(package)
return core.download_file(params.url, params.dest)
end
local function callback(success)
screenshot_downloading[package.thumbnail] = nil
screenshot_downloaded[package.thumbnail] = true
screenshot_downloading[screenshot_url] = nil
screenshot_downloaded[screenshot_url] = true
if not success then
core.log("warning", "Screenshot download failed for some reason")
end
ui.update()
end
if core.handle_async(download_screenshot,
{ dest = filepath, url = package.thumbnail }, callback) then
screenshot_downloading[package.thumbnail] = true
{ dest = filepath, url = screenshot_url }, callback) then
screenshot_downloading[screenshot_url] = true
else
core.log("error", "ERROR: async event failed")
return defaulttexturedir .. "error_screenshot.png"