quasarr 1.31.0__py3-none-any.whl → 2.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of quasarr might be problematic. Click here for more details.

@@ -0,0 +1,250 @@
1
+ # -*- coding: utf-8 -*-
2
+ # Quasarr
3
+ # Project by https://github.com/rix1337
4
+
5
+ import base64
6
+ import hashlib
7
+ import hmac
8
+ import os
9
+ import time
10
+
11
+ from bottle import request, response, redirect
12
+
13
+ import quasarr.providers.html_images as images
14
+ from quasarr.providers.version import get_version
15
+
16
+ # Auth configuration from environment
17
+ AUTH_USER = os.environ.get('USER', '')
18
+ AUTH_PASS = os.environ.get('PASS', '')
19
+ AUTH_TYPE = os.environ.get('AUTH', '').lower()
20
+
21
+ # Cookie settings
22
+ COOKIE_NAME = 'quasarr_session'
23
+ COOKIE_MAX_AGE = 30 * 24 * 60 * 60 # 30 days
24
+
25
+
26
+ def is_auth_enabled():
27
+ """Check if authentication is enabled (both USER and PASS set)."""
28
+ return bool(AUTH_USER and AUTH_PASS)
29
+
30
+
31
+ def is_form_auth():
32
+ """Check if form-based auth is enabled."""
33
+ return AUTH_TYPE == 'form'
34
+
35
+
36
+ def _sign_cookie(user, expiry):
37
+ """Create HMAC signature for cookie."""
38
+ msg = f"{user}:{expiry}".encode()
39
+ return hmac.new(AUTH_PASS.encode(), msg, hashlib.sha256).hexdigest()
40
+
41
+
42
+ def _create_session_cookie(user):
43
+ """Create a signed session cookie value."""
44
+ expiry = int(time.time()) + COOKIE_MAX_AGE
45
+ signature = _sign_cookie(user, expiry)
46
+ return f"{user}:{expiry}:{signature}"
47
+
48
+
49
+ def _verify_session_cookie(cookie_value):
50
+ """Verify a session cookie. Returns True if valid."""
51
+ try:
52
+ parts = cookie_value.split(':')
53
+ if len(parts) != 3:
54
+ return False
55
+ user, expiry, signature = parts
56
+ expiry = int(expiry)
57
+ if time.time() > expiry:
58
+ return False
59
+ expected_sig = _sign_cookie(user, expiry)
60
+ return hmac.compare_digest(signature, expected_sig)
61
+ except:
62
+ return False
63
+
64
+
65
+ def check_basic_auth():
66
+ """Check HTTP Basic Auth header. Returns True if valid."""
67
+ auth = request.headers.get('Authorization', '')
68
+ if not auth.startswith('Basic '):
69
+ return False
70
+ try:
71
+ decoded = base64.b64decode(auth[6:]).decode('utf-8')
72
+ user, passwd = decoded.split(':', 1)
73
+ return user == AUTH_USER and passwd == AUTH_PASS
74
+ except:
75
+ return False
76
+
77
+
78
+ def check_form_auth():
79
+ """Check session cookie. Returns True if valid."""
80
+ cookie = request.get_cookie(COOKIE_NAME)
81
+ return cookie and _verify_session_cookie(cookie)
82
+
83
+
84
+ def require_basic_auth():
85
+ """Send 401 response for Basic Auth."""
86
+ response.status = 401
87
+ response.set_header('WWW-Authenticate', 'Basic realm="Quasarr"')
88
+ return "Authentication required"
89
+
90
+
91
+ def _render_login_page(error=None):
92
+ """Render login form page using Quasarr styling."""
93
+ error_html = f'<p style="color: #dc3545; margin-bottom: 1rem;"><b>{error}</b></p>' if error else ''
94
+ next_url = request.query.get('next', '/')
95
+
96
+ # Inline the centered HTML to avoid circular import
97
+ return f'''<html>
98
+ <head>
99
+ <meta charset="utf-8">
100
+ <meta name="viewport" content="width=device-width, initial-scale=1">
101
+ <title>Quasarr - Login</title>
102
+ <link rel="icon" href="{images.logo}" type="image/png">
103
+ <style>
104
+ :root {{
105
+ --bg-color: #ffffff;
106
+ --fg-color: #212529;
107
+ --card-bg: #ffffff;
108
+ --card-shadow: rgba(0, 0, 0, 0.1);
109
+ --primary: #0d6efd;
110
+ --secondary: #6c757d;
111
+ --spacing: 1rem;
112
+ }}
113
+ @media (prefers-color-scheme: dark) {{
114
+ :root {{
115
+ --bg-color: #181a1b;
116
+ --fg-color: #f1f1f1;
117
+ --card-bg: #242526;
118
+ --card-shadow: rgba(0, 0, 0, 0.5);
119
+ }}
120
+ }}
121
+ *, *::before, *::after {{ box-sizing: border-box; }}
122
+ html, body {{
123
+ margin: 0; padding: 0; width: 100%; height: 100%;
124
+ background-color: var(--bg-color); color: var(--fg-color);
125
+ font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
126
+ line-height: 1.6; display: flex; flex-direction: column; min-height: 100vh;
127
+ }}
128
+ .outer {{ flex: 1; display: flex; justify-content: center; align-items: center; padding: var(--spacing); }}
129
+ .inner {{
130
+ background-color: var(--card-bg); border-radius: 1rem;
131
+ box-shadow: 0 0.5rem 1.5rem var(--card-shadow);
132
+ padding: calc(var(--spacing) * 2); text-align: center; width: 100%; max-width: 400px;
133
+ }}
134
+ .logo {{ height: 64px; width: 64px; vertical-align: middle; margin-right: 0.5rem; }}
135
+ h1 {{ margin: 0 0 1rem 0; font-size: 1.75rem; }}
136
+ h2 {{ margin: 0 0 1.5rem 0; font-size: 1.25rem; font-weight: 500; }}
137
+ label {{ display: block; text-align: left; margin-bottom: 0.25rem; font-weight: 500; }}
138
+ input[type="text"], input[type="password"] {{
139
+ width: 100%; padding: 0.5rem; margin-bottom: 1rem;
140
+ border: 1px solid var(--secondary); border-radius: 0.5rem;
141
+ font-size: 1rem; background: var(--card-bg); color: var(--fg-color);
142
+ }}
143
+ .btn-primary {{
144
+ background-color: var(--primary); color: #fff; border: 1.5px solid #0856c7;
145
+ padding: 0.5rem 1.5rem; font-size: 1rem; border-radius: 0.5rem;
146
+ font-weight: 500; cursor: pointer; width: 100%;
147
+ }}
148
+ .btn-primary:hover {{ background-color: #0b5ed7; }}
149
+ footer {{ text-align: center; font-size: 0.75rem; color: var(--secondary); padding: 0.5rem 0; }}
150
+ </style>
151
+ </head>
152
+ <body>
153
+ <div class="outer">
154
+ <div class="inner">
155
+ <h1><img src="{images.logo}" class="logo" alt="Quasarr"/>Quasarr</h1>
156
+ {error_html}
157
+ <form method="post" action="/login">
158
+ <input type="hidden" name="next" value="{next_url}">
159
+ <label for="username">Username</label>
160
+ <input type="text" id="username" name="username" autocomplete="username" required>
161
+ <label for="password">Password</label>
162
+ <input type="password" id="password" name="password" autocomplete="current-password" required>
163
+ <button type="submit" class="btn-primary">Login</button>
164
+ </form>
165
+ </div>
166
+ </div>
167
+ <footer>Quasarr v.{get_version()}</footer>
168
+ </body>
169
+ </html>'''
170
+
171
+
172
+ def _handle_login_post():
173
+ """Handle login form submission."""
174
+ username = request.forms.get('username', '')
175
+ password = request.forms.get('password', '')
176
+ next_url = request.forms.get('next', '/')
177
+
178
+ if username == AUTH_USER and password == AUTH_PASS:
179
+ cookie_value = _create_session_cookie(username)
180
+ response.set_cookie(COOKIE_NAME, cookie_value, max_age=COOKIE_MAX_AGE, path='/', httponly=True)
181
+ redirect(next_url)
182
+ else:
183
+ return _render_login_page(error="Invalid username or password")
184
+
185
+
186
+ def _handle_logout():
187
+ """Handle logout - clear cookie and redirect to login."""
188
+ response.delete_cookie(COOKIE_NAME, path='/')
189
+ redirect('/login')
190
+
191
+
192
+ def show_logout_link():
193
+ """Returns True if logout link should be shown (form auth enabled and authenticated)."""
194
+ return is_auth_enabled() and is_form_auth() and check_form_auth()
195
+
196
+
197
+ def add_auth_routes(app):
198
+ """Add login/logout routes to a Bottle app (for form auth only)."""
199
+ if not is_auth_enabled():
200
+ return
201
+
202
+ if is_form_auth():
203
+ @app.get('/login')
204
+ def login_get():
205
+ if check_form_auth():
206
+ redirect('/')
207
+ return _render_login_page()
208
+
209
+ @app.post('/login')
210
+ def login_post():
211
+ return _handle_login_post()
212
+
213
+ @app.get('/logout')
214
+ def logout():
215
+ return _handle_logout()
216
+
217
+
218
+ def add_auth_hook(app, whitelist_prefixes=None):
219
+ """Add authentication hook to a Bottle app.
220
+
221
+ Args:
222
+ app: Bottle application
223
+ whitelist_prefixes: List of path prefixes to skip auth (e.g., ['/api/', '/sponsors_helper/'])
224
+ """
225
+ if whitelist_prefixes is None:
226
+ whitelist_prefixes = []
227
+
228
+ @app.hook('before_request')
229
+ def auth_hook():
230
+ if not is_auth_enabled():
231
+ return
232
+
233
+ path = request.path
234
+
235
+ # Always allow login/logout
236
+ if path in ['/login', '/logout']:
237
+ return
238
+
239
+ # Check whitelist prefixes
240
+ for prefix in whitelist_prefixes:
241
+ if path.startswith(prefix):
242
+ return
243
+
244
+ # Check authentication
245
+ if is_form_auth():
246
+ if not check_form_auth():
247
+ redirect(f'/login?next={path}')
248
+ else:
249
+ if not check_basic_auth():
250
+ return require_basic_auth()
@@ -2,8 +2,18 @@
2
2
  # Quasarr
3
3
  # Project by https://github.com/rix1337
4
4
 
5
+ from quasarr.providers.log import debug
5
6
  from quasarr.providers.myjd_api import TokenExpiredException, RequestTimeoutException, MYJDException
6
7
 
8
+ # Known archive extensions for fallback detection
9
+ ARCHIVE_EXTENSIONS = frozenset([
10
+ '.rar', '.zip', '.7z', '.tar', '.gz', '.bz2', '.xz',
11
+ '.001', '.002', '.003', '.004', '.005', '.006', '.007', '.008', '.009',
12
+ '.r00', '.r01', '.r02', '.r03', '.r04', '.r05', '.r06', '.r07', '.r08', '.r09',
13
+ '.part1.rar', '.part01.rar', '.part001.rar',
14
+ '.part2.rar', '.part02.rar', '.part002.rar',
15
+ ])
16
+
7
17
 
8
18
  class JDPackageCache:
9
19
  """
@@ -16,116 +26,264 @@ class JDPackageCache:
16
26
 
17
27
  This reduces redundant API calls within a single operation where the same
18
28
  data (e.g., linkgrabber_links) is needed multiple times.
19
-
20
- Usage:
21
- # Cache is created and discarded within a single function call
22
- cache = JDPackageCache(device)
23
- packages = cache.linkgrabber_packages # Fetches from API
24
- packages = cache.linkgrabber_packages # Returns cached (same request)
25
- # Cache goes out of scope and is garbage collected
26
29
  """
27
30
 
28
31
  def __init__(self, device):
32
+ debug("JDPackageCache: Initializing new cache instance")
29
33
  self._device = device
30
34
  self._linkgrabber_packages = None
31
35
  self._linkgrabber_links = None
32
36
  self._downloader_packages = None
33
37
  self._downloader_links = None
34
- self._archive_package_uuids = None # Set of package UUIDs containing archives
38
+ self._archive_cache = {} # package_uuid -> bool (is_archive)
35
39
  self._is_collecting = None
40
+ # Stats tracking
41
+ self._api_calls = 0
42
+ self._cache_hits = 0
43
+
44
+ def get_stats(self):
45
+ """Return cache statistics string."""
46
+ pkg_count = len(self._downloader_packages or []) + len(self._linkgrabber_packages or [])
47
+ link_count = len(self._downloader_links or []) + len(self._linkgrabber_links or [])
48
+ return f"{self._api_calls} API calls | {pkg_count} packages, {link_count} links cached"
36
49
 
37
50
  @property
38
51
  def linkgrabber_packages(self):
39
52
  if self._linkgrabber_packages is None:
53
+ debug("JDPackageCache: Fetching linkgrabber_packages from API")
54
+ self._api_calls += 1
40
55
  try:
41
56
  self._linkgrabber_packages = self._device.linkgrabber.query_packages()
42
- except (TokenExpiredException, RequestTimeoutException, MYJDException):
57
+ debug(f"JDPackageCache: Retrieved {len(self._linkgrabber_packages)} linkgrabber packages")
58
+ except (TokenExpiredException, RequestTimeoutException, MYJDException) as e:
59
+ debug(f"JDPackageCache: Failed to fetch linkgrabber_packages: {e}")
43
60
  self._linkgrabber_packages = []
61
+ else:
62
+ self._cache_hits += 1
63
+ debug(f"JDPackageCache: Using cached linkgrabber_packages ({len(self._linkgrabber_packages)} packages)")
44
64
  return self._linkgrabber_packages
45
65
 
46
66
  @property
47
67
  def linkgrabber_links(self):
48
68
  if self._linkgrabber_links is None:
69
+ debug("JDPackageCache: Fetching linkgrabber_links from API")
70
+ self._api_calls += 1
49
71
  try:
50
72
  self._linkgrabber_links = self._device.linkgrabber.query_links()
51
- except (TokenExpiredException, RequestTimeoutException, MYJDException):
73
+ debug(f"JDPackageCache: Retrieved {len(self._linkgrabber_links)} linkgrabber links")
74
+ except (TokenExpiredException, RequestTimeoutException, MYJDException) as e:
75
+ debug(f"JDPackageCache: Failed to fetch linkgrabber_links: {e}")
52
76
  self._linkgrabber_links = []
77
+ else:
78
+ self._cache_hits += 1
79
+ debug(f"JDPackageCache: Using cached linkgrabber_links ({len(self._linkgrabber_links)} links)")
53
80
  return self._linkgrabber_links
54
81
 
55
82
  @property
56
83
  def downloader_packages(self):
57
84
  if self._downloader_packages is None:
85
+ debug("JDPackageCache: Fetching downloader_packages from API")
86
+ self._api_calls += 1
58
87
  try:
59
88
  self._downloader_packages = self._device.downloads.query_packages()
60
- except (TokenExpiredException, RequestTimeoutException, MYJDException):
89
+ debug(f"JDPackageCache: Retrieved {len(self._downloader_packages)} downloader packages")
90
+ except (TokenExpiredException, RequestTimeoutException, MYJDException) as e:
91
+ debug(f"JDPackageCache: Failed to fetch downloader_packages: {e}")
61
92
  self._downloader_packages = []
93
+ else:
94
+ self._cache_hits += 1
95
+ debug(f"JDPackageCache: Using cached downloader_packages ({len(self._downloader_packages)} packages)")
62
96
  return self._downloader_packages
63
97
 
64
98
  @property
65
99
  def downloader_links(self):
66
100
  if self._downloader_links is None:
101
+ debug("JDPackageCache: Fetching downloader_links from API")
102
+ self._api_calls += 1
67
103
  try:
68
104
  self._downloader_links = self._device.downloads.query_links()
69
- except (TokenExpiredException, RequestTimeoutException, MYJDException):
105
+ debug(f"JDPackageCache: Retrieved {len(self._downloader_links)} downloader links")
106
+ except (TokenExpiredException, RequestTimeoutException, MYJDException) as e:
107
+ debug(f"JDPackageCache: Failed to fetch downloader_links: {e}")
70
108
  self._downloader_links = []
109
+ else:
110
+ self._cache_hits += 1
111
+ debug(f"JDPackageCache: Using cached downloader_links ({len(self._downloader_links)} links)")
71
112
  return self._downloader_links
72
113
 
73
114
  @property
74
115
  def is_collecting(self):
75
116
  if self._is_collecting is None:
117
+ debug("JDPackageCache: Checking is_collecting from API")
118
+ self._api_calls += 1
76
119
  try:
77
120
  self._is_collecting = self._device.linkgrabber.is_collecting()
78
- except (TokenExpiredException, RequestTimeoutException, MYJDException):
121
+ debug(f"JDPackageCache: is_collecting = {self._is_collecting}")
122
+ except (TokenExpiredException, RequestTimeoutException, MYJDException) as e:
123
+ debug(f"JDPackageCache: Failed to check is_collecting: {e}")
79
124
  self._is_collecting = False
125
+ else:
126
+ self._cache_hits += 1
127
+ debug(f"JDPackageCache: Using cached is_collecting = {self._is_collecting}")
80
128
  return self._is_collecting
81
129
 
82
- def get_archive_package_uuids(self, downloader_packages, downloader_links):
130
+ def _has_archive_extension(self, package_uuid, links):
131
+ """Check if any link in the package has an archive file extension."""
132
+ for link in links:
133
+ if link.get("packageUUID") != package_uuid:
134
+ continue
135
+ name = link.get("name", "")
136
+ name_lower = name.lower()
137
+ for ext in ARCHIVE_EXTENSIONS:
138
+ if name_lower.endswith(ext):
139
+ debug(
140
+ f"JDPackageCache: Found archive extension '{ext}' in file '{name}' for package {package_uuid}")
141
+ return True
142
+ return False
143
+
144
+ def _bulk_detect_archives(self, package_uuids):
83
145
  """
84
- Get set of package UUIDs that contain at least one archive file.
146
+ Detect archives for multiple packages in ONE API call.
147
+
148
+ Returns:
149
+ tuple: (confirmed_archives: set, api_succeeded: bool)
150
+ - confirmed_archives: Package UUIDs confirmed as archives
151
+ - api_succeeded: Whether the API call worked (for fallback decisions)
152
+ """
153
+ confirmed_archives = set()
154
+
155
+ if not package_uuids:
156
+ debug("JDPackageCache: _bulk_detect_archives called with empty package_uuids")
157
+ return confirmed_archives, True
85
158
 
86
- Two-phase detection:
87
- 1. Check extractionStatus in link data (free - catches in-progress/completed extractions)
88
- 2. Single API call for all remaining packages (catches pre-extraction archives)
159
+ package_list = list(package_uuids)
160
+ debug(f"JDPackageCache: Bulk archive detection for {len(package_list)} packages")
89
161
 
90
- This correctly handles:
91
- - Mixed packages (archive + non-archive files)
92
- - Archives before extraction starts
93
- - Archives during/after extraction
162
+ try:
163
+ self._api_calls += 1
164
+ archive_infos = self._device.extraction.get_archive_info([], package_list)
165
+ debug(f"JDPackageCache: get_archive_info returned {len(archive_infos) if archive_infos else 0} results")
166
+
167
+ if archive_infos:
168
+ for i, archive_info in enumerate(archive_infos):
169
+ if archive_info:
170
+ debug(f"JDPackageCache: archive_info[{i}] = {archive_info}")
171
+ # Try to get packageUUID from response
172
+ pkg_uuid = archive_info.get("packageUUID")
173
+ if pkg_uuid:
174
+ debug(f"JDPackageCache: Confirmed archive via packageUUID: {pkg_uuid}")
175
+ confirmed_archives.add(pkg_uuid)
176
+ else:
177
+ # Log what fields ARE available for debugging
178
+ debug(
179
+ f"JDPackageCache: archive_info has no packageUUID, available keys: {list(archive_info.keys())}")
180
+ else:
181
+ debug(f"JDPackageCache: archive_info[{i}] is empty/None")
182
+
183
+ debug(f"JDPackageCache: Bulk detection confirmed {len(confirmed_archives)} archives: {confirmed_archives}")
184
+ return confirmed_archives, True
185
+
186
+ except Exception as e:
187
+ debug(f"JDPackageCache: Bulk archive detection API FAILED: {type(e).__name__}: {e}")
188
+ return confirmed_archives, False
189
+
190
+ def detect_all_archives(self, packages, links):
94
191
  """
95
- if self._archive_package_uuids is not None:
96
- return self._archive_package_uuids
192
+ Detect archives for all packages efficiently.
97
193
 
98
- self._archive_package_uuids = set()
194
+ Uses ONE bulk API call, then applies safety fallbacks for packages
195
+ where detection was uncertain.
99
196
 
100
- if not downloader_packages:
101
- return self._archive_package_uuids
197
+ Args:
198
+ packages: List of downloader packages
199
+ links: List of downloader links (for extension fallback)
102
200
 
103
- all_package_uuids = {p.get("uuid") for p in downloader_packages if p.get("uuid")}
201
+ Returns:
202
+ Set of package UUIDs that should be treated as archives
203
+ """
204
+ if not packages:
205
+ debug("JDPackageCache: detect_all_archives called with no packages")
206
+ return set()
104
207
 
105
- # Phase 1: Check extractionStatus in already-fetched link data (free - no API call)
106
- # This catches packages where extraction is in progress or completed
107
- for link in downloader_links:
108
- extraction_status = link.get("extractionStatus")
109
- if extraction_status: # Any non-empty extraction status means it's an archive
110
- pkg_uuid = link.get("packageUUID")
111
- if pkg_uuid:
112
- self._archive_package_uuids.add(pkg_uuid)
208
+ all_package_uuids = {p.get("uuid") for p in packages if p.get("uuid")}
209
+ debug(f"JDPackageCache: detect_all_archives for {len(all_package_uuids)} packages")
113
210
 
114
- # Phase 2: Single API call for all unchecked packages
115
- unchecked_package_uuids = list(all_package_uuids - self._archive_package_uuids)
211
+ # ONE bulk API call for all packages
212
+ confirmed_archives, api_succeeded = self._bulk_detect_archives(all_package_uuids)
213
+ debug(f"JDPackageCache: Bulk API succeeded={api_succeeded}, confirmed={len(confirmed_archives)} archives")
116
214
 
117
- if unchecked_package_uuids:
118
- try:
119
- # One API call for ALL unchecked packages
120
- archive_infos = self._device.extraction.get_archive_info([], unchecked_package_uuids)
121
- if archive_infos:
122
- for archive_info in archive_infos:
123
- if archive_info:
124
- # Extract package UUID from response
125
- pkg_uuid = archive_info.get("packageUUID")
126
- if pkg_uuid:
127
- self._archive_package_uuids.add(pkg_uuid)
128
- except:
129
- pass
130
-
131
- return self._archive_package_uuids
215
+ # For packages NOT confirmed as archives, apply safety fallbacks
216
+ unconfirmed = all_package_uuids - confirmed_archives
217
+ debug(f"JDPackageCache: {len(unconfirmed)} packages need fallback checking")
218
+
219
+ for pkg_uuid in unconfirmed:
220
+ # Fallback 1: Check file extensions
221
+ if self._has_archive_extension(pkg_uuid, links):
222
+ debug(f"JDPackageCache: Package {pkg_uuid} confirmed as archive via extension fallback")
223
+ confirmed_archives.add(pkg_uuid)
224
+ # Fallback 2: If bulk API failed completely, assume archive (safe)
225
+ elif not api_succeeded:
226
+ debug(f"JDPackageCache: SAFETY - Bulk API failed, assuming package {pkg_uuid} is archive")
227
+ confirmed_archives.add(pkg_uuid)
228
+ else:
229
+ debug(f"JDPackageCache: Package {pkg_uuid} confirmed as NON-archive (API worked, no extension match)")
230
+
231
+ # Cache results for is_package_archive() lookups
232
+ for pkg_uuid in all_package_uuids:
233
+ self._archive_cache[pkg_uuid] = pkg_uuid in confirmed_archives
234
+
235
+ debug(
236
+ f"JDPackageCache: Final archive detection: {len(confirmed_archives)}/{len(all_package_uuids)} packages are archives")
237
+ return confirmed_archives
238
+
239
+ def is_package_archive(self, package_uuid, links=None):
240
+ """
241
+ Check if a package contains archive files.
242
+
243
+ Prefer calling detect_all_archives() first for efficiency.
244
+ This method is for single lookups or cache hits.
245
+
246
+ SAFETY: On API error, defaults to True (assume archive) to prevent
247
+ premature "finished" status.
248
+ """
249
+ if package_uuid is None:
250
+ debug("JDPackageCache: is_package_archive called with None UUID")
251
+ return False
252
+
253
+ if package_uuid in self._archive_cache:
254
+ self._cache_hits += 1
255
+ cached = self._archive_cache[package_uuid]
256
+ debug(f"JDPackageCache: is_package_archive({package_uuid}) = {cached} (cached)")
257
+ return cached
258
+
259
+ debug(f"JDPackageCache: is_package_archive({package_uuid}) - cache miss, querying API")
260
+
261
+ # Single package lookup (fallback if detect_all_archives wasn't called)
262
+ is_archive = None
263
+ api_failed = False
264
+
265
+ try:
266
+ self._api_calls += 1
267
+ archive_info = self._device.extraction.get_archive_info([], [package_uuid])
268
+ debug(f"JDPackageCache: Single get_archive_info returned: {archive_info}")
269
+ # Original logic: is_archive = True if archive_info and archive_info[0] else False
270
+ is_archive = True if archive_info and archive_info[0] else False
271
+ debug(f"JDPackageCache: API says is_archive = {is_archive}")
272
+ except Exception as e:
273
+ api_failed = True
274
+ debug(f"JDPackageCache: Single archive detection API FAILED for {package_uuid}: {type(e).__name__}: {e}")
275
+
276
+ # Fallback: check file extensions if API failed or returned False
277
+ if (api_failed or not is_archive) and links:
278
+ if self._has_archive_extension(package_uuid, links):
279
+ debug(f"JDPackageCache: Package {package_uuid} confirmed as archive via extension fallback")
280
+ is_archive = True
281
+
282
+ # SAFETY: If API failed and no extension detected, assume archive (conservative)
283
+ if is_archive is None:
284
+ debug(f"JDPackageCache: SAFETY - Detection uncertain for {package_uuid}, assuming archive")
285
+ is_archive = True
286
+
287
+ self._archive_cache[package_uuid] = is_archive
288
+ debug(f"JDPackageCache: is_package_archive({package_uuid}) = {is_archive} (final)")
289
+ return is_archive