月球大数据地理空间分析展示平台-【后端】-月球后台服务
1
13693261870
2024-11-17 796b44ea813a1133beae4f3a67f1c0263510c0c7
src/main/java/com/moon/server/service/all/UploadAttachService.java
@@ -2,20 +2,17 @@
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moon.server.controller.all.BaseController;
import com.moon.server.entity.all.ResponseMsg;
import com.moon.server.entity.all.SettingData;
import com.moon.server.entity.all.*;
import com.moon.server.entity.ctrl.KeyValueEntity;
import com.moon.server.entity.data.DownloadEntity;
import com.moon.server.entity.data.MetaEntity;
import com.moon.server.entity.data.MetaFileEntity;
import com.moon.server.entity.sys.AttachEntity;
import com.moon.server.entity.sys.UserEntity;
import com.moon.server.helper.*;
import com.moon.server.mapper.all.BasicMapper;
import com.moon.server.mapper.data.DownloadMapper;
import com.moon.server.service.sys.AttachService;
import com.moon.server.entity.all.BaseEntity;
import com.moon.server.entity.all.StaticData;
import com.moon.server.helper.*;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.model.FileHeader;
import net.lingala.zip4j.model.ZipParameters;
@@ -31,11 +28,8 @@
import java.lang.reflect.Field;
import java.util.*;
/**
 * 上传附件服务类
 * @author WWW
 */
@Service
@SuppressWarnings("ALL")
public class UploadAttachService {
    @Autowired
    PathHelper pathHelper;
@@ -52,16 +46,10 @@
    private final static Log log = LogFactory.getLog(UploadAttachService.class);
    /**
     * 获取表名
     */
    public static String getTabs() {
        return tabs;
    }
    /**
     * 初始化附件表
     */
    public static void init(String cfg) {
        if (StringHelper.isEmpty(cfg)) {
            return;
@@ -84,9 +72,6 @@
        tabs = StringHelper.join(keys, ",");
    }
    /**
     * 上传文件
     */
    public ResponseMsg<String> upload(UserEntity ue, String tab, MultipartFile file, BaseController ctrl) {
        try {
            if (file == null && file.isEmpty()) {
@@ -96,14 +81,12 @@
                return ctrl.fail(String.format("文件大于 %d MB", SettingData.MAX_FILE_SIZE / 1024 / 1024), null);
            }
            // 传输文件
            String oldName = file.getOriginalFilename();
            String filePath = pathHelper.getTempPath() + File.separator + oldName;
            File newFile = new File(filePath);
            file.transferTo(newFile);
            double sizes = FileHelper.sizeToMb(file.getSize());
            // 获取MD5
            String md5 = FileHelper.getFileMd5(filePath);
            AttachEntity entity = attachService.selectByGuid(md5);
            if (entity != null) {
@@ -123,9 +106,6 @@
        }
    }
    /**
     * 获取附件实体类
     */
    protected AttachEntity getAttachEntity(UserEntity ue, String tab, String oldName, String md5, double sizes) {
        AttachEntity entity = new AttachEntity();
        entity.setName(oldName);
@@ -141,16 +121,10 @@
        return entity;
    }
    /**
     * 下载文件
     */
    public void download(String guid, HttpServletResponse res) {
        download(guid, false, res);
    }
    /**
     * 下载文件
     */
    public void download(String guid, boolean inline, HttpServletResponse res) {
        try {
            if (StringHelper.isEmpty(guid)) {
@@ -177,9 +151,6 @@
        }
    }
    /**
     * 上传Excel附件
     */
    public List<KeyValueEntity> uploadXlsAnnex(UserEntity ue, List<MetaEntity> ms, List<MetaFileEntity> list, String path) {
        List<String> files = getAttachFiles(list, path);
        if (files.size() == 0) {
@@ -195,9 +166,6 @@
        return rs;
    }
    /**
     * 获取附件文件
     */
    private List<String> getAttachFiles(List<MetaFileEntity> list, String path) {
        List<String> files = new ArrayList<>();
@@ -216,9 +184,6 @@
        return files;
    }
    /**
     * 获取zip中的文件
     */
    private List<String> getZipFiles(MetaFileEntity mf, String path) {
        File file = new File(mf.getPath());
        if (!file.exists() || file.isDirectory()) {
@@ -239,9 +204,6 @@
        return files;
    }
    /**
     * 根据路径获取文件
     */
    private void getFilesByPath(List<String> list, String path) {
        File file = new File(path);
        if (!file.isDirectory()) {
@@ -263,9 +225,6 @@
        }
    }
    /**
     * 上传Excel附件
     */
    private int uploadXlsAnnex(UserEntity ue, MetaEntity meta, List<String> files) {
        List<?> list = queryXlsData(meta);
        if (null == list || list.size() == 0) {
@@ -295,9 +254,6 @@
        return rows;
    }
    /**
     * 获取名称
     */
    public static String[] getNames(String str) {
        if (StringHelper.isEmpty(str)) {
            return null;
@@ -306,9 +262,6 @@
        return str.replace(";", ",").replace(",", ",").split(",");
    }
    /**
     * 查询Excel数据
     */
    private List<?> queryXlsData(MetaEntity meta) {
        String entity = meta.getTab().substring(meta.getTab().indexOf(".") + 1).replace("_", "").toLowerCase();
        BasicMapper basicMapper = ClassHelper.getBasicMapper(entity);
@@ -330,9 +283,6 @@
        return list;
    }
    /**
     * 获取附件字段
     */
    public static Field getAnnexField(Object obj, String tab) {
        try {
            String str = ATTACH_TABS.get(tab);
@@ -346,9 +296,6 @@
        }
    }
    /**
     * 获取附件名称
     */
    public static String getAnnexName(Object obj, Field field) {
        try {
            Object val = field.get(obj);
@@ -359,9 +306,6 @@
        }
    }
    /**
     * 查找附件文件
     */
    private String findAnnexFile(String name, String folder, List<String> files) {
        name = File.separator + name.split("\\.")[0].trim().toLowerCase();
        for (String file : files) {
@@ -378,9 +322,6 @@
        return null;
    }
    /**
     * 插入Excel附件
     */
    private int insertXlsAnnex(UserEntity ue, MetaEntity meta, BaseEntity be, String file) {
        File f = new File(file);
        if (!f.exists() || f.isDirectory()) {
@@ -408,9 +349,6 @@
        return attachService.insert(ae);
    }
    /**
     * 获取附件实体类
     */
    protected AttachEntity getAttachEntity(UserEntity ue, String tabName, String eventid, String fileName, String md5, double sizes) {
        AttachEntity entity = new AttachEntity();
        entity.setTab(tabName);
@@ -427,27 +365,18 @@
        return entity;
    }
    /**
     * 请求下载
     */
    public String downloadReqForGuids(UserEntity ue, List<String> guids) {
        List<AttachEntity> list = attachService.selectByGuids(guids);
        return downloadReq(ue, list);
    }
    /**
     * 请求下载
     */
    public String downloadReqForTabGuids(UserEntity ue, String tab, List<String> guids) {
        List<AttachEntity> list = attachService.selectByTabGuids(tab, guids);
        return downloadReq(ue, list);
    }
    /**
     * 请求下载
     */
    protected String downloadReq(UserEntity ue, List<AttachEntity> list) {
        if (null == list || list.isEmpty()) {
            return null;
@@ -466,9 +395,6 @@
        return rows > 0 ? downloadEntity.getGuid() : null;
    }
    /**
     * 添加Zip文件
     */
    private void addZipFiles(ZipFile zip, ZipParameters params, List<AttachEntity> list) {
        int i = 1;
        String uploadPath = pathHelper.getConfig().getUploadPath();
@@ -492,9 +418,6 @@
        }
    }
    /**
     * 获取下载实体类
     */
    private DownloadEntity getDownloadEntity(UserEntity ue, String file, String pwd) {
        DownloadEntity de = new DownloadEntity();
        de.setName(FileHelper.getFileName(file));