Use Nextcloud configuration DB prefix

master
Florian Charlaix 3 years ago
parent 0d4d45bbd6
commit 7d4d1ac3fe
  1. 34
      clean_s3_user.py

@ -106,7 +106,8 @@ class CleanS3User:
""" """
conn = {} 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") self.logger.error("🛑 Missing Nextcloud database configuration")
raise ValueError("Missing Nextcloud database configuration") raise ValueError("Missing Nextcloud database configuration")
elif self.nxc_conf["dbtype"] != "mysql": elif self.nxc_conf["dbtype"] != "mysql":
@ -185,12 +186,12 @@ class CleanS3User:
:return: The user Nextcloud UID :return: The user Nextcloud UID
""" """
email = self.db.escape_string(email) 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() res = cursor.next()
return res[0] if res else None return res[0] if res else None
@staticmethod def all_users(self, cursor, exclude: [str] = None) -> [str]:
def all_users(cursor, exclude: [str] = None) -> [str]:
""" """
Return all Nextcloud users UID Return all Nextcloud users UID
:param cursor: The database cursor :param cursor: The database cursor
@ -200,7 +201,7 @@ class CleanS3User:
if exclude is None: if exclude is None:
exclude = [] 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))) 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): 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 # 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 " "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 # 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 # 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 # Join the filecache of each share to get the current path
"WHERE sh.uid_owner = ?", "WHERE sh.uid_owner = ?",
# Filter share owner for source user # Filter share owner for source user
@ -232,13 +233,13 @@ class CleanS3User:
self.logger.info(f"🩹 Transferring {user}'s shares to {dest_user}") self.logger.info(f"🩹 Transferring {user}'s shares to {dest_user}")
self.db.commit() # Get database modification from the ownership transfer 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 # Get all shares
"INNER JOIN save_shares save ON save.id = sh.id " "INNER JOIN save_shares save ON save.id = sh.id "
# Join to have only saved shares in transfer # 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 # 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 # 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), " "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 " "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}") self.logger.info(f"🩹 Removing {user} shares to {dest_user}")
cursor.execute("DELETE sh.* " cursor.execute("DELETE sh.* "
# Rome share # Rome share
"FROM oc_share sh " f"FROM {self.nxc_conf['dbtableprefix']}share sh "
# Get all shares # Get all shares
"INNER JOIN save_shares save ON save.id = sh.id " "INNER JOIN save_shares save ON save.id = sh.id "
# Join to have only saved shares in transfer # Join to have only saved shares in transfer
@ -259,8 +260,7 @@ class CleanS3User:
(dest_user, )) (dest_user, ))
self.db.commit() self.db.commit()
@staticmethod def get_user_cache(self, cursor, user: str):
def get_user_cache(cursor, user: str):
""" """
Get the user cache Get the user cache
:param cursor: The database cursor :param cursor: The database cursor
@ -268,8 +268,8 @@ class CleanS3User:
:return: The database cursor with an executed statement :return: The database cursor with an executed statement
""" """
cursor.execute("SELECT fc.* " cursor.execute("SELECT fc.* "
"FROM oc_filecache fc " f"FROM {self.nxc_conf['dbtableprefix']}filecache fc "
"INNER JOIN oc_storages s ON s.numeric_id = fc.storage " f"INNER JOIN {self.nxc_conf['dbtableprefix']}storages s ON s.numeric_id = fc.storage "
"WHERE s.id = ?", "WHERE s.id = ?",
(f"object::user:{user}",)) (f"object::user:{user}",))
return cursor return cursor
@ -307,8 +307,8 @@ class CleanS3User:
:return: List of S3 keys :return: List of S3 keys
""" """
cursor.execute(f"SELECT CONCAT('{self.nxt_s3_key_prefix}', fc.fileid) " cursor.execute(f"SELECT CONCAT('{self.nxt_s3_key_prefix}', fc.fileid) "
"FROM oc_filecache fc " f"FROM {self.nxc_conf['dbtableprefix']}filecache fc "
"INNER JOIN oc_storages s ON s.numeric_id = fc.storage " f"INNER JOIN {self.nxc_conf['dbtableprefix']}storages s ON s.numeric_id = fc.storage "
"WHERE s.id = ?", "WHERE s.id = ?",
(f"object::user:{user}",)) (f"object::user:{user}",))
return list(map(lambda k: k[0], cursor)) return list(map(lambda k: k[0], cursor))

Loading…
Cancel
Save