aiohttp-zip-response 1.0.2__tar.gz → 2.0.1__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: aiohttp-zip-response
3
- Version: 1.0.2
3
+ Version: 2.0.1
4
4
  Summary: A AIOHTTP response class streaming a directory as ZIP archive
5
5
  Home-page: https://github.com/DoctorJohn/aiohttp-zip-response
6
6
  License: MIT
@@ -17,11 +17,10 @@ Classifier: Programming Language :: Python :: 3.9
17
17
  Classifier: Programming Language :: Python :: 3.10
18
18
  Classifier: Programming Language :: Python :: 3.11
19
19
  Classifier: Programming Language :: Python :: 3.12
20
- Classifier: Programming Language :: Python :: 3.8
21
20
  Classifier: Topic :: Internet :: WWW/HTTP
22
21
  Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
23
22
  Requires-Dist: aiohttp (>=3.9.5,<4.0.0)
24
- Requires-Dist: stream-zip (>=0.0.71,<0.0.72)
23
+ Requires-Dist: stream-zip (>=0.0.81,<0.0.82)
25
24
  Project-URL: Documentation, https://github.com/DoctorJohn/aiohttp-zip-response
26
25
  Project-URL: Repository, https://github.com/DoctorJohn/aiohttp-zip-response
27
26
  Description-Content-Type: text/markdown
@@ -45,7 +44,7 @@ Description-Content-Type: text/markdown
45
44
  A AIOHTTP response class streaming the contents of a directory as a ZIP archive.
46
45
  Thanks to [stream-zip](https://github.com/uktrade/stream-zip/), this works **without storing the entire ZIP in memory or disk**.
47
46
 
48
- Generally, this package is meant to complement the existing `aiohttp.web.FileResponse` class, which can already stream the contents of a single file.
47
+ Generally, this package is meant to complement the existing `aiohttp.web.FileResponse` class which can be used to stream the contents of a single file.
49
48
 
50
49
  ## Installation
51
50
 
@@ -69,3 +68,7 @@ app.router.add_get('/zip', handle_zip)
69
68
  web.run_app(app)
70
69
  ```
71
70
 
71
+ ## Caveats
72
+
73
+ - The `Content-Length` header is not set because the size of the ZIP archive is not known in advance. This means clients such as browsers cannot display a download progress bar.
74
+
@@ -17,7 +17,7 @@
17
17
  A AIOHTTP response class streaming the contents of a directory as a ZIP archive.
18
18
  Thanks to [stream-zip](https://github.com/uktrade/stream-zip/), this works **without storing the entire ZIP in memory or disk**.
19
19
 
20
- Generally, this package is meant to complement the existing `aiohttp.web.FileResponse` class, which can already stream the contents of a single file.
20
+ Generally, this package is meant to complement the existing `aiohttp.web.FileResponse` class which can be used to stream the contents of a single file.
21
21
 
22
22
  ## Installation
23
23
 
@@ -40,3 +40,7 @@ app = web.Application()
40
40
  app.router.add_get('/zip', handle_zip)
41
41
  web.run_app(app)
42
42
  ```
43
+
44
+ ## Caveats
45
+
46
+ - The `Content-Length` header is not set because the size of the ZIP archive is not known in advance. This means clients such as browsers cannot display a download progress bar.
@@ -0,0 +1,67 @@
1
+ from datetime import datetime
2
+ from pathlib import Path
3
+ from typing import AsyncGenerator, Optional
4
+
5
+ from aiohttp import web
6
+ from aiohttp.typedefs import LooseHeaders, PathLike
7
+ from stream_zip import ZIP_32, AsyncMemberFile, async_stream_zip
8
+
9
+
10
+ class ZipResponse(web.StreamResponse):
11
+ def __init__(
12
+ self,
13
+ base_path: PathLike,
14
+ chunk_size: int = 256 * 1024,
15
+ status: int = 200,
16
+ reason: Optional[str] = None,
17
+ headers: Optional[LooseHeaders] = None,
18
+ ) -> None:
19
+ self._base_path = Path(base_path)
20
+ self._chunk_size = chunk_size
21
+ return super().__init__(
22
+ status=status,
23
+ reason=reason,
24
+ headers=headers,
25
+ )
26
+
27
+ async def prepare(self, request: web.BaseRequest) -> None:
28
+ await super().prepare(request)
29
+
30
+ async for chunk in async_stream_zip(self.yield_member_files()):
31
+ await self.write(chunk)
32
+
33
+ await self.write_eof()
34
+
35
+ async def yield_member_paths(self) -> AsyncGenerator[Path, None]:
36
+ for member_path in self._base_path.glob("**/*"):
37
+ yield member_path
38
+
39
+ async def yield_member_files(self) -> AsyncGenerator[AsyncMemberFile, None]:
40
+ async for member_path in self.yield_member_paths():
41
+ lstat = member_path.lstat()
42
+ relative_member_path = member_path.relative_to(self._base_path)
43
+
44
+ member_name = (
45
+ f"{relative_member_path}/"
46
+ if member_path.is_dir()
47
+ else str(relative_member_path)
48
+ )
49
+
50
+ yield (
51
+ member_name,
52
+ datetime.fromtimestamp(lstat.st_mtime),
53
+ lstat.st_mode,
54
+ ZIP_32,
55
+ self.yield_member_chunks(member_path),
56
+ )
57
+
58
+ async def yield_member_chunks(self, path: Path) -> AsyncGenerator[bytes, None]:
59
+ if path.is_dir():
60
+ return
61
+
62
+ with path.open("rb") as file:
63
+ while True:
64
+ chunk = file.read(self._chunk_size)
65
+ if not chunk:
66
+ break
67
+ yield chunk
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "aiohttp-zip-response"
3
- version = "1.0.2"
3
+ version = "2.0.1"
4
4
  description = "A AIOHTTP response class streaming a directory as ZIP archive"
5
5
  authors = ["Jonathan Ehwald <github@ehwald.info>"]
6
6
  license = "MIT"
@@ -15,7 +15,6 @@ classifiers = [
15
15
  "Operating System :: OS Independent",
16
16
  "Programming Language :: Python",
17
17
  "Programming Language :: Python :: 3",
18
- "Programming Language :: Python :: 3.8",
19
18
  "Programming Language :: Python :: 3.9",
20
19
  "Programming Language :: Python :: 3.10",
21
20
  "Programming Language :: Python :: 3.11",
@@ -27,16 +26,17 @@ classifiers = [
27
26
  [tool.poetry.dependencies]
28
27
  python = "^3.9"
29
28
  aiohttp = "^3.9.5"
30
- stream-zip = "^0.0.71"
29
+ stream-zip = "^0.0.81"
31
30
 
32
31
  [tool.poetry.group.dev.dependencies]
33
32
  pre-commit = "^3.7.1"
34
- pytest = "^8.2.0"
33
+ pytest = "^8.2.2"
35
34
  pytest-cov = "^5.0.0"
36
- pytest-asyncio = "^0.23.6"
35
+ pytest-asyncio = "^0.23.7"
37
36
  pytest-aiohttp = "^1.0.5"
38
- ruff = "^0.4.4"
39
- mypy = "^1.10.0"
37
+ pytest-randomly = "^3.15.0"
38
+ ruff = "^0.5.1"
39
+ mypy = "^1.10.1"
40
40
 
41
41
  [tool.ruff.lint]
42
42
  extend-select = ["I"]
@@ -1,48 +0,0 @@
1
- from datetime import datetime
2
- from pathlib import Path
3
- from typing import AsyncGenerator, Optional
4
-
5
- from aiohttp import web
6
- from aiohttp.typedefs import LooseHeaders, PathLike
7
- from stream_zip import ZIP_32, async_stream_zip # type: ignore
8
-
9
-
10
- class ZipResponse(web.Response):
11
- def __init__(
12
- self,
13
- path: PathLike,
14
- chunk_size: int = 256 * 1024,
15
- status: int = 200,
16
- reason: Optional[str] = None,
17
- headers: Optional[LooseHeaders] = None,
18
- ) -> None:
19
- self._path = Path(path)
20
- self._chunk_size = chunk_size
21
- return super().__init__(
22
- body=async_stream_zip(self.yield_members()),
23
- status=status,
24
- reason=reason,
25
- headers=headers,
26
- )
27
-
28
- async def yield_members(self):
29
- for sub_path in self._path.glob("**/*"):
30
- lstat = sub_path.lstat()
31
- yield (
32
- str(sub_path.relative_to(self._path)),
33
- datetime.fromtimestamp(lstat.st_mtime),
34
- lstat.st_mode,
35
- ZIP_32,
36
- self.yield_contents(sub_path),
37
- )
38
-
39
- async def yield_contents(self, file_path: Path) -> AsyncGenerator[bytes, None]:
40
- if file_path.is_dir():
41
- return
42
-
43
- with file_path.open("rb") as file:
44
- while True:
45
- chunk = file.read(self._chunk_size)
46
- if not chunk:
47
- break
48
- yield chunk