2
0

screenshots.lua 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. --Luanti
  2. --Copyright (C) 2023-24 rubenwardy
  3. --
  4. --This program is free software; you can redistribute it and/or modify
  5. --it under the terms of the GNU Lesser General Public License as published by
  6. --the Free Software Foundation; either version 2.1 of the License, or
  7. --(at your option) any later version.
  8. --
  9. --This program is distributed in the hope that it will be useful,
  10. --but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. --GNU Lesser General Public License for more details.
  13. --
  14. --You should have received a copy of the GNU Lesser General Public License along
  15. --with this program; if not, write to the Free Software Foundation, Inc.,
  16. --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. -- Screenshot
  18. local screenshot_dir = core.get_cache_path() .. DIR_DELIM .. "cdb"
  19. assert(core.create_dir(screenshot_dir))
  20. local screenshot_downloading = {}
  21. local screenshot_downloaded = {}
  22. local function get_filename(path)
  23. local parts = path:split("/")
  24. return parts[#parts]
  25. end
  26. local function get_file_extension(path)
  27. local parts = path:split(".")
  28. return parts[#parts]
  29. end
  30. function get_screenshot(package, screenshot_url, level)
  31. if not screenshot_url then
  32. return defaulttexturedir .. "no_screenshot.png"
  33. end
  34. -- Luanti only supports png and jpg
  35. local ext = get_file_extension(screenshot_url)
  36. if ext ~= "png" and ext ~= "jpg" then
  37. screenshot_url = screenshot_url:sub(0, -#ext - 1) .. "png"
  38. end
  39. -- Set thumbnail level
  40. screenshot_url = screenshot_url:gsub("/thumbnails/[0-9]+/", "/thumbnails/" .. level .. "/")
  41. screenshot_url = screenshot_url:gsub("/uploads/", "/thumbnails/" .. level .. "/")
  42. if screenshot_downloading[screenshot_url] then
  43. return defaulttexturedir .. "loading_screenshot.png"
  44. end
  45. local filepath = screenshot_dir .. DIR_DELIM ..
  46. ("%s-%s-%s-l%d-%s"):format(package.type, package.author, package.name,
  47. level, get_filename(screenshot_url))
  48. -- Return if already downloaded
  49. local file = io.open(filepath, "r")
  50. if file then
  51. file:close()
  52. return filepath
  53. end
  54. -- Show error if we've failed to download before
  55. if screenshot_downloaded[screenshot_url] then
  56. return defaulttexturedir .. "error_screenshot.png"
  57. end
  58. -- Download
  59. local function download_screenshot(params)
  60. return core.download_file(params.url, params.dest)
  61. end
  62. local function callback(success)
  63. screenshot_downloading[screenshot_url] = nil
  64. screenshot_downloaded[screenshot_url] = true
  65. if not success then
  66. core.log("warning", "Screenshot download failed for some reason")
  67. end
  68. ui.update()
  69. end
  70. if core.handle_async(download_screenshot,
  71. { dest = filepath, url = screenshot_url }, callback) then
  72. screenshot_downloading[screenshot_url] = true
  73. else
  74. core.log("error", "ERROR: async event failed")
  75. return defaulttexturedir .. "error_screenshot.png"
  76. end
  77. return defaulttexturedir .. "loading_screenshot.png"
  78. end