xiangpei
2024-03-28 0f431b52e0936456bd165d9553761bfd8a5a0517
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
package com.mindskip.xzs.controller.teacher;
 
 
import cn.hutool.core.codec.Base64;
import com.mindskip.xzs.base.BaseApiController;
import com.mindskip.xzs.base.RestResponse;
import com.mindskip.xzs.service.FileUpload;
import com.mindskip.xzs.service.ExcelImportService;
import com.mindskip.xzs.viewmodel.admin.file.UeditorConfigVM;
import com.mindskip.xzs.viewmodel.admin.file.UploadResultVM;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
 
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
 
 
/**
 * @version 2.2.0
 * @description: 上传
 * Copyright (C), 2020-2021, 武汉思维跳跃科技有限公司
 * @date 2021 /9/7 9:45
 */
@AllArgsConstructor
@RequestMapping("/api/teacher/upload")
@Controller("TeacherUploadController")
public class UploadController extends BaseApiController {
 
    private final FileUpload fileUpload;
    private static final Logger logger = LoggerFactory.getLogger(com.mindskip.xzs.controller.admin.UploadController.class);
    private static final String SCRAWL_UPLOAD = "scrawlUpload";
    private static final String SCRAWL_FIELD_NAME = "scrawl";
    private static final String IMAGE_UPLOAD = "imgUpload";
    private static final String IMAGE_UPLOAD_FILE = "upFile";
    private static final String STATE = "SUCCESS";
    private final ExcelImportService excelImportService;
 
    /**
     * 图片上传
     *
     * @param request the request
     * @return the object
     */
    @ResponseBody
    @RequestMapping("/configAndUpload")
    public Object upload(HttpServletRequest request) {
        String action = request.getParameter("action");
        if (null == action)
            return null;
        String filePath = null;
        switch (action) {
            case IMAGE_UPLOAD:
                MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
                MultipartFile multipartFile = multipartHttpServletRequest.getFile(IMAGE_UPLOAD_FILE);
                String fileName = multipartFile.getOriginalFilename();
                long attachSize = multipartFile.getSize();
                try (InputStream inputStream = multipartFile.getInputStream()) {
                    filePath = fileUpload.fileUpload(inputStream, attachSize, fileName, "image");
                } catch (IOException e) {
                    logger.error(e.getMessage(), e);
                }
                return new UploadResultVM(fileName, fileName, filePath, multipartFile.getSize(), fileName, STATE);
            case SCRAWL_UPLOAD:
                String imageBase64 = request.getParameter(SCRAWL_FIELD_NAME);
                String uuidName = UUID.randomUUID().toString();
                try {
                    File file = File.createTempFile(uuidName, ".png");
                    try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
                        byte[] buff = Base64.decode(imageBase64);
                        fileOutputStream.write(buff);
                        filePath = fileUpload.fileUpload(file, uuidName + ".png", "latex");
                    }
                } catch (IOException e) {
                    logger.error(e.getMessage(), e);
                }
                return new UploadResultVM(filePath, STATE);
            default:
                return new UeditorConfigVM(SCRAWL_UPLOAD, SCRAWL_FIELD_NAME, IMAGE_UPLOAD, IMAGE_UPLOAD_FILE);
        }
    }
 
 
    /**
     * 题目导入
     *
     * @param request the request
     * @return the rest response
     */
    @RequestMapping("/excel/question")
    @ResponseBody
    public RestResponse questionUploadAndReadExcel(HttpServletRequest request) {
        MultipartFile file = ((MultipartHttpServletRequest) request).getFile("file");
        String excelTempPath = null;
        try (InputStream inputStream = file.getInputStream()) {
            excelTempPath = File.createTempFile(UUID.randomUUID().toString(), ".xlsx").getAbsolutePath();
            excelImportService.teacherQuestionImport(inputStream, excelTempPath, getCurrentUser());
        } catch (IOException e) {
            logger.error(e.getMessage(), "文件上传失败");
        }
        String filePath = fileUpload.fileUpload(new File(excelTempPath), "题目导入结果.xlsx", "excel");
        return RestResponse.ok(filePath);
    }
 
 
    /**
     * 学生导入
     *
     * @param request the request
     * @return the rest response
     */
    @RequestMapping("/excel/student")
    @ResponseBody
    public RestResponse studentUploadAndReadExcel(HttpServletRequest request) {
        MultipartFile file = ((MultipartHttpServletRequest) request).getFile("file");
        String excelTempPath = null;
        try (InputStream inputStream = file.getInputStream()) {
            excelTempPath = File.createTempFile(UUID.randomUUID().toString(), ".xlsx").getAbsolutePath();
            excelImportService.studentImport(inputStream, excelTempPath, getCurrentUser());
        } catch (IOException e) {
            logger.error(e.getMessage(), "文件上传失败");
        }
        String filePath = fileUpload.fileUpload(new File(excelTempPath), "学生导入结果.xlsx", "excel");
        return RestResponse.ok(filePath);
    }
}