mindoc-org / mindoc

Golang实现的基于beego框架的接口在线文档管理系统
https://mindoc.com.cn/docs/mindochelp
Apache License 2.0
7.35k stars 1.91k forks source link

无用附件清理(系统文件和数据库记录) #917

Closed ywz978020607 closed 11 months ago

ywz978020607 commented 11 months ago

你可以在db文件同级目录放置此python文件,然后执行python xxx.py即可,可同时删除目录和不存在的空记录: (go语言这个beego框架不熟悉,希望作者有时间能支持一下这个功能。项目运行久了会有大量文件及数据冗余)

import sqlite3
import os, glob

conn = sqlite3.connect("mindoc.db")
cur = conn.cursor()         #通过建立数据库游标对象,准备读写操作

cmd = """
SELECT 
    att.http_path
FROM
    md_attachment AS att
WHERE (att.document_id != 0 OR (NOT EXISTS( SELECT 1 FROM md_documents WHERE markdown LIKE ("%" || att.http_path || "%"))))
AND (att.document_id = 0 OR (NOT EXISTS( SELECT 1 FROM md_documents WHERE att.document_id = document_id )))
"""
cur.execute(cmd)
file_list = cur.fetchall()
for file_item in file_list:
    item_path = file_item[0]
    # 1. 删除os文件
    if os.path.exists(os.path.join("..", item_path[1:])):
        os.remove(os.path.join("..", item_path[1:]))

    # 2. 查询os是否删除成功,成功则删除附件记录
    if not os.path.exists(os.path.join("..", item_path[1:])):
        cmd = """
        delete
        from md_attachment
        WHERE http_path = '{}'
        """.format(item_path)
        cur.execute(cmd)
conn.commit()   #保存提交,确保数据保存成功

conn.close()        #关闭与数据库的连接
ywz978020607 commented 11 months ago

支持编译版本:https://github.com/ywz978020607/mindoc/releases/tag/attach_clean_change_theme