Skip to content

Commit

Permalink
no need for special post_upload function
Browse files Browse the repository at this point in the history
  • Loading branch information
mib1185 committed Jan 5, 2025
1 parent b78d19f commit 68082e9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/synology_dsm/api/file_station/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ async def upload_files(
content: bytes | BufferedReader | AsyncIterator[bytes],
) -> bool | None:
"""Upload a file to a folder."""
raw_data = await self._dsm.post_upload(
self.UPLOAD_API_KEY, "upload", path, filename, content
raw_data = await self._dsm.post(
self.UPLOAD_API_KEY, "upload", path=path, filename=filename, content=content
)
if not isinstance(raw_data, dict):
return None
Expand Down
41 changes: 8 additions & 33 deletions src/synology_dsm/synology_dsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
import asyncio
import logging
import socket
from collections.abc import AsyncIterator
from hashlib import md5
from io import BufferedReader
from ipaddress import IPv6Address
from json import JSONDecodeError
from typing import TYPE_CHECKING, Any, Coroutine, TypedDict
from typing import Any, Coroutine, TypedDict
from urllib.parse import quote, urlencode

from aiohttp import ClientError, ClientSession, ClientTimeout, MultipartWriter, hdrs
Expand Down Expand Up @@ -251,26 +249,6 @@ async def post(
"""Handles API POST request."""
return await self._request("POST", api, method, params, **kwargs)

async def post_upload(
self,
api: str,
method: str,
filepath: str,
filename: str,
content: bytes | BufferedReader | AsyncIterator[bytes],
**kwargs: Any,
) -> bytes | dict | str:
"""Handles an upload API POST request."""
return await self._request(
"POST",
api,
method,
filepath=filepath,
filename=filename,
content=content,
**kwargs,
)

async def generate_url(
self,
api: str,
Expand Down Expand Up @@ -361,7 +339,7 @@ async def _request(
return response

async def _execute_request(
self, method: str, url: URL, params: dict | None, **kwargs: Any
self, method: str, url: URL, params: dict, **kwargs: Any
) -> bytes | dict | str:
"""Function to execute and handle a request."""
if params:
Expand All @@ -379,20 +357,17 @@ async def _execute_request(
url_encoded, timeout=self._aiohttp_timeout, **kwargs
)
elif (
method == "POST"
and (content := kwargs.get("content"))
and (filepath := kwargs.get("filepath"))
and (filename := kwargs.get("filename"))
method == "POST" and params.get("api") == SynoFileStation.UPLOAD_API_KEY
):
if TYPE_CHECKING:
assert isinstance(content, bytes) # noqa: S101
assert isinstance(filename, str) # noqa: S101
content = kwargs.pop("content")
path = kwargs.pop("path")
filename = kwargs.pop("filename")

boundary = md5(
str(url_encoded).encode("utf-8"), usedforsecurity=False
).hexdigest()
with MultipartWriter("form-data", boundary=boundary) as mp:
part = mp.append(filepath)
part = mp.append(path)
part.headers.pop(hdrs.CONTENT_TYPE)
part.set_content_disposition("form-data", name="path")

Expand Down Expand Up @@ -599,7 +574,7 @@ def external_usb(self) -> SynoCoreExternalUSB:

@property
def file(self) -> SynoFileStation:
"""Gets NAS files."""
"""Gets NAS FileStation."""
if not self._file:
self._file = SynoFileStation(self)
return self._file
Expand Down

0 comments on commit 68082e9

Please sign in to comment.