ludc
2023-03-16 86b6157299a50579f454e4fb45a09ff21d252dab
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.core.boot.ctrl;
 
import org.springblade.core.boot.file.LocalFile;
import org.springblade.core.boot.file.BladeFileUtil;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Charsets;
import org.springblade.core.tool.utils.StringPool;
import org.springblade.core.tool.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourceRegion;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.UriUtils;
 
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
 
/**
 * Blade控制器封装类
 *
 * @author Chill
 */
public class BladeController {
 
    /**
     * ============================     REQUEST    =================================================
     */
 
    @Autowired
    private HttpServletRequest request;
 
    /**
     * 获取request
     */
    public HttpServletRequest getRequest() {
        return this.request;
    }
 
    /**
     * 获取当前用户
     *
     * @return
     */
    public BladeUser getUser() {
        return AuthUtil.getUser();
    }
 
    /** ============================     API_RESULT    =================================================  */
 
    /**
     * 返回ApiResult
     *
     * @param data
     * @return R
     */
    public <T> R<T> data(T data) {
        return R.data(data);
    }
 
    /**
     * 返回ApiResult
     *
     * @param data
     * @param message
     * @return R
     */
    public <T> R<T> data(T data, String message) {
        return R.data(data, message);
    }
 
    /**
     * 返回ApiResult
     *
     * @param data
     * @param message
     * @param code
     * @return R
     */
    public <T> R<T> data(T data, String message, int code) {
        return R.data(code, data, message);
    }
 
    /**
     * 返回ApiResult
     *
     * @param message
     * @return R
     */
    public R success(String message) {
        return R.success(message);
    }
 
    /**
     * 返回ApiResult
     *
     * @param message
     * @return R
     */
    public R fail(String message) {
        return R.fail(message);
    }
 
    /**
     * 返回ApiResult
     *
     * @param flag
     * @return R
     */
    public R status(boolean flag) {
        return R.status(flag);
    }
 
 
    /**============================     FILE    =================================================  */
 
    /**
     * 获取BladeFile封装类
     *
     * @param file
     * @return
     */
    public LocalFile getFile(MultipartFile file) {
        return BladeFileUtil.getFile(file);
    }
 
    /**
     * 获取BladeFile封装类
     *
     * @param file
     * @param dir
     * @return
     */
    public LocalFile getFile(MultipartFile file, String dir) {
        return BladeFileUtil.getFile(file, dir);
    }
 
    /**
     * 获取BladeFile封装类
     *
     * @param file
     * @param dir
     * @param path
     * @param virtualPath
     * @return
     */
    public LocalFile getFile(MultipartFile file, String dir, String path, String virtualPath) {
        return BladeFileUtil.getFile(file, dir, path, virtualPath);
    }
 
    /**
     * 获取BladeFile封装类
     *
     * @param files
     * @return
     */
    public List<LocalFile> getFiles(List<MultipartFile> files) {
        return BladeFileUtil.getFiles(files);
    }
 
    /**
     * 获取BladeFile封装类
     *
     * @param files
     * @param dir
     * @return
     */
    public List<LocalFile> getFiles(List<MultipartFile> files, String dir) {
        return BladeFileUtil.getFiles(files, dir);
    }
 
    /**
     * 获取BladeFile封装类
     *
     * @param files
     * @param path
     * @param virtualPath
     * @return
     */
    public List<LocalFile> getFiles(List<MultipartFile> files, String dir, String path, String virtualPath) {
        return BladeFileUtil.getFiles(files, dir, path, virtualPath);
    }
    /**
     * 下载文件
     *
     * @param file 文件
     * @return {ResponseEntity}
     * @throws IOException io异常
     */
    protected ResponseEntity<ResourceRegion> download(File file) throws IOException {
        String fileName = file.getName();
        return download(file, fileName);
    }
 
    /**
     * 下载
     *
     * @param file     文件
     * @param fileName 生成的文件名
     * @return {ResponseEntity}
     * @throws IOException io异常
     */
    protected ResponseEntity<ResourceRegion> download(File file, String fileName) throws IOException {
        Resource resource = new FileSystemResource(file);
        return download(resource, fileName);
    }
 
    /**
     * 下载
     *
     * @param resource 资源
     * @param fileName 生成的文件名
     * @return {ResponseEntity}
     * @throws IOException io异常
     */
    protected ResponseEntity<ResourceRegion> download(Resource resource, String fileName) throws IOException {
        HttpServletRequest request = WebUtil.getRequest();
        String header = request.getHeader(HttpHeaders.USER_AGENT);
        // 避免空指针
        header = header == null ? StringPool.EMPTY : header.toUpperCase();
        HttpStatus status;
        String msie= "MSIE";
        String trident= "TRIDENT";
        String edge= "EDGE";
        if (header.contains(msie) || header.contains(trident) || header.contains(edge)) {
            status = HttpStatus.OK;
        } else {
            status = HttpStatus.CREATED;
        }
        // 断点续传
        long position = 0;
        long count = resource.contentLength();
        String range = request.getHeader(HttpHeaders.RANGE);
        if (null != range) {
            status = HttpStatus.PARTIAL_CONTENT;
            String[] rangeRange = range.replace("bytes=", StringPool.EMPTY).split(StringPool.DASH);
            position = Long.parseLong(rangeRange[0]);
            if (rangeRange.length > 1) {
                long end = Long.parseLong(rangeRange[1]);
                count = end - position + 1;
            }
        }
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        String encodeFileName = UriUtils.encode(fileName, Charsets.UTF_8);
        // 兼容各种浏览器下载:
        // https://blog.robotshell.org/2012/deal-with-http-header-encoding-for-file-download/
        String disposition = "attachment;" +
            "filename=\"" + encodeFileName + "\";" +
            "filename*=utf-8''" + encodeFileName;
        headers.set(HttpHeaders.CONTENT_DISPOSITION, disposition);
        return new ResponseEntity<>(new ResourceRegion(resource, position, count), headers, status);
    }
 
}