fix
Helius
2021-09-15 7ab7943de217a9e5d4a489c22c7ae27a29692d55
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.xzx.gc.system.controller;
 
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.xzx.gc.common.Result;
import com.xzx.gc.common.constant.Constants;
import com.xzx.gc.common.dto.CommonDto;
import com.xzx.gc.common.request.BaseController;
import com.xzx.gc.common.utils.BusinessUtil;
import com.xzx.gc.common.utils.FileUtils;
import com.xzx.gc.entity.ResourceInfo;
import com.xzx.gc.system.mapper.ResourceInfoMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
@RestController
@Api(tags = {"上传下载管理"})
@Slf4j
public class UpOrDownloadController extends BaseController {
 
    @Autowired
    private BusinessUtil businessUtil;
 
    @Autowired
    private ResourceInfoMapper resourceInfoMapper;
 
 
    @ApiOperation("文件上传")
    @PostMapping(value = {"/file/upload", "/admin/front/file/upload"})
    @ResponseBody
    public Result<List<ResourceInfo>> upload(HttpServletRequest request, @ApiParam(value = "文件类型 ,1:默认图片") @RequestParam(name = "type", defaultValue = "1") int type, @RequestParam(name = "file") MultipartFile[] files) throws IOException {
        Result result = new Result();
        if (files == null || files.length == 0) {
            result.setCode(-1);
            result.setMsg("没有需要上传的文件");
            return result;
        }
 
        if (files.length > 6) {
            result.setCode(-1);
            result.setMsg("最多只能传6张图片");
            return result;
        }
 
 
        long sumSize = 0;
        for (MultipartFile file : files) {
            //是否格式正确
            boolean isGreate=true;
            if(file.getContentType().equals(MediaType.APPLICATION_OCTET_STREAM_VALUE)){
                String suffix = FileUtils.getSuffixByFile(file);
                if(!suffix.equals("jpg")&&!suffix.equals("png")){
                    isGreate=false;
                }
            }else {
                if (!"image/png".equals(file.getContentType()) && !"image/jpeg".equals(file.getContentType()) && !"image/jpg".equals(file.getContentType())) {
                   isGreate=false;
                }
            }
            if(!isGreate) {
                result.setCode(-1);
                result.setMsg("只支持JPG/PNG格式的图片");
                log.debug("当前文件格式为:{}", file.getContentType());
                return result;
            }
 
            sumSize += file.getSize();
        }
 
        //限制24m图片
        long maxSize = 24 * 1024 * 1024;
 
        if (maxSize < sumSize) {
            result.setCode(-1);
            result.setMsg("图片总共不能超过24M");
            return result;
        }
 
 
        List<ResourceInfo> results = new ArrayList<>();
        for (MultipartFile file : files) {
            String suffix = StrUtil.subAfter(file.getOriginalFilename(), ".", true);
            String realName = IdUtil.fastSimpleUUID() + "." + suffix;
            FileUtil.writeFromStream(file.getInputStream(), Constants.IMG_UPLOAD_PATH + "/" + realName);
            ResourceInfo resourceInfo = new ResourceInfo();
            if(file.getContentType().equals(MediaType.APPLICATION_OCTET_STREAM_VALUE)){
                suffix = FileUtils.getSuffixByFile(file);
                if(suffix.equals("jpg")){
                    resourceInfo.setResourceSubType("image/jpg");
                }else if(suffix.equals("png")){
                    resourceInfo.setResourceSubType("image/png");
                }
            }else {
                resourceInfo.setResourceSubType(file.getContentType());
            }
            resourceInfo.setResourceOldName(file.getOriginalFilename());
            resourceInfo.setResourceSize(file.getSize());
            resourceInfo.setResourceUrl(businessUtil.getViewUrl() + "/" + realName);
            resourceInfo.setResourceType(Convert.toShort(type));
            resourceInfo.setCreateTime(DateUtil.now());
            resourceInfo.setCreateUserId(getUserId(request));
            resourceInfo.setRealName(realName);
            results.add(resourceInfo);
        }
        resourceInfoMapper.insertList(results);
        result.setData(results);
        return result;
    }
 
 
    @ApiOperation(value = "文件删除", notes = "id:返回的文件资源ID,多个用逗号分隔")
    @PostMapping(value = "/file/delete")
    public Result uploadDelete(@RequestBody CommonDto commonDto) {
        String[] split = commonDto.getId().split(",");
        for (String id : split) {
            ResourceInfo resourceInfo = resourceInfoMapper.selectByPrimaryKey(id);
            resourceInfoMapper.deleteByPrimaryKey(id);
            if (resourceInfo != null) {
                String path = Constants.IMG_UPLOAD_PATH + "/" + resourceInfo.getRealName();
                FileUtil.del(path);
            }
        }
        return Result.success("");
    }
 
 
}