Helius
2022-08-19 2eb1aa06e1d01914106fa85626adb165597c3f95
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
package com.xcong.farmer.cms.modules.system.controller;
 
import cn.hutool.core.img.Img;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import com.xcong.farmer.cms.common.contants.AppContants;
import com.xcong.farmer.cms.common.response.Result;
import com.xcong.farmer.cms.modules.system.dto.AdminLoginDto;
import com.xcong.farmer.cms.modules.system.service.ICommonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.validation.Valid;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
import java.io.InputStream;
import java.util.Map;
 
 
@RestController
@RequestMapping(value = "/api/common")
@Slf4j
@Api(value = "登录", tags = "登录")
public class AdminCommonController {
 
    @Resource
    private ICommonService iCommonService;
 
    @ApiOperation(value = "登陆接口", notes = "登陆接口")
    @PostMapping("/login")
    public Result login(@RequestBody @Valid AdminLoginDto adminLoginDto) {
        return iCommonService.login(adminLoginDto);
    }
 
    @ApiOperation(value = "获取验证码", notes = "获取验证码")
    @GetMapping("/captcha")
    public Result captcha() throws IOException {
        return iCommonService.captchaCreator();
    }
 
    /**
     * 用户退出登录
     * @return
     */
    @ApiOperation(value="用户退出登录", notes="用户退出登录")
    @PostMapping(value = "/Logout")
    public Result  memberLogout() {
        return iCommonService.memberLogout();
    }
 
    private Long maxSize = 1024*1024*100L;
 
 
 
//    /**
//     * 文件上传方法
//     */
//    @ApiOperation(value = "文件上传", notes = "文件上传")
//    @PostMapping(value = "/doUpload")
//    public Result doFileUpload(@RequestBody @Validated AdminBase64UploadDto uploadDto){
//        // 文件保存目录路径
//        String savePath = AppContants.PICTURE_PATH;
//        // 文件保存目录URL
//        String saveUrl = resourceUrl;
//        // 保存和访问路径检查
//        if (StrUtil.isEmpty(saveUrl) || StrUtil.isEmpty(savePath)) {
//            return Result.fail("文件上传失败");
//        }
//        // 检查目录
//        File uploadDir = new File(savePath);
//        if (!uploadDir.isDirectory()) {
//            uploadDir.mkdir();
//        }
//        log.info("uploadDto:" + uploadDto.getBase64Str());
//        BASE64Decoder decoder = new BASE64Decoder();
//        byte[] bytes = new byte[0];
//        try {
//            bytes = decoder.decodeBuffer(uploadDto.getBase64Str());
//        } catch (IOException e) {
//            return Result.fail("上传文件失败");
//        }
//
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//        String ymd = sdf.format(new Date());
//        savePath += ymd + "/";
//        saveUrl += ymd + "/";
//        File dirFile = new File(savePath);
//        if (!dirFile.exists()) {
//            dirFile.mkdirs();
//        }
//        if (bytes.length > maxSize) {
//            return Result.fail("上传文件大小超过限制");
//        }
//        String newFileName = IdUtil.simpleUUID() + IdUtil.simpleUUID() + ".png";
//        File uploadedFile = new File(savePath, newFileName);
//        try {
//            FileCopyUtils.copy(bytes, uploadedFile);
//        } catch (Exception e) {
//            return Result.fail("上传文件失败");
//        }
//        log.info("saveUrl:" + saveUrl);
//        String visitPath = saveUrl + newFileName;
//        log.info("上传一个文件:" + newFileName);
//        log.info("访问路径:" + visitPath);
//
//        return Result.ok("上传成功",visitPath);
//    }
 
    @Value("${static.resource.url}")
    private String resourceUrl;
 
    @Value("${static.resource.path}")
    private String resourcePath;
 
    @ApiOperation(value = "文件上传", notes = "文件上传")
    @PostMapping("/uploadFile")
    public Result uploadFile(@RequestParam("file") MultipartFile file) throws Exception {
 
        // 文件保存目录路径
        String savePath = resourcePath;
        // 文件保存目录URL
        String saveUrl = resourceUrl;
        // 检查目录
        File uploadDir = new File(savePath);
        if (!uploadDir.isDirectory()) {
            uploadDir.mkdir();
        }
 
 
        // 获得文件的后缀
        String filename = IdUtil.simpleUUID() + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        if (isImage(file.getInputStream())) {
            Thumbnails.of(file.getInputStream())
                    // 图片大小(长宽)压缩比例 从0-1,1表示原图
                    .scale(1f)
                    // 图片质量压缩比例 从0-1,越接近1质量越好
                    .outputQuality(0.5f)
                    .toOutputStream(new FileOutputStream(savePath + filename));
        } else {
            File filepath = new File(savePath + filename);
            try {
                //存文件
                file.transferTo(filepath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
 
        String visitPath = (saveUrl + filename);
        return Result.ok("上传成功",visitPath);
    }
 
    private boolean isImage(InputStream inputStream) {
        BufferedImage read = null;
        try {
            read = ImageIO.read(inputStream);
        } catch (IOException e) {
            return false;
        }
        return read != null;
    }
}