aiohttp-zip-response 1.0.1__tar.gz → 2.0.0__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.1
3
+ Version: 2.0.0
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
@@ -42,7 +41,10 @@ Description-Content-Type: text/markdown
42
41
  [license-image]: https://img.shields.io/pypi/l/aiohttp-zip-response
43
42
  [license-url]: https://github.com/DoctorJohn/aiohttp-zip-response/blob/master/LICENSE
44
43
 
45
- A AIOHTTP response class streaming a directory as ZIP archive.
44
+ A AIOHTTP response class streaming the contents of a directory as a ZIP archive.
45
+ Thanks to [stream-zip](https://github.com/uktrade/stream-zip/), this works **without storing the entire ZIP in memory or disk**.
46
+
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.
46
48
 
47
49
  ## Installation
48
50
 
@@ -66,3 +68,7 @@ app.router.add_get('/zip', handle_zip)
66
68
  web.run_app(app)
67
69
  ```
68
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
+
@@ -14,7 +14,10 @@
14
14
  [license-image]: https://img.shields.io/pypi/l/aiohttp-zip-response
15
15
  [license-url]: https://github.com/DoctorJohn/aiohttp-zip-response/blob/master/LICENSE
16
16
 
17
- A AIOHTTP response class streaming a directory as ZIP archive.
17
+ A AIOHTTP response class streaming the contents of a directory as a ZIP archive.
18
+ Thanks to [stream-zip](https://github.com/uktrade/stream-zip/), this works **without storing the entire ZIP in memory or disk**.
19
+
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.
18
21
 
19
22
  ## Installation
20
23
 
@@ -37,3 +40,7 @@ app = web.Application()
37
40
  app.router.add_get('/zip', handle_zip)
38
41
  web.run_app(app)
39
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,59 @@
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
+ yield (
43
+ str(member_path.relative_to(self._base_path)),
44
+ datetime.fromtimestamp(lstat.st_mtime),
45
+ lstat.st_mode,
46
+ ZIP_32,
47
+ self.yield_member_chunks(member_path),
48
+ )
49
+
50
+ async def yield_member_chunks(self, path: Path) -> AsyncGenerator[bytes, None]:
51
+ if path.is_dir():
52
+ return
53
+
54
+ with path.open("rb") as file:
55
+ while True:
56
+ chunk = file.read(self._chunk_size)
57
+ if not chunk:
58
+ break
59
+ yield chunk
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "aiohttp-zip-response"
3
- version = "1.0.1"
3
+ version = "2.0.0"
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,16 @@ 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
+ ruff = "^0.4.10"
38
+ mypy = "^1.10.1"
40
39
 
41
40
  [tool.ruff.lint]
42
41
  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