file2text 提供压缩包/文档/图片转文本功能。
下方介绍的 25065 文档转文本服务是 xyz-antispam 的一个模块;
独立 AppImage 命令行工具是另一套运行方式, 不属于该服务器模块。
解出的文本不关注样式, 不适合展示, 适用于机器学习, 分词等 效果演示
下载: https://share.weiyun.com/KuDHQn8I
本 AppImage 使用 bwrap 在沙箱隔离环境中解析文件, 只将指定输入文件只读提供给解析程序。
运行
file2text-*.AppImage:
# 默认输出 JSON ./file2text-x86_64-YYYYMMDDHHmmss_64.AppImage /absolute/path/document.docx # 输出纯文本 ./file2text-x86_64-YYYYMMDDHHmmss_64.AppImage /absolute/path/document.pdf -file2text_cmd_text yes # 也可以将文件内容通过标准输入传入 cat /absolute/path/document.pdf | ./file2text-x86_64-YYYYMMDDHHmmss_64.AppImage - -file2text_cmd_text yes # 查看可用选项 ./file2text-x86_64-YYYYMMDDHHmmss_64.AppImage help
如果只使用 "文档转文本服务", 可以删除 milter.cf, spamd.cf
/home/spam/etc/service/file2text.cf
# 服务程序及监听地址 server-command = bin/xyz-antispam server-service = 127.0.0.1:25065 server-proc-count = 1 # fork 调用并发数、排队上限;队列满时停止接收新请求 server-fork-call-concurrency = 1 server-fork-call-queue-limit = 32 server-fork-call-stop-accept-when-full = yes # file2text 独立服务不使用反垃圾分类器 antispam_classifier_disabled = yes # 额外按普通文本处理的格式、额外以 fork 模式处理的格式、禁用解析的格式 # 格式名以文件后缀表示,多个格式用逗号分隔 # file2text_extra_text_formats = js,css,hpp,cpp,h # file2text_extra_robust_formats = iso,zip # file2text_disabled_formats = zip,doc,docx,xls,xlsx,ppt,pptx # 是否启用严格(fork)模式,以及是否启用反垃圾模式 file2text_robust_mode_enabled = no file2text_antispam_mode_enabled = no # 递归解析限制 file2text_resource_size_limit = 1024102400 file2text_resource_count_limit = 10240 file2text_recursion_time_limit_isolated = 0 file2text_recursion_depth_limit = 3 file2text_recursion_node_count_limit = 10240 file2text_recursion_node_text_size_limit = 10240 file2text_recursion_all_text_size_limit = 102400 file2text_recursion_ocr_node_char_count_limit = 10240 # file2text_recursion_image_file_count_limit = 6 # 图片处理;缓存目录相对于服务进程的工作目录 file2text_image_preload_cache_disabled = no file2text_image_enabled = yes file2text_image_cache_dir = ./f2t_cache_image_dir/ file2text_image_time_limit = 100 file2text_image_width_height_sum_limit = 0 file2text_image_size_limit = 0 # 图片 OCR 识别 file2text_image_ocr_enabled = yes # file2text_image_ocr_char_count_limit = 500 # file2text_image_ocr_width_limit = 3000 # file2text_image_ocr_slice_min_size_limit = 0 # file2text_image_ocr_height_per_slice = 100 # file2text_image_ocr_height_slice_limit = 3 # 图片二维码识别 file2text_image_qrcode_enabled = yes file2text_image_qrcode_width_height_sum_limit = 0
1, 客户端 连接(2种方法):
1) socket 文件 ./var/socket/file2text
2) 端口 0:25065 ### 默认配置
2, 客户端 输入(3种方法): ### 请注意, 服务在docker内运行
1) file2json_from_file[一个空格]文件路径\r\n
2) file2json_from_tree[一个空格]文件名后缀[一个空格]文件路径\r\n
2) file2json_from_tree[一个空格]文件名后缀[一个空格]字符集[一个空格]文件路径\r\n
3) file2json_from_tree[一个空格]文件名后缀[一个空格]{数据长度}\r\n文件数据\r\n
3) file2json_from_tree[一个空格]文件名后缀[一个空格]字符集[一个空格]{数据长度}\r\n文件数据\r\n
3, 服务端 返回(2个情况):
1) OK[一个空格]{数据(JSON)的长度}\r\nJSON数据\r\n
2) ERROR[一个空格]error_msg\r\n
4, 客户端 继续(2种):
1)关闭连接
2)回到 2
{
"name": "1.doc", # [必填:string] 文件名
"format": "doc", # [必填:string] 文件格式后缀
"size": 12345, # [必填:int] 文件字节数
"text_utf8": "提取的文本", # [必填:string] 提取的文本内容, UTF-8 编码
"urls": ["https://example.com"], # [必填:array] 从文本中提取的 URL 字符串数组
"actually_format": "docx", # [可选:string] 格式探测修正后的实际格式
"actually_name": "1.docx", # [可选:string] 实际文件名
"image_width": 800, # [可选:int] 图片宽度(像素), 仅图片节点出现
"image_height": 600, # [可选:int] 图片高度(像素), 仅图片节点出现
"is_detected": 1, # [可选:int=1] 格式被自动探测并修正
"is_malicious": 1, # [可选:int=1] 文件被判为恶意
"is_high_risk": 1, # [可选:int=1] 文件被判为高风险
"is_encrypted": 1, # [可选:int=1] 文件已加密
"is_damaged": 1, # [可选:int=1] 文件已损坏
"is_error": 1, # [可选:int=1] 解析出错
"is_no_handler": 1, # [可选:int=1] 无对应解析器
"resources": [] # [可选:array] 子资源列表, 元素结构与当前节点相同
}
输出为递归树, 根节点和 resources 中每个子节点结构相同。
可选字段仅在相应情况出现; is_* 字段仅在值为 1 时出现。
命令 /home/spam/bin/test_file2text.sh 用于在线测试 file2text 服务
可以在容器外执行
# docker cp container_name:/home/spam/bin/test_file2text.sh /somepath/ ./test_file2text.sh [ 127.0.0.1:25065 ] [ file_type(doc/ppt/zip/...) ] file [ file1 [ ... ] ] [ dir [ ...] ]