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

Fix and modification of "download_file" #63

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/nextcloud/api_wrappers/webdav.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import re
import os
import pathlib
from urllib.parse import unquote

import xml.etree.ElementTree as ET

Expand Down Expand Up @@ -69,7 +70,7 @@ def list_folders(self, uid, path=None, depth=1, all_properties=False):
resp.data = files_data if not self.json_output else [each.as_dict() for each in files_data]
return resp

def download_file(self, uid, path):
def download_file(self, uid, path, download_path, overwrite=False):
"""
Download file of given user by path
File will be saved to working directory
Expand All @@ -84,25 +85,38 @@ def download_file(self, uid, path):
Args:
uid (str): uid of user
path (str): file path
download_path (str): directory where the files will be downloaded
overwrite (bool): overwrite files if the name is the same

Returns:
None
"""
additional_url = "/".join([uid, path])
filename = path.split('/')[-1] if '/' in path else path
filename = unquote(filename)
file_data = self.list_folders(uid=uid, path=path, depth=0)

if not file_data:
raise ValueError("Given path doesn't exist")

file_resource_type = (file_data.data[0].get('resource_type')
if self.json_output
else file_data.data[0].resource_type)

if file_resource_type == File.COLLECTION_RESOURCE_TYPE:
raise ValueError("This is a collection, please specify file path")
if filename in os.listdir('./'):

if filename in os.listdir(download_path) and not overwrite:
raise ValueError("File with such name already exists in this directory")

res = self.requester.download(additional_url)
with open(filename, 'wb') as f:
f.write(res.data)

file_path = os.path.join(download_path, filename)
with open(file_path, 'wb') as f:
if type(res.data) == str:
f.write(res.data.encode())
else:
f.write(res.data)

# get timestamp of downloaded file from file property on Nextcloud
# If it succeeded, set the timestamp to saved local file
Expand All @@ -112,7 +126,7 @@ def download_file(self, uid, path):
else file_data.data[0].last_modified)
file_timestamp = timestamp_to_epoch_time(file_timestamp_str)
if isinstance(file_timestamp, int):
os.utime(filename, (datetime.now().timestamp(), file_timestamp))
os.utime(file_path, (datetime.now().timestamp(), file_timestamp))

def upload_file(self, uid, local_filepath, remote_filepath, timestamp=None):
"""
Expand Down