feat(product-suite): make image pulls cancellable

This commit is contained in:
chengma
2026-07-16 19:37:22 +08:00
parent c9afaad24b
commit 75af87d441
12 changed files with 1157 additions and 88 deletions
+31
View File
@@ -29,6 +29,7 @@ class _Response:
self.headers = dict(headers or {})
self.url = url
self.chunk_size = chunk_size
self.closed = False
def iter_content(self, chunk_size=65536):
if self.chunk_size:
@@ -37,6 +38,9 @@ class _Response:
return
yield self.content
def close(self):
self.closed = True
class ImageStudioImageTests(TempDirMixin, unittest.TestCase):
def _png_bytes(self, size=(20, 16), color=(80, 120, 200, 255)):
@@ -246,6 +250,33 @@ class ImageStudioImageTests(TempDirMixin, unittest.TestCase):
self.assert_removed(temp_dir)
def test_download_remote_image_cancels_between_chunks_and_closes_response(self):
png = self._png_bytes(size=(120, 120))
response = _Response(
content=png,
headers={"Content-Type": "image/png"},
chunk_size=32,
)
session = SimpleNamespace(get=lambda *args, **kwargs: response)
checks = {"count": 0}
def should_stop():
checks["count"] += 1
return checks["count"] >= 4
with mock.patch(
"app.image_studio_images.socket.getaddrinfo",
return_value=self._public_dns(),
):
with self.assertRaises(image_studio_images.ImageStudioImageCancelled):
image_studio_images.download_remote_image(
"https://cdn.example.com/a.png",
session=session,
should_stop=should_stop,
)
self.assertTrue(response.closed)
def test_import_original_files_copies_valid_images_deduplicates_and_limits(self):
with self.make_temp_dir() as temp_dir:
db_path = os.path.join(temp_dir, "cmshopee.db")