Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUp(self):
self.temp_config_dir, self.temp_repo_dir, self.drive_config, self.repo = get_sample_repo()
self.root_folder_item = onedrivesdk.Item(json.loads(get_resource('data/folder_item.json', pkg_name='tests')))
self.root_subfolder_item = onedrivesdk.Item(json.loads(
get_resource('data/subfolder_item.json', pkg_name='tests')))
self.root_child_item = onedrivesdk.Item(json.loads(
get_resource('data/folder_child_item.json', pkg_name='tests')))
self.image_item = onedrivesdk.Item(json.loads(get_resource('data/image_item.json', pkg_name='tests')))
self._add_all_items()
def test_remote_dir_matches_record(self):
item = onedrivesdk.Item(json.loads(get_resource('data/folder_item.json', pkg_name='tests')))
self.repo.update_item(item, '', size_local=0)
record = self.repo.get_item_by_path(item.name, '')
merge_dir.MergeDirectoryTask._remote_dir_matches_record(item, record)
def __create_folder(self, item_id, folder_name): #Create folder with provided name
f = onedrivesdk.Folder()
i = onedrivesdk.Item()
i.name = folder_name
i.folder = f
folder = self.__onedrive_service.item(drive='me', id=item_id).children.add(i)
log_success('[+] creating new directory...')
return folder.id #Return folder object ID
def _get_new_item(self):
item = Item()
if self.new_parent_relpath != self.parent_relpath:
ref = ItemReference()
# Refer to https://dev.onedrive.com/items/move.htm for Move API request.
ref.path = '/drives/' + self.repo.drive.id + '/root:'
if self.new_parent_relpath != '':
ref.path += self.new_parent_relpath
item.parent_reference = ref
if self.new_name != self.item_name:
item.name = self.new_name
return item
def update_timestamp_and_record(self, new_item, item_local_stat):
remote_mtime, remote_mtime_w = get_item_modified_datetime(new_item)
if not remote_mtime_w:
# last_modified_datetime attribute is not modifiable in OneDrive server. Update local mtime.
fix_owner_and_timestamp(self.local_abspath, self.repo.context.user_uid,
datetime_to_timestamp(remote_mtime))
else:
file_system_info = FileSystemInfo()
file_system_info.last_modified_date_time = datetime.utcfromtimestamp(item_local_stat.st_mtime)
updated_item = Item()
updated_item.file_system_info = file_system_info
item_request = self.repo.authenticator.client.item(drive=self.repo.drive.id, id=new_item.id)
new_item = item_request_call(self.repo, item_request.update, updated_item)
self.repo.update_item(new_item, self.parent_relpath, item_local_stat.st_size)
item_stat = os.stat(self.local_abspath)
if item_stat.st_size < self.PUT_FILE_SIZE_THRESHOLD_BYTES:
item_request = self.parent_dir_request.children[self.item_name]
returned_item = item_request_call(self.repo, item_request.upload, self.local_abspath)
if returned_item is None:
logging.warning('Upload API did not return metadata of remote item for "%s". '
'Make an explicit request.', self.local_abspath)
returned_item = item_request_call(self.repo, item_request.get)
else:
logging.info('Uploading large file "%s" in chunks of 10MB.', self.local_abspath)
item_request = self.repo.authenticator.client.item(drive=self.repo.drive.id, path=self.rel_path)
returned_item = item_request_call(self.repo, item_request.upload_async,
local_path=self.local_abspath, upload_status=self.update_progress)
if not isinstance(returned_item, onedrivesdk.Item):
if hasattr(returned_item, '_prop_dict'):
returned_item = onedrivesdk.Item(returned_item._prop_dict)
else:
returned_item = item_request_call(self.repo, item_request.get)
self.update_timestamp_and_record(returned_item, item_stat)
self.task_pool.release_path(self.local_abspath)
logging.info('Finished uploading file "%s".', self.local_abspath)
return True
except (onedrivesdk.error.OneDriveError, OSError) as e:
logging.error('Error uploading file "%s": %s.', self.local_abspath, e)
# TODO: what if quota is exceeded?
if (isinstance(e, onedrivesdk.error.OneDriveError) and
e.code == onedrivesdk.error.ErrorCode.MalwareDetected):
logging.warning('File "%s" was detected as malware by OneDrive. '
'Do not upload during program session.', self.local_abspath)
self.task_pool.occupy_path(self.local_abspath, None)
return False
self.task_pool.release_path(self.local_abspath)
size = len(data)
if size < max_chunk_size:
request = self._get_item(filename).content.request()
request.append_option(onedrivesdk.options.HeaderOption("Content-Type", "application/octet-stream"))
self.client.auth_provider.authenticate_request(request)
response = self.client.http_provider.send("PUT", request._headers, request.request_url, data=data)
assert response.status >= 200 and response.status < 300
else:
# create a blank file
self.put(filename, "")
f = io.BytesIO(data)
item = self._get_item(filename)
new_item = onedrivesdk.Item()
new_item.name = filename
response = item.create_session(new_item).post()
upload_url = response.upload_url
request = item.content.request()
request.append_option(onedrivesdk.options.HeaderOption("Content-Type", "application/octet-stream"))
self.client.auth_provider.authenticate_request(request)
offset = 0
while offset < size:
chunk = f.read(max_chunk_size)
chunklen = len(chunk)
request.append_option(onedrivesdk.options.HeaderOption(
"Content-Range", "bytes %s-%s/%s" % (offset, offset+chunklen-1, size)))
response = self.client.http_provider.send("PUT", request._headers, upload_url, data=chunk)
def _get_folder_pseudo_item(item_name):
item = Item()
item.name = item_name
item.folder = Folder()
return item
def _get_folder_pseudo_item(item_name):
item = Item()
item.name = item_name
item.folder = Folder()
return item