From 5f84db708c42171608325a2e03880d07b4e6dad4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cau=C3=AA=20Faleiros?= Date: Fri, 27 Nov 2026 16:37:57 -0300 Subject: [PATCH] fix: the folders' download links last an hour, and one that ran out is fetched again quietly The folders download a whole list of files one after another, from links made when the list was asked for, valid 5 minutes. With films of 10 m the last ones began after that and failed with "erro 403": a red error, a Sentry report, and only the next pass got new links. The folders' and hot folder's links now last an hour (customer downloads keep 5 minutes), and an expired link waits for the next pass like a dropped network. Co-Authored-By: Claude Opus 5.5 --- app/adapters.py | 6 +++--- app/api/operator/files.py | 8 ++++++-- tests/print_file_test.py | 3 +++ web/kanban.js | 3 +++ 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/app/adapters.py b/app/adapters.py index 2bd2f7d..6faa8e0 100644 --- a/app/adapters.py +++ b/app/adapters.py @@ -232,7 +232,7 @@ class ObjectStorage(Protocol): def part_url(self, key: str, upload_id: str, part: int, size: int) -> str: ... def complete(self, key: str, upload_id: str, parts: list): ... def size(self, key: str) -> int: ... - def download(self, key: str, name: str) -> str: ... + def download(self, key: str, name: str, expires: int = 300) -> str: ... def fetch(self, key: str, path: str): ... def store(self, key: str, path: str, content_type: str): ... def health(self): ... @@ -299,12 +299,12 @@ class LocalS3Storage: """Upload a file the service generated itself, such as a print file.""" self.patient.upload_file(path, self.bucket, key, ExtraArgs={'ContentType': content_type}) - def download(self, key, name): + def download(self, key, name, expires=300): from urllib.parse import quote return self.public.generate_presigned_url('get_object', Params={ 'Bucket': self.bucket, 'Key': key, 'ResponseContentDisposition': "attachment; filename*=UTF-8''" + quote(name, safe=''), - 'ResponseContentType': 'application/octet-stream'}, ExpiresIn=300) + 'ResponseContentType': 'application/octet-stream'}, ExpiresIn=expires) def view(self, key, content_type, expires=3600): """A link to show an object in the page, not download it: the Kanban's diff --git a/app/api/operator/files.py b/app/api/operator/files.py index bf2faa7..32844ba 100644 --- a/app/api/operator/files.py +++ b/app/api/operator/files.py @@ -16,6 +16,10 @@ from ...scanning import require_clean from .common import FINAL_OK router = APIRouter() +# The folders download a whole list of files, one after another, from links +# made when the list was asked for: with films of 10 m, 5 minutes ran out +# before the last ones began (erro 403). Only the team's computers see them. +FOLDER_LINK_SECONDS = 3600 def named_files(rows): """Each file named after its order and item, so a folder of them stays @@ -39,7 +43,7 @@ def named_files(rows): names.add(name) files.append({'order': str(row['order_id']), 'state': row['state'], 'number': row['number'], 'item': row['item_index'] + 1, 'name': name, 'upload_id': str(row['upload_id']), - 'url': storage.download(row['object_key'], name)}) + 'url': storage.download(row['object_key'], name, FOLDER_LINK_SECONDS)}) return files @@ -163,7 +167,7 @@ def queue_hot(user=Depends(can('print'))): watched = hotfolder.watched(c) return {'folder': hotfolder.PRINTING_FOLDER, 'write': [{'id': r['id'], 'order': str(r['order_id']), 'folder': r['folder'], 'name': r['name'], - 'url': storage.download(keys[str(r['upload_id'])], r['name'])} for r in due], + 'url': storage.download(keys[str(r['upload_id'])], r['name'], FOLDER_LINK_SECONDS)} for r in due], 'watch': [{'id': r['id'], 'order': str(r['order_id']), 'folder': r['folder'], 'name': r['name']} for r in watched if r['state'] == 'written'], 'withdraw': [{'id': r['id'], 'order': str(r['order_id']), 'folder': r['folder'], 'name': r['name']} diff --git a/tests/print_file_test.py b/tests/print_file_test.py index a88022f..0e2a6e1 100644 --- a/tests/print_file_test.py +++ b/tests/print_file_test.py @@ -155,6 +155,9 @@ def run(): mine = [f for f in early['files'] if f['order'] == oid] assert [(f['upload_id'], f['state']) for f in mine] == [(str(row['upload_id']), 'rec')], early['files'] assert mine[0]['name'].startswith('DTF-%d-item-1' % order['number']) and early['states'] == {oid: 'rec'} + # The folders download a whole list one file after another: an hour's link, + # not the 5 minutes that ran out before the last ones began. + assert 'X-Amz-Expires=3600' in mine[0]['url'], mine[0]['url'] late = client.call('/operator/queue/folder', {'early': False, 'held': []}, operator=True) assert not [f for f in late['files'] if f['order'] == oid] and late['states'] == {} client.call('/operator/queue/folder', {'early': True, 'held': []}, expected=401) diff --git a/web/kanban.js b/web/kanban.js index 95a4824..08e8d8f 100644 --- a/web/kanban.js +++ b/web/kanban.js @@ -568,6 +568,9 @@ async function fetchFile(url,options){ } async function fetchInto(dir,file,watched=false){ const r=await fetchFile(file.url,{referrerPolicy:'no-referrer'}); + // A link that ran out while the files before it were downloading: the next + // pass asks for a new one, as for a network that dropped. + if(r.status===403){const e=new Error('o link de '+file.name+' venceu antes do download.');e.unreachable=true;throw e;} if(!r.ok||!r.body)throw new Error('não foi possível baixar '+file.name+' (erro '+r.status+').'); await writeWhole(dir,file.name,r,watched); }