diff --git a/clean_s3_user.py b/clean_s3_user.py index 8be1989..62c9f27 100644 --- a/clean_s3_user.py +++ b/clean_s3_user.py @@ -106,7 +106,8 @@ class CleanS3User: """ conn = {} - if not {"dbtype", "dbhost", "dbport", "dbuser", "dbpassword", "dbname"}.issubset(self.nxc_conf.keys()): + if not {"dbtype", "dbhost", "dbport", "dbuser", "dbpassword", "dbname", "dbtableprefix"}\ + .issubset(self.nxc_conf.keys()): self.logger.error("🛑 Missing Nextcloud database configuration") raise ValueError("Missing Nextcloud database configuration") elif self.nxc_conf["dbtype"] != "mysql": @@ -185,12 +186,12 @@ class CleanS3User: :return: The user Nextcloud UID """ email = self.db.escape_string(email) - cursor.execute('SELECT uid FROM oc_accounts WHERE data LIKE \'%"email":{"value":"'+email+'","%\'') + cursor.execute(f"SELECT uid FROM {self.nxc_conf['dbtableprefix']}accounts WHERE data LIKE ?", + ('%"email":{"value":"'+email+'","%',)) res = cursor.next() return res[0] if res else None - @staticmethod - def all_users(cursor, exclude: [str] = None) -> [str]: + def all_users(self, cursor, exclude: [str] = None) -> [str]: """ Return all Nextcloud users UID :param cursor: The database cursor @@ -200,7 +201,7 @@ class CleanS3User: if exclude is None: exclude = [] - cursor.execute("SELECT uid FROM oc_accounts") + cursor.execute(f"SELECT uid FROM {self.nxc_conf['dbtableprefix']}accounts") return list(filter(lambda u: u not in exclude, map(lambda u: u[0], cursor))) def transfer_data(self, cursor, user: str, dest_user: str): @@ -217,9 +218,9 @@ class CleanS3User: # Create or replace a temporary table to store transferring shares information "SELECT sh.id, CONCAT('files/transferred from ', CONCAT(REPLACE(sh.uid_owner, '%', '\%'), ' on%/', REPLACE(SUBSTR(fc.path, 7), '%', '\%'))) as new_path " # Store share id and determine the new path of the share with % escape to avoid interfering LIKE - "FROM oc_share sh " + f"FROM {self.nxc_conf['dbtableprefix']}share sh " # Get all share - "INNER JOIN oc_filecache fc ON fc.fileid = sh.file_source " + f"INNER JOIN {self.nxc_conf['dbtableprefix']}filecache fc ON fc.fileid = sh.file_source " # Join the filecache of each share to get the current path "WHERE sh.uid_owner = ?", # Filter share owner for source user @@ -232,13 +233,13 @@ class CleanS3User: self.logger.info(f"🩹 Transferring {user}'s shares to {dest_user}") self.db.commit() # Get database modification from the ownership transfer - cursor.execute("UPDATE oc_share sh " + cursor.execute(f"UPDATE {self.nxc_conf['dbtableprefix']}share sh " # Get all shares "INNER JOIN save_shares save ON save.id = sh.id " # Join to have only saved shares in transfer - "INNER JOIN oc_filecache fc ON fc.path LIKE save.new_path " + f"INNER JOIN {self.nxc_conf['dbtableprefix']}filecache fc ON fc.path LIKE save.new_path " # Get filecache of each share from new path - "INNER JOIN oc_storages s ON s.numeric_id = fc.storage AND s.id = CONCAT('object::user:', ?) " + f"INNER JOIN {self.nxc_conf['dbtableprefix']}storages s ON s.numeric_id = fc.storage AND s.id = CONCAT('object::user:', ?) " # Get storage of filecache to filter only destination user files "SET sh.uid_owner = SUBSTR(s.id, 14), sh.uid_initiator = SUBSTR(s.id, 14), " "sh.item_source = fc.fileid, sh.file_source = fc.fileid " @@ -250,7 +251,7 @@ class CleanS3User: self.logger.info(f"🩹 Removing {user} shares to {dest_user}") cursor.execute("DELETE sh.* " # Rome share - "FROM oc_share sh " + f"FROM {self.nxc_conf['dbtableprefix']}share sh " # Get all shares "INNER JOIN save_shares save ON save.id = sh.id " # Join to have only saved shares in transfer @@ -259,8 +260,7 @@ class CleanS3User: (dest_user, )) self.db.commit() - @staticmethod - def get_user_cache(cursor, user: str): + def get_user_cache(self, cursor, user: str): """ Get the user cache :param cursor: The database cursor @@ -268,8 +268,8 @@ class CleanS3User: :return: The database cursor with an executed statement """ cursor.execute("SELECT fc.* " - "FROM oc_filecache fc " - "INNER JOIN oc_storages s ON s.numeric_id = fc.storage " + f"FROM {self.nxc_conf['dbtableprefix']}filecache fc " + f"INNER JOIN {self.nxc_conf['dbtableprefix']}storages s ON s.numeric_id = fc.storage " "WHERE s.id = ?", (f"object::user:{user}",)) return cursor @@ -307,8 +307,8 @@ class CleanS3User: :return: List of S3 keys """ cursor.execute(f"SELECT CONCAT('{self.nxt_s3_key_prefix}', fc.fileid) " - "FROM oc_filecache fc " - "INNER JOIN oc_storages s ON s.numeric_id = fc.storage " + f"FROM {self.nxc_conf['dbtableprefix']}filecache fc " + f"INNER JOIN {self.nxc_conf['dbtableprefix']}storages s ON s.numeric_id = fc.storage " "WHERE s.id = ?", (f"object::user:{user}",)) return list(map(lambda k: k[0], cursor))