net.mingsoft.clean.biz.impl.TableBizImpl Maven / Gradle / Ivy
The newest version!
/**
The MIT License (MIT) * Copyright (c) 2019 铭飞科技
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package net.mingsoft.clean.biz.impl;
import cn.hutool.core.util.ObjectUtil;
import net.mingsoft.base.biz.impl.BaseBizImpl;
import net.mingsoft.base.dao.IBaseDao;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.clean.biz.ITableBiz;
import net.mingsoft.clean.dao.ITableDao;
import net.mingsoft.clean.entity.TableEntity;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.util.regex.Pattern.compile;
/**
* 文件清理管理持久化层
* @author 铭飞开源团队
* 创建日期:2019-11-21 17:08:10
* 历史修订:
*/
@Service("cleanTableBizImpl")
public class TableBizImpl extends BaseBizImpl implements ITableBiz {
private static final Logger log = LoggerFactory.getLogger(TableBizImpl.class);
@Autowired
private AnnotationConfigServletWebServerApplicationContext applicationContext;
@Autowired
private ITableDao tableDao;
/**
* 上传路径
*/
@Value("${ms.upload.path}")
private String uploadFloderPath;
/**
* 上传路径映射
*/
@Value("${ms.upload.mapping}")
private String uploadMapping;
/**
* 上传路径映射
*/
@Value("${ms.clean.pattern:/(upload/.*?(png|gif|bmp|pdf|mp4|avi|jpg|gif))}")
private String cleanFilePattern;
/**
* 寻找文件正则
*/
private static Pattern filePattern;
@PostConstruct
public void init()
{
// filePattern= compile("/(upload/.*?(png|gif|bmp|pdf|mp4|avi|jpg|gif))");
filePattern= compile(cleanFilePattern);
}
@Override
protected IBaseDao getDao() {
// TODO Auto-generated method stub
return tableDao;
}
@Override
public void clearFile() {
List list = queryAll();
List fileList=new ArrayList();
File relFile = new File(uploadFloderPath);
if (relFile.isAbsolute()) {
findFiles(relFile,fileList);
}else {
findFiles(new File(getRealPath(uploadFloderPath)),fileList);
findFiles(new File(getRealPath("ueditor")),fileList);
}
long startTime=System.currentTimeMillis();
List exitFiles = new ArrayList<>();
//获取文件上传路径
String uploadFile=relFile.isAbsolute()?relFile.toString():getRealPath("");
for (TableEntity tableEntity : list) {
//自定义表
if(StringUtils.isNotBlank(tableEntity.getCleanField())){
String[] fields = tableEntity.getCleanField().split(",");
try {
exitFiles.addAll(tableTraList(tableEntity.getCleanTable(),fields ,uploadFile));
}catch (Exception e){
log.error("清理错误:",e);
}
}
}
int deleFile = deleFile(fileList, exitFiles);
long endTime=System.currentTimeMillis();
log.info("当前程序耗时 {}ms,找到{}个文件,需验证{}个文件,删除{}个文件",
(endTime-startTime),
list.size(),
exitFiles.size(),
deleFile);
}
private int deleFile(List source,List exitFile){
int i= 0;
for (File file : source) {
//判断文件是否存在数据库里
if(!exitFile.contains(file.getPath())){
i++;
file.delete();
}
}
return i;
}
/**
* 通过表名找到表中所有文件
* @param table 表名
* @param fields 字段数组 字段类型 json为element上传组件的json,需要有path字段,string为富文本内容,path单纯路径
* @param rootPath
* @return
*/
public List tableTraList(String table,String[] fields,String rootPath) {
List paths=new ArrayList<>();
List