summaryrefslogtreecommitdiff
path: root/src/main/java/com/mesasoft/cn/web/controller/FileMangerController.java
blob: ca7dbffa80b8be88035755938b6d4dfd99942649 (plain)
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
package com.mesasoft.cn.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.mesasoft.cn.annotation.AuthInterceptor;
import com.mesasoft.cn.enums.InterceptorLevel;
import com.mesasoft.cn.service.IFileManagerService;
import com.mesasoft.cn.util.ControllerUtils;
import com.zhazhapan.modules.constant.ValueConsts;
import com.zhazhapan.util.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * <a href="https://github.com/joni2back/angular-filemanager/blob/master/API.md">see api doc</a>
 *
 * @author pantao
 * @since 2018/1/29
 */
@ApiIgnore
@RestController
@RequestMapping("/filemanager")
@AuthInterceptor(InterceptorLevel.SYSTEM)
public class FileMangerController {

    private final IFileManagerService fileManagerService;

    private final JSONObject jsonObject;

    @Autowired
    public FileMangerController(IFileManagerService fileManagerService, JSONObject jsonObject) {
        this.fileManagerService = fileManagerService;
        this.jsonObject = jsonObject;
    }

    @AuthInterceptor(InterceptorLevel.SYSTEM)
    @RequestMapping(value = "/multidownload", method = RequestMethod.GET)
    public void multiDownload(HttpServletResponse response, String[] items, String toFilename) throws IOException {
        ControllerUtils.setResponseFileName(response, toFilename);
        fileManagerService.multiDownload(response, items, toFilename);
    }

    @AuthInterceptor(InterceptorLevel.SYSTEM)
    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public void download(HttpServletResponse response, String path) throws IOException {
        ControllerUtils.loadResource(response, path, ValueConsts.TRUE);
    }
    public static String getEncoding(String str) {
        String encode = "GB2312";
        String finecode = "";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                finecode = encode;
            }
        } catch (Exception exception) {
        }
        encode = "ISO-8859-1";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                finecode = encode;
            }
        } catch (Exception exception1) {
        }
        encode = "GBK";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                finecode = encode;
            }
        } catch (Exception exception1) {
        }
        encode = "UTF-8";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                finecode = encode;
            }
        } catch (Exception exception2) {
        }
        encode = "GBK";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                finecode = encode;
            }
        } catch (Exception exception3) {
        }
        return finecode;
    }
    /**
     * 暂时没有找到更好的解决方案
     *
     * @param destination 目的
     *
     * @return 响应结果
     */
    @AuthInterceptor(InterceptorLevel.SYSTEM)
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public String upload(String destination, MultipartHttpServletRequest request) {
        Map<String, MultipartFile> fileMap = request.getFileMap();
        MultipartFile[] files = ArrayUtils.mapToArray(fileMap, MultipartFile.class);
        jsonObject.put("result", fileManagerService.upload(destination, files));
        return jsonObject.toJSONString();
    }

    @AuthInterceptor(InterceptorLevel.SYSTEM)
    @RequestMapping(value = "/extract", method = RequestMethod.POST)
    public String extract(@RequestBody JSONObject json) {
        jsonObject.put("result", fileManagerService.extract(json));
        return jsonObject.toJSONString();
    }

    @AuthInterceptor(InterceptorLevel.SYSTEM)
    @RequestMapping(value = "/compress", method = RequestMethod.POST)
    public String compress(@RequestBody JSONObject json) {
        jsonObject.put("result", fileManagerService.compress(json));
        return jsonObject.toJSONString();
    }

    @AuthInterceptor(InterceptorLevel.SYSTEM)
    @RequestMapping(value = "/permission", method = RequestMethod.POST)
    public String setPermission(@RequestBody JSONObject json) {
        jsonObject.put("result", fileManagerService.setPermission(json));
        return jsonObject.toJSONString();
    }

    @AuthInterceptor(InterceptorLevel.SYSTEM)
    @RequestMapping(value = "/folder", method = RequestMethod.POST)
    public String createFolder(@RequestBody JSONObject json) {
        jsonObject.put("result", fileManagerService.createFolder(json));
        return jsonObject.toJSONString();
    }

    @AuthInterceptor(InterceptorLevel.SYSTEM)
    @RequestMapping(value = "/content", method = RequestMethod.POST)
    public String getContent(@RequestBody JSONObject json) {
        jsonObject.put("result", fileManagerService.getContent(json));
        return jsonObject.toJSONString();
    }

    @AuthInterceptor(InterceptorLevel.SYSTEM)
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public String edit(@RequestBody JSONObject json) {
        jsonObject.put("result", fileManagerService.edit(json));
        return jsonObject.toJSONString();
    }

    @AuthInterceptor(InterceptorLevel.SYSTEM)
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public String remove(@RequestBody JSONObject json) {
        jsonObject.put("result", fileManagerService.remove(json));
        return jsonObject.toJSONString();
    }

    @AuthInterceptor(InterceptorLevel.SYSTEM)
    @RequestMapping(value = "/copy", method = RequestMethod.POST)
    public String copy(@RequestBody JSONObject json) {
        jsonObject.put("result", fileManagerService.copy(json));
        return jsonObject.toJSONString();
    }

    @AuthInterceptor(InterceptorLevel.SYSTEM)
    @RequestMapping(value = "/move", method = RequestMethod.POST)
    public String move(@RequestBody JSONObject json) {
        jsonObject.put("result", fileManagerService.move(json));
        return jsonObject.toJSONString();
    }

    @AuthInterceptor(InterceptorLevel.SYSTEM)
    @RequestMapping(value = "/rename", method = RequestMethod.POST)
    public String rename(@RequestBody JSONObject json) {
        jsonObject.put("result", fileManagerService.rename(json));
        return jsonObject.toJSONString();
    }

    @AuthInterceptor(InterceptorLevel.SYSTEM)
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public String list(@RequestBody JSONObject json) {
        jsonObject.put("result", fileManagerService.list(json));
        return jsonObject.toJSONString();
    }
}