DecryptController.java 3.6 KB
Newer Older
jianglw's avatar
jianglw committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
package com.ruoyi.web.controller.handle;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.HandleFileType;
import com.ruoyi.common.handlefile.HandleFile;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.file.FileImportUtils;
import com.ruoyi.common.utils.file.FileToZip;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.framework.mail.service.IMailService;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Objects;

/**
 * @author jianglw
 * @version 1.0
 * @date 2021/2/1 15:31
 */
@RestController
@RequestMapping("/handle/decrypt")
public class DecryptController {
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IMailService iMailService;
    @Value("${spring.mail.username}")
    private String from;
    @Value( ("${ruoyi.profile}") )
    private String profile;

    @Log(title = "配置文件解密", businessType = BusinessType.IMPORT)
    @PostMapping("/analysis")
    @PreAuthorize("@ss.hasPermi('handle:encryption:analysis')")
    public void upload(@RequestParam(value = "files") MultipartFile[] files, HttpServletResponse response){
        LoginUser loginUser = tokenService.getLoginUser( ServletUtils.getRequest());
        for(MultipartFile file:files){
            if (!file.isEmpty()) {
                //对文文件的全名进行截取然后在后缀名进行删选。
                int begin = Objects.requireNonNull( file.getOriginalFilename() ).indexOf(".");
                int last = file.getOriginalFilename().length();
                //获取后缀名称
                String suffix = file.getOriginalFilename().substring(begin+1, last);
                //获取处理文件方式
                HandleFile handleFile= HandleFileType.match( suffix );
                try {
                    assert handleFile != null;
                    handleFile.readFile( file ).decryptFile().createFile( file.getOriginalFilename(),loginUser.getUsername() ,profile);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        if(FileToZip.fileToZip(profile+loginUser.getUsername(), profile+loginUser.getUsername(),
                "配置文件")){
            //发送邮件
            //FileSystemResource fileSystemResource=new FileSystemResource( new File(profile+loginUser.getUsername()+"/"+"配置文件.zip") );
            //iMailService.sendAttachmentsMail(from, loginUser.getUser().getEmail(),"配置文件","配置文件",fileSystemResource );
            //下载文件
            try {
                FileImportUtils.exportDownload(response,new File(profile+loginUser.getUsername()+"/"+"配置文件.zip"),"配置文件.zip" );
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //删除该用户文件夹下所有临时文件
        FileUtils.deleteFiles(new File( profile+loginUser.getUsername() ));
    }
}