Skip to main content

从数据库中的本地文件同步元数据

项目描述

使用此应用程序搜索本地文件并索引一些元数据信息以及数据库中文件(文本)的内容(FileMetadata 模型)。可以将多个目录配置为索引(settings.FILEMETADATA_LOOKUP_DIRS)。使用数据库中注册的信息,可以使用 django 的功能(过滤器、导出等)或其他应用程序来操作数据。例如,此应用程序可用作实现受保护的下载页面或在搜索工具中搜索文件内容的基础。

Python 3.6+ 支持此版本;和 Django 2.2+。

安装

使用 pip 安装软件包:

$ pip install django-filemetadata

INSTALLED_APPS中添加 App

INSTALLED_APPS=(
    "django-filemetadata",
),

执行 makemigrations/migrate。

配置

配置目录以在设置中查找文件:

FILEMETADATA_LOOKUP_DIRS=['/folder1/folder2', '/folder3/folder4']

利用率

使用管理命令索引数据

usage:  filemetadata_index [-f FOLDERS] [-c] [-d] [-s] [-x] [-n] [-a]

Update the the file-metadata found in the directories into the DB.

optional arguments:
  -f FOLDERS            Folder(s) to index (coma separated)
  -c                    Clear the data before reindex
  -d                    Delete only the data from these folders and exit
  -s                    Index the symlinks (Do not follow it)
  -x                    Extract the content of the file (text)
  -n                    Non-reentrant mode (Not recursive)
  -a                    Abort on errors

例如

重新索引设置中配置的文件

python 文件元数据索引

或通知目录

python filemetadata_index -f /folder1/folder2,/folder3

或者只是从这些文件夹中删除数据(在这种情况下不是递归的):

python filemetadata_index -d -n -f /folder1/folder2,/folder3

转到 Admin 并检查 FileMetadata 模型中的数据。

定制

支持 .pdf 文件 此应用程序与“PyPDF4”库兼容。如果安装了它,它可用于在必要时从 pdf 文件中提取内容。

自定义提取器 如果需要,可以通过更具体的功能覆盖提取文件内容的功能。为此,请在 indexer.py 模块中重载函数 'func_extract_text'

from filemetadata import indexer

def my_extractor(posixpath_obj):
    ...
    return file_content

indexer.func_extract_text = my_extractor

或 FileIndexer 类的 extract_text 方法

from filemetadata.indexer import FileIndexer

class MyFileIndexer(FileIndexer):
  def extract_text(self, file_obj):
    ...
    return file_content

测试

运行测试

python load_tests.py

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

django-filemetadata-1.0.4.tar.gz (17.6 kB 查看哈希)

已上传 source