邹景立 d874d1baa7 fix(文件): 修复自定义表前缀下的重复文件清理
背景与目的:去重子查询写死 system_file.id,配置表前缀后会引用不存在的列,导致清理失败。

主要改动:从实际删除查询获取表名,使用 whereColumn 关联派生表中的保留记录;补充去重范围、保留规则及异常行为说明。

影响范围与兼容性:仍只清理当前管理员的非安全重复记录,按存储类型和路径保留最大 ID;不删除存储对象,保留成功响应与查询异常。

验证入口及实际结果:临时应用使用本项目控制器、依赖及内存 SQLite。旧代码在 tenant_ 前缀下复现列不存在;当前代码在空前缀和 tenant_ 下均通过分组去重、管理员隔离、安全文件保护、重复执行、空集合与响应检查。PHP 语法及差异检查通过,本次未运行真实 MySQL 验收。
2026-10-19 09:35:16 +08:00

122 lines
3.7 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
/**
* +----------------------------------------------------------------------
* | ThinkAdmin Plugin for ThinkAdmin
* +----------------------------------------------------------------------
* | 版权所有 2014~2026 ThinkAdmin [ thinkadmin.top ]
* +----------------------------------------------------------------------
* | 官方网站: https://thinkadmin.top
* +----------------------------------------------------------------------
* | 开源协议 ( https://mit-license.org )
* | 免责声明 ( https://thinkadmin.top/disclaimer )
* | 会员特权 ( https://thinkadmin.top/vip-introduce )
* +----------------------------------------------------------------------
* | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
* | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
* +----------------------------------------------------------------------
*/
namespace app\admin\controller;
use think\admin\Controller;
use think\admin\helper\QueryHelper;
use think\admin\model\SystemFile;
use think\admin\service\AdminService;
use think\admin\Storage;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
/**
* 系统文件管理.
* @class File
*/
class File extends Controller
{
/**
* 存储类型.
* @var array
*/
protected $types;
/**
* 系统文件管理.
* @auth true
* @menu true
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function index()
{
SystemFile::mQuery()->layTable(function () {
$this->title = '系统文件管理';
$this->xexts = SystemFile::mk()->distinct()->column('xext');
}, static function (QueryHelper $query) {
$query->like('name,hash,xext')->equal('type')->dateBetween('create_at');
$query->where(['issafe' => 0, 'status' => 2, 'uuid' => AdminService::getUserId()]);
});
}
/**
* 编辑系统文件.
* @auth true
*/
public function edit()
{
SystemFile::mForm('form');
}
/**
* 删除系统文件.
* @auth true
*/
public function remove()
{
if (!AdminService::isSuper()) {
$where = ['uuid' => AdminService::getUserId()];
}
SystemFile::mDelete('', $where ?? []);
}
/**
* 清理当前管理员的非安全重复文件,按存储类型和路径保留最大编号。
* 仅删除数据库记录;查询失败时传递异常,不删除存储中的文件。
* @auth true
* @throws DbException
*/
public function distinct()
{
$map = ['issafe' => 0, 'uuid' => AdminService::getUserId()];
// 使用派生表包装子查询,避免直接引用同一表
$keepSubQuery = SystemFile::mk()->fieldRaw('MAX(id) AS id')->where($map)->group('type, xkey')->buildSql();
// 使用 whereNotExists 配合派生表子查询删除,避免 1093 错误和 whereIn
$deleteQuery = SystemFile::mk()->where($map);
$table = $deleteQuery->getTable();
$deleteQuery->whereNotExists(function ($query) use ($keepSubQuery, $table) {
$query->table("({$keepSubQuery})")->alias('f2')->whereColumn('f2.id', $table . '.id');
})->delete();
$this->success('清理重复文件成功!');
}
/**
* 控制器初始化.
*/
protected function initialize()
{
$this->types = Storage::types();
}
/**
* 数据列表处理.
*/
protected function _page_filter(array &$data)
{
foreach ($data as &$vo) {
$vo['ctype'] = $this->types[$vo['type']] ?? $vo['type'];
}
}
}