ke
2024-12-05 87e524604a801de51cf277535e26988ce3d2ce53
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package cn.gistack.by.nettyUpload;
 
 
import cn.gistack.by.nettyUpload.config.NettyHttpConfig;
import cn.gistack.by.nettyUpload.utils.BeenUtils;
import cn.gistack.by.nettyUpload.utils.HttpResponseUtils;
import cn.gistack.by.nettyUpload.utils.UriUtils;
import cn.gistack.by.nettyUpload.utils.ExceptionUtils;
import cn.gistack.by.picUpload.entity.BaiyiPicEntity;
import cn.gistack.by.picUpload.entity.XfByPic;
import cn.gistack.by.picUpload.entity.XfBySnRelation;
import cn.gistack.by.picUpload.service.BaiyiPicService;
import cn.gistack.resource.feign.IOssClient;
import cn.hutool.core.date.DateUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import io.netty.handler.codec.http.multipart.*;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
 
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
 
 
 
import static cn.gistack.by.nettyUpload.utils.ExceptionUtils.getStackString;
 
/**
 * 文件上传处理类
 * @author chenhaijian
 * @date 2020-05-21 14:45
 */
@Slf4j
public class  HttpUploadFileHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
 
    // http请求中可选参数
    private static final String FILENAME_PARAM = "filename";
    // http请求中必选的参数
    private static final String FILE_EXTNAME_PARAM = "fileextname";
 
 
    private static final String UPLOAD_DATA_URL = "/termite/syncDeviceState";
 
    /***
     * 新设备传图片接口
     */
    private static final String UPLOAD_FILE_URL = "/termite/termiteDataup";
    /**
     * 旧设备传图片接口
     * 没有Authorization验证
     */
    private static final String UPLOAD_FILE_URL_OLD = "/termite/termiteDataupOld";
    /**
     * * 旧设备sn码和物联网关系表
     * * 没有Authorization验证
     */
    private static final String UPLOAD_FILE_OLD_RELATION = "/termite/termiteRelation";
 
    private NettyHttpConfig nettyHttpConfig;
 
    private BaiyiPicService baiyiPicService;
 
    /**
     * 对象存储构建类
     */
 
    private   IOssClient iOssClient;
 
    static {
        // should delete file on exit (in normal exit)
        DiskFileUpload.deleteOnExitTemporaryFile = true;
        DiskFileUpload.baseDirectory = null; // system temp directory
        DiskAttribute.deleteOnExitTemporaryFile = true; // should delete file on
        // exit (in normal exit)
        DiskAttribute.baseDirectory = null; // system temp directory
 
 
    }
 
    public HttpUploadFileHandler(NettyHttpConfig nettyHttpConfig, IOssClient iOssClient,BaiyiPicService baiyiPicService) {
        this.nettyHttpConfig = nettyHttpConfig;
        this.iOssClient = iOssClient;
        this.baiyiPicService = baiyiPicService;
 
    }
 
 
    protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest fullRequest) throws Exception {
 
        // 请求路径验证
        String urlyz  = UriUtils.getURI(fullRequest).getPath();
        log.info("请求路径urlyz::"+urlyz);
 
        if (UPLOAD_FILE_URL.equals(urlyz)) { //传图片
            // Authorization验证
            boolean isRequestValid = isValidHttpRequest(ctx, fullRequest);
            if (!isRequestValid) {
                HttpResponseUtils.sendResponseJson2(ctx, HttpResponseStatus.BAD_REQUEST, "验证失败",null);
                return ;
            }
 
            XfByPic xfByPic = setEntity(fullRequest);
 
            // 保存入库xfByPic
            baiyiPicService.inserXfPicUrl(xfByPic);
 
 
 
        }else if(UPLOAD_FILE_URL_OLD.equals(urlyz)) { // 旧类型设备上传图片数据接口
            log.info("UPLOAD_FILE_URL_OLD接口调用成功!!!!:");
            System.err.println(fullRequest);
 
            XfByPic xfByPic = setEntity(fullRequest);
            // 保存入库xfByPic
            baiyiPicService.inserXfOldPicUrl(xfByPic);
 
 
        }else if(UPLOAD_FILE_OLD_RELATION.equals(urlyz)) { //传sn和rtu编码关系接口
            HttpPostMultipartRequestDecoder test = new HttpPostMultipartRequestDecoder(fullRequest);
            List<InterfaceHttpData> bodyHttpDatas = test.getBodyHttpDatas();
            String  sn = "";
            for (InterfaceHttpData item : bodyHttpDatas ) {
                if (  item instanceof Attribute){
                    Attribute attribute = (Attribute) item;
                    String value = attribute.getValue();
                    switch (attribute.getName()) {
                        case "sn":
                            sn = value;
                            break;
                    }
 
                }
            }
            // 查询sn对应的
            XfBySnRelation xfBySnRelation = baiyiPicService.selectMnnoBySn(sn);
            HashMap<String, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("mn_no", xfBySnRelation.getWlwRtu());
            objectObjectHashMap.put("order", xfBySnRelation.getWlwOrder());
 
 
            HttpResponseUtils.sendResponseJson2(ctx, HttpResponseStatus.OK, "查询成功",objectObjectHashMap);
            return ;
        } else {
            HttpResponseUtils.sendResponseJson2(ctx, HttpResponseStatus.BAD_REQUEST, "验证失败",null);
            return ;
        }
 
        HttpResponseUtils.sendResponseJson(ctx, HttpResponseStatus.OK, "上传成功");
    }
 
    //请求参数注入实体
    public XfByPic setEntity(FullHttpRequest fullRequest) throws Exception {
        HttpPostMultipartRequestDecoder test = new HttpPostMultipartRequestDecoder(fullRequest);
        List<InterfaceHttpData> bodyHttpDatas = test.getBodyHttpDatas();
        XfByPic xfByPic = new XfByPic();
        for (InterfaceHttpData item : bodyHttpDatas ) {
            if (item instanceof Attribute) {
                Attribute attribute = (Attribute) item;
                String value = attribute.getValue();
                switch (attribute.getName()) {
                    case "mnNo":
                        xfByPic.setMnNo(value);
                        break;
                    case "order":
 
                        xfByPic.setOrder(value);
                        break;
                    case "sn":
                        xfByPic.setSn(value);
                        break;
                    case "ap":
                        xfByPic.setAp(value);
                        break;
                    case "camera":
                        xfByPic.setCamera(value);
                        break;
                    case "hardware":
                        xfByPic.setHardware(value);
                        break;
                    case "rssi":
                        xfByPic.setRssi(value);
                        break;
                    case "software":
                        xfByPic.setSoftware(value);
                        break;
                    case "temperature":
                        xfByPic.setTemperature(value);
                        break;
                    case "voltage":
                        xfByPic.setVoltage(value);
                        break;
                }
 
 
            } else if (item instanceof FileUpload) {
 
                FileUpload file = (FileUpload) item;
                String filename = file.getFilename();
 
                ByteArrayInputStream stream = new ByteArrayInputStream(ByteBufUtil.getBytes(file.getByteBuf()));
                MockMultipartFile multipartFile
                    = new MockMultipartFile("11", filename, "application/octet-stream", stream);
 
                String picurl = iOssClient.uploadFile(filename, multipartFile);
                log.info("图片上传地址::" + picurl);
                xfByPic.setImageurl(picurl);
            }
        }
        xfByPic.setCreatedAt(new Date());
 
        return xfByPic;
    }
 
 
    // 请求路径验证
    public  String  urlyz(ChannelHandlerContext ctx, FullHttpRequest request) {
        try {
 
            URI uri = UriUtils.getURI(request);
            System.err.println("uri.getPath()::::"+uri.getPath());
            System.err.println(""+uri.getPath());
 
            if (uri.getPath().startsWith(UPLOAD_DATA_URL)) {
                return UPLOAD_DATA_URL;
 
            } else if (uri.getPath().startsWith(UPLOAD_FILE_URL)) {
                return UPLOAD_FILE_URL;
 
            } else if (uri.getPath().startsWith(UPLOAD_FILE_URL_OLD)) {
 
                return UPLOAD_FILE_URL_OLD;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
            log.error(getStackString(exception));
            return null;
        }
        return null;
    }
 
 
 
    public static MultipartFile convert(File file) throws IOException {
 
        FileInputStream input = new FileInputStream(file);
        MultipartFile multipartFile = new MockMultipartFile("file",
            file.getName(), "application/octet-stream", input);
 
        return multipartFile;
    }
 
    private String parseFilePath(FullHttpRequest fullRequest) throws URISyntaxException {
        String dirPath = this.nettyHttpConfig.getFileSaveRootPath();
        String fileName = this.parseFileName(fullRequest);
 
        return dirPath +  File.separator + fileName;
    }
 
    private RandomAccessFile getSaveFile(String filePath) {
        try {
            RandomAccessFile file = new RandomAccessFile(filePath, "rw");
            return file;
        } catch (Exception exception) {
            log.error(getStackString(exception));
            return null;
        }
    }
 
    private boolean isValidHttpRequest(ChannelHandlerContext ctx, FullHttpRequest request) {
        try {
            URI uri = UriUtils.getURI(request);
//            log.info(uri.getPath());
            // authorization效验
            String authorization = request.headers().get("Authorization");
            if(!this.nettyHttpConfig.getAuthorization().equals(authorization)) {
                log.warn("Authorization校验失败!!");
                // 校验不通过
                HttpResponseUtils.sendResponseJson(ctx, HttpResponseStatus.UNAUTHORIZED, HttpResponseStatus.UNAUTHORIZED.toString());
                // 校验不通过
                return false;
            }
 
            // 密码校验
//            boolean authFlag = auth(ctx, uri);
//            if (!authFlag) {
//                // 校验不通过
//                HttpResponseUtils.sendResponseJson(ctx, HttpResponseStatus.UNAUTHORIZED, HttpResponseStatus.UNAUTHORIZED.toString());
//                // 校验不通过
//                return false;
//            }
 
            // uri 路径校验
//            if (!uri.getPath().startsWith(UPLOAD_FILE_URL)) {
//                HttpResponseUtils.sendResponseJson(ctx, HttpResponseStatus.BAD_REQUEST,"the url must starts with /postfile");
//                return false;
//            }
 
            return true;
        } catch (Exception exception) {
            exception.printStackTrace();
            log.error(getStackString(exception));
            return false;
        }
    }
 
    private String parseFileName(FullHttpRequest fullRequest) throws URISyntaxException {
        URI uri = UriUtils.getURI(fullRequest);
        Map<String, String> queryParams = UriUtils.getQueryParams(uri);
        try {
            if (queryParams.containsKey(FILENAME_PARAM)) {
                return queryParams.get(FILENAME_PARAM);
            } else {
                SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY_MM_dd_HH_MM_SS_sss");
                String filePureName = dateFormat.format(new Date());
                return filePureName + "." + queryParams.get(FILE_EXTNAME_PARAM);
            }
        } catch (Exception exception) {
            log.error(ExceptionUtils.getStackString(exception));
            return String.valueOf(System.currentTimeMillis());
        }
    }
 
    private void writeResponse(ChannelHandlerContext ctx, String context) {
        ByteBuf buf = Unpooled.copiedBuffer(context, CharsetUtil.UTF_8);
        FullHttpResponse response = new DefaultFullHttpResponse(
                HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf);
        response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html;charset=utf-8");
        //设置短连接 addListener 写完马上关闭连接
        ctx.channel().writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
    }
 
    /**
     * 进行请求的密码校验
     * @param ctx
     * @param uri
     * @return
     */
    private boolean auth(ChannelHandlerContext ctx, URI uri) {
        Map<String, String> queryParams = UriUtils.getQueryParams(uri);
        String passwd = queryParams.get("passwd");
        if (this.nettyHttpConfig.getFileUpPasswd().equals(passwd)) {
            return true;
        }
        return false;
    }
 
    @Override
    public void channelInactive(ChannelHandlerContext ctx) {
        ctx.close();
    }
 
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.error(getStackString(cause));
        cause.printStackTrace();
        ctx.close();
    }
}