diff --git a/bot/helpers/gdrive_utils/gDrive.py b/bot/helpers/gdrive_utils/gDrive.py index 16d69f86..57e0404a 100644 --- a/bot/helpers/gdrive_utils/gDrive.py +++ b/bot/helpers/gdrive_utils/gDrive.py @@ -60,7 +60,6 @@ def getFilesByFolderId(self, folder_id): break return files - @retry(wait=wait_exponential(multiplier=2, min=3, max=6), stop=stop_after_attempt(5), retry=retry_if_exception_type(HttpError), before=before_log(LOGGER, logging.DEBUG)) def copyFile(self, file_id, dest_id): @@ -76,7 +75,6 @@ def copyFile(self, file_id, dest_id): else: raise err - def cloneFolder(self, name, local_path, folder_id, parent_id): files = self.getFilesByFolderId(folder_id) new_id = None @@ -85,7 +83,7 @@ def cloneFolder(self, name, local_path, folder_id, parent_id): for file in files: if file.get('mimeType') == self.__G_DRIVE_DIR_MIME_TYPE: file_path = os.path.join(local_path, file.get('name')) - current_dir_id = self.create_directory(file.get('name')) + current_dir_id = self.create_directory(file.get('name'), parent_id) new_id = self.cloneFolder(file.get('name'), file_path, file.get('id'), current_dir_id) else: try: @@ -101,15 +99,18 @@ def cloneFolder(self, name, local_path, folder_id, parent_id): @retry(wait=wait_exponential(multiplier=2, min=3, max=6), stop=stop_after_attempt(5), retry=retry_if_exception_type(HttpError), before=before_log(LOGGER, logging.DEBUG)) - def create_directory(self, directory_name): - file_metadata = { - "name": directory_name, - "mimeType": self.__G_DRIVE_DIR_MIME_TYPE - } - file_metadata["parents"] = [self.__parent_id] - file = self.__service.files().create(supportsTeamDrives=True, body=file_metadata).execute() - file_id = file.get("id") - return file_id + def create_directory(self, directory_name, parent_id=None): + file_metadata = { + "name": directory_name, + "mimeType": self.__G_DRIVE_DIR_MIME_TYPE + } + if parent_id is not None: + file_metadata["parents"] = [parent_id] + else: + file_metadata["parents"] = [self.__parent_id] + file = self.__service.files().create(supportsTeamDrives=True, body=file_metadata).execute() + file_id = file.get("id") + return file_id def clone(self, link): self.transferred_size = 0 @@ -134,7 +135,6 @@ def clone(self, link): LOGGER.error(err) return f"**ERROR:** ```{err}```" - @retry(wait=wait_exponential(multiplier=2, min=3, max=6), stop=stop_after_attempt(5), retry=retry_if_exception_type(HttpError), before=before_log(LOGGER, logging.DEBUG)) def upload_file(self, file_path, mimeType=None): @@ -219,4 +219,4 @@ def emptyTrash(self): def authorize(self, creds): - return build('drive', 'v3', credentials=creds, cache_discovery=False) \ No newline at end of file + return build('drive', 'v3', credentials=creds, cache_discovery=False)