FileController.java 4.88 KB
package vion.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import vion.Global;
import vion.dto.FileInfoDTO;
import vion.model.FileInfo;
import vion.service.IFileService;
import vion.utils.ResultUtil;
import vion.vo.ResultVO;

import java.io.File;
import java.io.IOException;
import java.util.Date;

@RestController
@RequestMapping(Global.BASE_URL)
@RequiredArgsConstructor
@Slf4j
public class FileController {

    private final IFileService fileService;
    private final Converter converter;

    @Value("${fileUrl:}")
    private String fileurl;

    @GetMapping("/files")
    public Page<FileInfo> getFiles(FileInfoDTO data) {
        return fileService.lambdaQuery(converter.convert(data, new FileInfo()))
                .page(Page.of(data.getPageNum(), data.getPageSize()));
    }

    @DeleteMapping("/file/{id}")
    public String remove(@PathVariable(name = "id") Integer id) {
        FileInfo file = fileService.getById(id);
        if (FileUtil.del(file.getUrl())) {
            fileService.removeById(id);
            return "文件删除成功";
        } else {
            return "文件删除失败";
        }
    }


    /*
     * todo
     * 手机端工单预处理提交
     * 项目阶段修改,待联调  sourceType=1 这里的附件查询
     * 工单处理上传附件
     */
    @PostMapping("/upLoadFile")
    public ResultVO uploadFile(Long storeId, Long sourceId, FileInfo fileInfo, @RequestParam(name = "file") MultipartFile infile) {
        try {
            //上传url地址
            String filename = infile.getOriginalFilename() + "_" + DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN);
            // String path = fileurl + FileUtil.FILE_SEPARATOR + storeId + FileUtil.FILE_SEPARATOR + sourceId + FileUtil.FILE_SEPARATOR + filename;
            String path = fileurl + FileUtil.FILE_SEPARATOR + filename;
            File file = FileUtil.touch(path);
            infile.transferTo(file);
            String sha256 = SecureUtil.sha256(file).toUpperCase();
            String extName = FileUtil.extName(file);
            return ResultUtil.success(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResultUtil.success();

        /*String filename = infile.getOriginalFilename() + "_" + DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN);
        String path = fileurl + FileUtil.FILE_SEPARATOR + fileInfo.getStoreId() + FileUtil.FILE_SEPARATOR + fileInfo.getSourceId() + FileUtil.FILE_SEPARATOR + filename;
        // String path = fileurl + FileUtil.FILE_SEPARATOR + filename;
        File file = FileUtil.touch(path);
        try {
            infile.transferTo(file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        fileInfo.setName(filename);
        fileInfo.setUrl(path);
        fileInfo.setType(FileUtil.extName(file));
        fileInfo.setSha256(SecureUtil.sha256(file).toUpperCase());
        // 如果是预工单的话,可能是客户上传的文件
        // fileInfo.setUploader();
        fileService.save(fileInfo);
        return ResultUtil.success();*/
    }

    @PostMapping("/uploadFile1")
    public String uploadFile1(FileInfo fileInfo, @RequestParam(name = "files") MultipartFile[] files) throws IOException {
        for (MultipartFile infile : files) {
            String orgName = infile.getOriginalFilename();
            String fileName = orgName.substring(0, orgName.lastIndexOf("."));
            String fileExt = orgName.substring(orgName.lastIndexOf("."));
            String filename = fileName + "_" + DateUtil.format(new Date(), "yyyyMMdd_HHmmss") + fileExt;
            String path = fileurl + FileUtil.FILE_SEPARATOR + fileInfo.getStoreId() + FileUtil.FILE_SEPARATOR + fileInfo.getSourceId() + FileUtil.FILE_SEPARATOR + filename;
            File file = FileUtil.touch(path);
            infile.transferTo(file);

            FileInfo tempFileInfo = new FileInfo();
            tempFileInfo.setStoreId(fileInfo.getStoreId());
            tempFileInfo.setSourceId(fileInfo.getSourceId());
            tempFileInfo.setSourceType(fileInfo.getSourceType());
            tempFileInfo.setName(filename);
            tempFileInfo.setUrl(path);
            tempFileInfo.setType(FileUtil.extName(file));
            tempFileInfo.setSha256(SecureUtil.sha256(file).toUpperCase());
            tempFileInfo.setUploader(fileInfo.getUploader());

            fileService.save(tempFileInfo);
        }
        return "文件保存成功";
    }
}