Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extension support for Nextcloud and Owncloud? #9

Open
skshetry opened this issue Mar 14, 2021 · 5 comments
Open

Extension support for Nextcloud and Owncloud? #9

skshetry opened this issue Mar 14, 2021 · 5 comments

Comments

@skshetry
Copy link
Owner

skshetry commented Mar 14, 2021

Also, look for CreationDate and ModifiedDate with X-OC- headers. See if they should/could be used instead. Also, we should consider supporting touch and/or modifying datetimes for Owncloud and Nextcloud.

@amichuda
Copy link

This would be a great feature! Thank you for your work on this.

@skshetry
Copy link
Owner Author

@amichuda, what feature are you thinking of?

@Seriousness
Copy link

I've added some customizations locally but don't feel save enough for commiting a pull request. So I just leave it here. What it does: optional named parameters for upload_file+upload_file_objects and introducing one that is named headers and is later merged with the chunk_size one.

so I have the git copied to moduls/webdav4 which results in this strange path:

from modules.webdav4.src.webdav4.client import Client, ResourceAlreadyExists
client = Client(nextcloud_url + "/remote.php/dav/files/" + username + '/', auth=(username, password))
local_timestamp = os.path.getmtime(full_path)
client.upload_file(
                    full_path,
                    nextcloud_file_path,
                    headers = {'X-OC-MTIME': str(int(local_timestamp))},
                    overwrite = True,
                    callback=progress_callback
                )

updated client.py

def upload_file(
        self,
        from_path: "PathLike[AnyStr]",
        to_path: str,
        *,
        overwrite: bool = False,
        chunk_size: int = None,
        callback: Callable[[int], Any] = None,
        headers: Dict[str, Any] = {}
    ) -> None:
        """Upload file from local path to a given remote path."""

        with open(from_path, mode="rb") as fobj:
            self.upload_fileobj(
                fobj,
                to_path,
                **{
                    'overwrite': overwrite,
                    'callback': callback,
                    'chunk_size': chunk_size,
                    'headers': headers
                }
            )

    def upload_fileobj(
        self,
        file_obj: BinaryIO,
        to_path: str,
        *,
        overwrite: bool = False,
        callback: Callable[[int], Any] = None,
        chunk_size: int = None,
        size: int = None,
        headers: Dict[str, Any] = {}
    ) -> None:
        """Upload file from file object to given path."""
        # we try to avoid chunked transfer as much as possible
        # so we try to use size as a hint if provided.
        # else, we will try to find that out from the file object
        # if we are not successful in that, we gracefully fallback
        # to the chunked encoding.
        if size is None:
            size = peek_filelike_length(file_obj)

        # merge headers plus content-length
        headers = headers | {"Content-Length": str(size)} if size is not None else None
        if not overwrite and self.exists(to_path):
            raise ResourceAlreadyExists(to_path)

        wrapped = wrap_file_like(file_obj, callback)
        content = read_chunks(
            wrapped, chunk_size=chunk_size or self.chunk_size
        )

        http_resp = self.request(
            HTTPMethod.PUT, to_path, content=content, headers=headers
        )
        http_resp.raise_for_status()

if somebody could tell me how arguments should work for the lib I'd gladly adjust my code and try a pull request...

@skshetry
Copy link
Owner Author

Hi, I am open to support headers= in client.upload_fileobj() and client.upload_file().

@Seriousness
Copy link

Including the optional named things I did?

erikman added a commit to erikman/webdav4 that referenced this issue Mar 2, 2024
Adds http header parameter to Client.upload and Client.upload_fileobj.
Allows for setting creation/modified date for nextcloud/owncloud
uploads. Solution is inspired by discussion in issue skshetry#9.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants