tj
2025-06-05 2d549a04870d1315868a7cf19952b64e8071e711
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package com.cloudroam.controller.sys;
 
import cn.hutool.core.util.StrUtil;
import com.cloudroam.dto.file.FileInfo;
import com.cloudroam.manager.SysFileManager;
import com.cloudroam.model.SysFileDO;
import com.cloudroam.util.MinioUtil;
import io.github.talelin.autoconfigure.exception.ForbiddenException;
import io.github.talelin.core.annotation.LoginRequired;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Objects;
 
/**
 * 文件操作接口
 */
@RestController
@RequestMapping(value = "/sys/file")
public class FilesController {
 
    @Autowired
    MinioUtil minioUtil;
 
    @Autowired
    private SysFileManager sysFileManager;
 
 
    /**
     * 上传单文件
     * @param uploadfile
     * @param bucket
     * @param objectName
     * @throws Exception
     */
//    @LoginRequired
    @RequestMapping(value = "/uploadfile", method = RequestMethod.POST)
    public SysFileDO fileupload(@RequestParam(value = "file",required = true) MultipartFile uploadfile, @RequestParam(required = true) String bucket,
                                    @RequestParam(required = true) String objectName) throws Exception {
 
        if(Objects.isNull(uploadfile)){
            throw new ForbiddenException(10400);
        }
        if(StrUtil.isBlank(bucket)){
            throw new ForbiddenException(10400);
        }
        if(StrUtil.isBlank(objectName)){
            throw new ForbiddenException(10401);
        }
 
        SysFileDO sysFileDO = sysFileManager.uploadFile(uploadfile, bucket, objectName);
        return sysFileDO;
    }
 
 
    /**
     * 列出所有桶
     * @return
     * @throws Exception
     */
    @LoginRequired
    @RequestMapping(value = "/listBuckets", method = RequestMethod.GET)
    public List<String> listBuckets() throws Exception {
        List<String> fileList= minioUtil.listBuckets();
 
        return fileList;
//        return ResponseUtils.success(minioUtil.listBuckets());
    }
 
    /**
     * 递归列出一个桶中的所有文件和目录
     * @param bucket
     * @return
     * @throws Exception
     */
    @LoginRequired
    @RequestMapping(value = "/listFiles", method = RequestMethod.GET)
    public List<FileInfo> listFiles(@RequestParam String bucket) throws Exception {
        List<FileInfo> fileInfos = minioUtil.listFiles(bucket);
 
        return fileInfos;
//        return ResponseUtils.success(fileInfos);
    }
 
    /**
     * 下载一个文件
     * @param bucket
     * @param objectName
     * @param response
     * @throws Exception
     */
//    @LoginRequired
    @RequestMapping(value = "/downloadFileWithBucket", method = RequestMethod.GET)
    public void downloadFileWithBucket(@RequestParam String bucket, @RequestParam String objectName,
                             HttpServletResponse response) throws Exception {
        InputStream stream = minioUtil.download(bucket, objectName);
        ServletOutputStream output = response.getOutputStream();
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(objectName.substring(objectName.lastIndexOf("/") + 1), "UTF-8"));
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("UTF-8");
        IOUtils.copy(stream, output);
    }
 
    @RequestMapping(value = "/downloadFile", method = RequestMethod.GET)
    public void downloadFile2(@RequestParam String fullpath,
                             HttpServletResponse response) throws Exception {
        if(Objects.isNull(fullpath)){
            throw new ForbiddenException(10403);
        }
        SysFileDO existSysFileDO = sysFileManager.getFileByFullpath(fullpath);
        if(Objects.isNull(existSysFileDO)){
            throw new ForbiddenException(10404);
        };
 
        sysFileManager.downloadFileWithFullpath(fullpath,response);
 
    }
 
 
    /**
     * 删除一个文件
     * @param bucket
     * @param objectName
     * @throws Exception
     */
    @LoginRequired
    @RequestMapping(value = "/deleteFile", method = RequestMethod.GET)
    public void deleteFile(@RequestParam String bucket, @RequestParam String objectName) throws Exception {
        minioUtil.deleteObject(bucket, objectName);
//        return ResponseUtils.success();
    }
 
 
    /**
     * 删除一个桶
     * @param bucket
     * @throws Exception
     */
    @LoginRequired
    @RequestMapping(value = "/deleteBucket", method = RequestMethod.GET)
    public void deleteBucket(@RequestParam String bucket) throws Exception {
        minioUtil.deleteBucket(bucket);
//        return ResponseUtils.success();
    }
 
 
    /**
     * 复制一个文件
     * @param sourceBucket
     * @param sourceObject
     * @param targetBucket
     * @param targetObject
     * @throws Exception
     */
    @LoginRequired
    @GetMapping("/copyObject")
    public void copyObject(@RequestParam String sourceBucket, @RequestParam String sourceObject, @RequestParam String targetBucket, @RequestParam String targetObject) throws Exception {
        minioUtil.copyObject(sourceBucket, sourceObject, targetBucket, targetObject);
//        return ResponseUtils.success();
    }
 
    /**
     * 获取文件信息
     * @param bucket
     * @param objectName
     * @return
     * @throws Exception
     */
    @LoginRequired
    @GetMapping("/getObjectInfo")
    public String getObjectInfo(@RequestParam String bucket, @RequestParam String objectName) throws Exception {
        String objectInfo = minioUtil.getObjectInfo(bucket, objectName);
        return objectInfo;
//        return ResponseUtils.success(minioUtil.getObjectInfo(bucket, objectName));
    }
 
    /**
     * 获取一个连接以供下载
     * @param bucket
     * @param objectName
     * @param expires
     * @return
     * @throws Exception
     */
    @LoginRequired
    @GetMapping("/getPresignedObjectUrl")
    public String getPresignedObjectUrl(@RequestParam String bucket, @RequestParam String objectName, @RequestParam Integer expires) throws Exception {
        String presignedObjectUrl = minioUtil.getPresignedObjectUrl(bucket, objectName, expires);
//        return ResponseUtils.success(minioUtil.getPresignedObjectUrl(bucket, objectName, expires));
        return presignedObjectUrl;
    }
 
    /**
     * 获取minio中所有的文件
     * @return
     * @throws Exception
     */
    @LoginRequired
    @GetMapping("/listAllFile")
    public List<FileInfo> listAllFile() throws Exception {
        List<FileInfo> fileInfos = minioUtil.listAllFile();
        return fileInfos;
 
//        return ResponseUtils.success(fileInfos);
    }
 
 
}