From 2e6ff04c0b4da4557116442b7170ffe929a40971 Mon Sep 17 00:00:00 2001 From: mib1185 Date: Sun, 5 Jan 2025 20:24:45 +0000 Subject: [PATCH] add download_file, improve upload_file --- src/synology_dsm/api/file_station/__init__.py | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/synology_dsm/api/file_station/__init__.py b/src/synology_dsm/api/file_station/__init__.py index 657adf2..70b33ba 100644 --- a/src/synology_dsm/api/file_station/__init__.py +++ b/src/synology_dsm/api/file_station/__init__.py @@ -5,6 +5,8 @@ from collections.abc import AsyncIterator from io import BufferedReader +from aiohttp import StreamReader + from synology_dsm.api import SynoBaseApi from .models import ( @@ -110,16 +112,40 @@ async def get_files( return files - async def upload_files( + async def upload_file( self, path: str, filename: str, - content: bytes | BufferedReader | AsyncIterator[bytes], + source: bytes | BufferedReader | AsyncIterator[bytes] | str, ) -> bool | None: - """Upload a file to a folder.""" + """Upload a file to a folder from eather a local source_file or content.""" + if isinstance(source, str): + source = open(source, "rb") + raw_data = await self._dsm.post( - self.UPLOAD_API_KEY, "upload", path=path, filename=filename, content=content + self.UPLOAD_API_KEY, "upload", path=path, filename=filename, content=source ) if not isinstance(raw_data, dict): return None return raw_data.get("success") + + async def download_file( + self, path: str, filename: str, target_file: str | None = None + ) -> StreamReader | bool | None: + """Download a file to local target_file or returns an async StreamReader.""" + response_content = await self._dsm.get( + self.DOWNLOAD_API_KEY, + "download", + {"path": f"{path}/{filename}", "mode": "download"}, + raw_response_content=True, + ) + if not isinstance(response_content, StreamReader): + return None + + if target_file: + with open(target_file, "wb") as fh: + async for data in response_content.iter_chunked(8192): + fh.write(data) + return True + + return response_content