From b2b22db261dea69001b2ad3e4ecf5af664984b28 Mon Sep 17 00:00:00 2001 From: JoinTyang <41655440+JoinTyang@users.noreply.github.com> Date: Mon, 15 Jul 2024 17:00:10 +0800 Subject: [PATCH] metadata add file type (#340) --- app/config.py | 1 + repo_metadata/metadata_server_api.py | 39 ------------ repo_metadata/repo_metadata.py | 7 ++- repo_metadata/utils.py | 91 ++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 40 deletions(-) create mode 100644 repo_metadata/utils.py diff --git a/app/config.py b/app/config.py index e84332ec..b7d48a97 100644 --- a/app/config.py +++ b/app/config.py @@ -23,6 +23,7 @@ METADATA_SERVER_SECRET_KEY = getattr(seahub_settings, 'METADATA_SERVER_SECRET_KEY', '') METADATA_SERVER_URL = getattr(seahub_settings, 'METADATA_SERVER_URL', '') ENABLE_METADATA_MANAGEMENT = getattr(seahub_settings, 'ENABLE_METADATA_MANAGEMENT', False) + METADATA_FILE_TYPES = getattr(seahub_settings, 'METADATA_FILE_TYPES', {}) except ImportError: logger.critical("Can not import seahub settings.") raise RuntimeError("Can not import seahub settings.") diff --git a/repo_metadata/metadata_server_api.py b/repo_metadata/metadata_server_api.py index 0657c787..1a66525e 100644 --- a/repo_metadata/metadata_server_api.py +++ b/repo_metadata/metadata_server_api.py @@ -3,45 +3,6 @@ from seafevents.app.config import METADATA_SERVER_SECRET_KEY, METADATA_SERVER_URL -class MetadataTable(object): - def __init__(self, table_id, name): - self.id = table_id - self.name = name - - @property - def columns(self): - return MetadataColumns() - - -class MetadataColumns(object): - def __init__(self): - self.id = MetadataColumn('_id', '_id', 'text') - self.file_creator = MetadataColumn('_file_creator', '_file_creator', 'text') - self.file_ctime = MetadataColumn('_file_ctime', '_file_ctime', 'date') - self.file_modifier = MetadataColumn('_file_modifier', '_file_modifier', 'text') - self.file_mtime = MetadataColumn('_file_mtime', '_file_mtime', 'date') - self.parent_dir = MetadataColumn('_parent_dir', '_parent_dir', 'text') - self.file_name = MetadataColumn('_name', '_name', 'text') - self.is_dir = MetadataColumn('_is_dir', '_is_dir', 'checkbox') - - -class MetadataColumn(object): - def __init__(self, key, name, type): - self.key = key - self.name = name - self.type = type - - def to_dict(self): - return { - 'key': self.key, - 'name': self.name, - 'type': self.type - } - - -METADATA_TABLE = MetadataTable('0001', 'Table1') - - def parse_response(response): if response.status_code >= 400 or response.status_code < 200: raise ConnectionError(response.status_code, response.text) diff --git a/repo_metadata/repo_metadata.py b/repo_metadata/repo_metadata.py index ebe9c2df..07702480 100644 --- a/repo_metadata/repo_metadata.py +++ b/repo_metadata/repo_metadata.py @@ -1,7 +1,7 @@ import os import logging -from seafevents.repo_metadata.metadata_server_api import METADATA_TABLE +from seafevents.repo_metadata.utils import METADATA_TABLE, get_file_type_by_name from seafevents.utils import timestamp_to_isoformat_timestr logger = logging.getLogger(__name__) @@ -49,6 +49,7 @@ def add_files(self, repo_id, added_files): parent_dir = os.path.dirname(path) file_name = os.path.basename(path) modifier = de.modifier + file_type = get_file_type_by_name(file_name) if self.is_excluded_path(path): continue @@ -62,6 +63,9 @@ def add_files(self, repo_id, added_files): METADATA_TABLE.columns.file_name.name: file_name, METADATA_TABLE.columns.is_dir.name: False, } + + if file_type: + row[METADATA_TABLE.columns.file_type.name] = file_type rows.append(row) if not rows: return @@ -436,6 +440,7 @@ def init_columns(self, repo_id): self.metadata_server_api.add_column(repo_id, METADATA_TABLE.id, METADATA_TABLE.columns.parent_dir.to_dict()) self.metadata_server_api.add_column(repo_id, METADATA_TABLE.id, METADATA_TABLE.columns.file_name.to_dict()) self.metadata_server_api.add_column(repo_id, METADATA_TABLE.id, METADATA_TABLE.columns.is_dir.to_dict()) + self.metadata_server_api.add_column(repo_id, METADATA_TABLE.id, METADATA_TABLE.columns.file_type.to_dict()) def create_base(self, repo_id): self.metadata_server_api.create_base(repo_id) diff --git a/repo_metadata/utils.py b/repo_metadata/utils.py new file mode 100644 index 00000000..3c3db185 --- /dev/null +++ b/repo_metadata/utils.py @@ -0,0 +1,91 @@ +import os +import random + +from seafevents.app.config import METADATA_FILE_TYPES + + +def gen_fileext_type_map(): + """ + Generate previewed file extension and file type relation map. + """ + ext_to_type = {} + for file_type in list(METADATA_FILE_TYPES.keys()): + for file_ext in METADATA_FILE_TYPES.get(file_type): + ext_to_type[file_ext] = file_type + + return ext_to_type + + +FILEEXT_TYPE_MAP = gen_fileext_type_map() + + +def get_file_type_by_name(filename): + file_ext = os.path.splitext(filename)[1][1:].lower() + file_type = FILEEXT_TYPE_MAP.get(file_ext) + return file_type + + +def gen_select_options(option_names): + options = [] + + id_set = set() + for option_name in option_names: + option_id = gen_option_id(id_set) + options.append({'id': option_id, 'name': option_name}) + id_set.add(option_id) + return options + + +def gen_option_id(id_set): + _id = str(random.random() * (10 ** 6)) + + while True: + if _id not in id_set: + return _id + _id = str(random.random() * (10 ** 6)) + + +class MetadataTable(object): + def __init__(self, table_id, name): + self.id = table_id + self.name = name + + @property + def columns(self): + return MetadataColumns() + + +class MetadataColumns(object): + def __init__(self): + self.id = MetadataColumn('_id', '_id', 'text') + self.file_creator = MetadataColumn('_file_creator', '_file_creator', 'text') + self.file_ctime = MetadataColumn('_file_ctime', '_file_ctime', 'date') + self.file_modifier = MetadataColumn('_file_modifier', '_file_modifier', 'text') + self.file_mtime = MetadataColumn('_file_mtime', '_file_mtime', 'date') + self.parent_dir = MetadataColumn('_parent_dir', '_parent_dir', 'text') + self.file_name = MetadataColumn('_name', '_name', 'text') + self.is_dir = MetadataColumn('_is_dir', '_is_dir', 'checkbox') + self.file_type = MetadataColumn('_file_type', '_file_type', 'single-select', + {'options': gen_select_options(list(METADATA_FILE_TYPES.keys()))}) + + +class MetadataColumn(object): + def __init__(self, key, name, type, data=None): + self.key = key + self.name = name + self.type = type + self.data = data + + def to_dict(self): + column_data = { + 'key': self.key, + 'name': self.name, + 'type': self.type, + } + if self.data: + column_data['data'] = self.data + + return column_data + + +METADATA_TABLE = MetadataTable('0001', 'Table1')