|
18 | 18 | local store = {}
|
19 | 19 | local package_dialog = {}
|
20 | 20 |
|
| 21 | +-- Screenshot |
| 22 | +local screenshot_dir = os.tempfolder() |
| 23 | +assert(core.create_dir(screenshot_dir)) |
| 24 | +local screenshot_downloading = {} |
| 25 | +local screenshot_downloaded = {} |
| 26 | + |
| 27 | +-- Filter |
21 | 28 | local search_string = ""
|
22 | 29 | local cur_page = 1
|
23 | 30 | local num_per_page = 5
|
@@ -137,6 +144,58 @@ local function start_install(calling_dialog, package)
|
137 | 144 | new_dlg:show()
|
138 | 145 | end
|
139 | 146 |
|
| 147 | +local function get_screenshot(package) |
| 148 | + if #package.screenshots == 0 then |
| 149 | + return defaulttexturedir .. "no_screenshot.png" |
| 150 | + elseif screenshot_downloading[package.screenshots[1]] then |
| 151 | + return defaulttexturedir .. "loading_screenshot.png" |
| 152 | + end |
| 153 | + |
| 154 | + -- Get tmp screenshot path |
| 155 | + local filepath = screenshot_dir .. DIR_DELIM .. |
| 156 | + package.type .. "-" .. package.author .. "-" .. package.name .. ".png" |
| 157 | + |
| 158 | + -- Return if already downloaded |
| 159 | + local file = io.open(filepath, "r") |
| 160 | + if file then |
| 161 | + file:close() |
| 162 | + return filepath |
| 163 | + end |
| 164 | + |
| 165 | + -- Show error if we've failed to download before |
| 166 | + if screenshot_downloaded[package.screenshots[1]] then |
| 167 | + return defaulttexturedir .. "error_screenshot.png" |
| 168 | + end |
| 169 | + |
| 170 | + -- Download |
| 171 | + |
| 172 | + local function download_screenshot(params) |
| 173 | + return core.download_file(params.url, params.dest) |
| 174 | + end |
| 175 | + local function callback(success) |
| 176 | + screenshot_downloading[package.screenshots[1]] = nil |
| 177 | + screenshot_downloaded[package.screenshots[1]] = true |
| 178 | + if not success then |
| 179 | + core.log("warning", "Screenshot download failed for some reason") |
| 180 | + end |
| 181 | + |
| 182 | + local ele = ui.childlist.store |
| 183 | + if ele and not ele.hidden then |
| 184 | + core.update_formspec(ele:formspec()) |
| 185 | + end |
| 186 | + end |
| 187 | + if core.handle_async(download_screenshot, |
| 188 | + { dest = filepath, url = package.screenshots[1] }, callback) then |
| 189 | + screenshot_downloading[package.screenshots[1]] = true |
| 190 | + else |
| 191 | + core.log("error", "ERROR: async event failed") |
| 192 | + return defaulttexturedir .. "error_screenshot.png" |
| 193 | + end |
| 194 | + |
| 195 | + return defaulttexturedir .. "loading_screenshot.png" |
| 196 | +end |
| 197 | + |
| 198 | + |
140 | 199 |
|
141 | 200 | function package_dialog.get_formspec()
|
142 | 201 | local package = package_dialog.package
|
@@ -315,8 +374,7 @@ function store.get_formspec()
|
315 | 374 |
|
316 | 375 | -- image
|
317 | 376 | formspec[#formspec + 1] = "image[-0.4,0;1.5,1;"
|
318 |
| - formspec[#formspec + 1] = defaulttexturedir |
319 |
| - formspec[#formspec + 1] = "no_screenshot.png" |
| 377 | + formspec[#formspec + 1] = get_screenshot(package) |
320 | 378 | formspec[#formspec + 1] = "]"
|
321 | 379 |
|
322 | 380 | -- title
|
|
0 commit comments