comparison libervia/pages/files/list/page_meta.py @ 1325:009542289bc9

pages (files/list): removed/updated code to download thumbnails and sort them as it is now done by the backend
author Goffi <goffi@goffi.org>
date Fri, 14 Aug 2020 09:09:03 +0200
parents d0575e9abf7d
children 089742e065e3
comparison
equal deleted inserted replaced
1324:898442c4ff68 1325:009542289bc9
1 #!/usr/bin/env python3 1 #!/usr/bin/env python3
2 2
3 import json 3 import json
4 import os 4 import os
5 from pathlib import Path 5 from pathlib import Path
6 from sat.core.i18n import _
7 from sat.core.log import getLogger 6 from sat.core.log import getLogger
8 from sat.tools.common import uri 7 from sat.tools.common import uri
9 from sat_frontends.bridge.bridge_frontend import BridgeException 8 from sat_frontends.bridge.bridge_frontend import BridgeException
10 from libervia.server.constants import Const as C 9 from libervia.server.constants import Const as C
11 from libervia.server import session_iface 10 from libervia.server import session_iface
56 if file_data["type"] == C.FILE_TYPE_DIRECTORY: 55 if file_data["type"] == C.FILE_TYPE_DIRECTORY:
57 page = self 56 page = self
58 elif file_data["type"] == C.FILE_TYPE_FILE: 57 elif file_data["type"] == C.FILE_TYPE_FILE:
59 page = self.getPageByName("files_view") 58 page = self.getPageByName("files_view")
60 59
61 ## thumbnails ## 60 # we set URL for the last thumbnail which has a size below thumb_limit
62 try: 61 try:
63 thumbnails = file_data["extra"]["thumbnails"] 62 thumbnails = file_data["extra"]["thumbnails"]
64 if not thumbnails:
65 raise KeyError
66 except KeyError:
67 pass
68 else:
69 thumbnails.sort(key=lambda t: t["size"])
70 thumb = thumbnails[0] 63 thumb = thumbnails[0]
71 for thumb_data in thumbnails: 64 for thumb_data in thumbnails:
72 if thumb_data["size"][0] > thumb_limit: 65 if thumb_data["size"][0] > thumb_limit:
73 break 66 break
74 thumb = thumb_data 67 thumb = thumb_data
75 if "url" in thumb: 68 file_data["thumb_url"] = (
76 file_data["thumb_url"] = thumb["url"] 69 thumb.get("url")
77 elif "id" in thumb: 70 or os.path.join(session_data.cache_dir, thumb["filename"])
78 try: 71 )
79 thumb_path = await self.host.bridgeCall( 72 except (KeyError, IndexError):
80 "bobGetFile", service.full(), thumb["id"], profile 73 pass
81 )
82 except Exception as e:
83 log.warning(
84 _("Can't retrieve thumbnail: {reason}").format(reason=e)
85 )
86 else:
87 filename = os.path.basename(thumb_path)
88 file_data["thumb_url"] = os.path.join(
89 session_data.cache_dir, filename
90 )
91 else: 74 else:
92 raise ValueError( 75 raise ValueError(
93 "unexpected file type: {file_type}".format(file_type=file_data["type"]) 76 "unexpected file type: {file_type}".format(file_type=file_data["type"])
94 ) 77 )
95 file_data["url"] = page.getURL(service.full(), *dir_path) 78 file_data["url"] = page.getURL(service.full(), *dir_path)