张晓波
2023-09-19 164694c47c35d6654df69b533e8dbf8b5423efc5
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
package com.thhy.filectrl.controller;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.utils.StringUtils;
import com.thhy.filectrl.service.FdfsService;
import com.thhy.filectrl.service.impl.FdfsServiceImpl;
import com.thhy.general.common.BasicResult;
import com.thhy.general.common.BasicStatus;
import com.thhy.general.entity.DownFileDto;
import com.thhy.general.exception.BasicException;
import org.csource.common.NameValuePair;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
@RestController
@RequestMapping("file")
public class FileController {
 
    @Autowired
    private FdfsService fdfsService;
 
    @Autowired
    private RedissonClient redissonClient;
 
    @Value("${file.prekey}")
    private String prekey;
 
    @RequestMapping("upload")
    public BasicResult upload(HttpServletRequest req){
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(req.getSession().getServletContext());
        String newFileName = "";
        if (commonsMultipartResolver.isMultipart(req)) {
            MultipartHttpServletRequest request = (MultipartHttpServletRequest)req;
            MultipartFile multipartFile = request.getFile("file");
            try {
                String result = fdfsService.upload(multipartFile);
                return BasicResult.success(result);
            } catch (Exception e) {
                throw new BasicException(BasicStatus.FILE_UPLOAD_ERROR);
            }
        }else{
            throw new BasicException(BasicStatus.REQUEST_NOT_FILE);
        }
    }
 
    @RequestMapping("down")
    public void down(String authcode,HttpServletResponse resp){
        RBucket<String> rBucket = redissonClient.getBucket(prekey+":authcode:"+authcode);
        String param = rBucket.getAndDelete();
        if(StringUtils.isEmpty(param)){
            throw new BasicException(BasicStatus.DOWNLOAD_IS_NOT_SUPPORT);
        }
        DownFileDto downFileDto = JSON.toJavaObject(JSON.parseObject(param),DownFileDto.class);
        if(StringUtils.isBlank(downFileDto.getFullpath())){
            throw new BasicException(BasicStatus.DOWNLOAD_IS_NOT_SUPPORT);
        }
        fdfsService.downfile(downFileDto.getFullpath(),downFileDto.getFileName(),resp);
    }
 
    @RequestMapping("fileInfo")
    public BasicResult fileInfo(@RequestBody String param){
        JSONObject jsonObject = JSON.parseObject(param);
        NameValuePair[] nameValuePairs = fdfsService.fileInfo(jsonObject.getString("fullpath"));
        return BasicResult.success(nameValuePairs);
    }
 
    @RequestMapping("preview")
    public void preview(String authcode,HttpServletResponse resp){
        RBucket<String> rBucket = redissonClient.getBucket(prekey+":authcode:"+authcode);
        String param = rBucket.getAndDelete();
        if(StringUtils.isEmpty(param)){
            throw new BasicException(BasicStatus.DOWNLOAD_IS_NOT_SUPPORT);
        }
        DownFileDto downFileDto = JSON.toJavaObject(JSON.parseObject(param),DownFileDto.class);
        if(StringUtils.isBlank(downFileDto.getFullpath())){
            throw new BasicException(BasicStatus.DOWNLOAD_IS_NOT_SUPPORT);
        }
        fdfsService.preview(downFileDto.getFullpath(),resp);
    }
}