jinlin
2024-02-21 b27d06b8f0b805efd16e28fd80a57a0ed8a053ce
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
/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有,侵权必究!
 */
 
package com.zt.modules.oss.controller;
 
import cn.hutool.core.convert.Convert;
import com.zt.common.annotation.LogOperation;
import com.zt.common.annotation.QueryParam;
import com.zt.common.constant.Constant;
import com.zt.common.db.query.QueryFilter;
import com.zt.common.exception.ErrorCode;
import com.zt.common.servlet.PageResult;
import com.zt.common.servlet.Result;
import com.zt.core.oss.dto.OssDto;
import com.zt.modules.oss.cloud.LocalStorageService;
import com.zt.modules.oss.constant.OssConstant;
import com.zt.modules.oss.enums.CloudChannel;
import com.zt.modules.oss.model.QdSysOss;
import com.zt.modules.oss.model.SysOss;
import com.zt.modules.oss.service.QdSysOssService;
import com.zt.modules.oss.service.SysOssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
 
/**
 * 文件上传
 *
 * @author hehz
 */
@RestController
@RequestMapping("sys/oss")
@Api(tags = "文件上传")
public class SysOssController {
    @Autowired
    private SysOssService sysOssService;
    @Autowired
    private LocalStorageService localStorageService;
    @Autowired
    private QdSysOssService qdSysOssService;
 
    @GetMapping("page")
    @ApiOperation(value = "分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.Q.PAGE, value = Constant.QV.PAGE, required = true, dataType = Constant.QT.INT),
            @ApiImplicitParam(name = Constant.Q.LIMIT, value = Constant.QV.LIMIT, required = true, dataType = Constant.QT.INT),
            @ApiImplicitParam(name = Constant.Q.ORDER_FIELD, value = Constant.QV.ORDER_FIELD, dataType = Constant.QT.STRING),
            @ApiImplicitParam(name = Constant.Q.ORDER, value = Constant.QV.ORDER, dataType = Constant.QT.STRING),
            @ApiImplicitParam(name = "fileName", value = "文件名", dataType = Constant.QT.STRING, format = "name^LK")})
//    @RequiresPermissions("sys:oss:all")
    public PageResult<SysOss> page(@ApiIgnore @QueryParam QueryFilter queryFilter) {
 
        return PageResult.ok(sysOssService.page(queryFilter));
    }
 
    @PostMapping("uploadNew")
    @ApiOperation(value = "上传文件")
    @LogOperation("文件上传模块--->上传")
//    @RequiresPermissions("sys:oss:all")
    public Result<OssDto> uploadNew(@RequestBody Map<String, Object> fileData) throws Exception {
        if (fileData.isEmpty()) {
            return Result.error(ErrorCode.UPLOAD_FILE_EMPTY);
        }
        // 上传文件
        OssDto oss = sysOssService.uploadNew(fileData);
        return Result.ok(oss);
    }
 
    @PostMapping("upload")
    @ApiOperation(value = "上传文件")
    @LogOperation("文件上传模块--->上传")
//    @RequiresPermissions("sys:oss:all")
    public Result<OssDto> upload(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            return Result.error(ErrorCode.UPLOAD_FILE_EMPTY);
        }
 
        // 上传文件
        OssDto oss = sysOssService.upload(file);
 
        return Result.ok(oss);
    }
 
 
    @DeleteMapping
    @ApiOperation(value = "删除")
    @LogOperation("文件上传模块--->删除")
//    @RequiresPermissions("sys:oss:all")
    public Result delete(@RequestBody Long[] ids) {
        for (Long id : ids) {
            sysOssService.deleteTemporary(id);
        }
        return Result.ok();
    }
 
    @RequestMapping(value = "/content")
    public void content(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String id = request.getParameter(OssConstant.ID);
        if (StringUtils.isNotEmpty(id)) {
            SysOss oss = sysOssService.getById(Long.parseLong(id));
            if (CloudChannel.LOCAL.getValue().equals(oss.getChannel())) { // 本地
                System.out.println(CloudChannel.LOCAL.getValue());
                System.out.println(oss.getChannel());
                localStorageService.content(oss, response);
            }
        }
    }
 
    @RequestMapping(value = "/content2")
    public ResponseEntity<byte[]> content2(HttpServletRequest request) throws Exception {
        //String id = fileId;
        String id = request.getParameter("fileId");
        if (StringUtils.isNotEmpty(id)) {
            SysOss oss = sysOssService.getById(Convert.toLong(id));
            if (CloudChannel.LOCAL.getValue().equals(oss.getChannel())) { // 本地
                System.out.println(CloudChannel.LOCAL.getValue());
                System.out.println(oss.getChannel());
                return localStorageService.content2(oss);
            }
        }
        String errorMessage = "文件不存在";
        return new ResponseEntity<>(errorMessage.getBytes(), HttpStatus.NOT_FOUND);
    }
 
    @RequestMapping(value = "/commFile")
    public void getCommFile(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String fileFlag = request.getParameter("fileFlag");
        if (StringUtils.isNotEmpty(fileFlag)) {
            localStorageService.getCommFile(fileFlag, response);
        }
    }
 
    @RequestMapping(value = "/qdContent")
    public void qdContent(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String id = request.getParameter("id");
        if (StringUtils.isNotEmpty(id)) {
            QdSysOss qdSysOss = qdSysOssService.getById(Long.parseLong(id));
            if (CloudChannel.LOCAL.getValue().equals(qdSysOss.getChannel())) {
                System.out.println("qd:" + CloudChannel.LOCAL.getValue());
                System.out.println("qd:" + qdSysOss.getChannel());
                localStorageService.qdContent(qdSysOss, response);
            }
        }
    }
 
    @GetMapping("/removeBase64File")
    @ResponseBody
    public Result<String> removeBase64File() {
        try {
            sysOssService.removeBase64File();
        } catch (Exception e) {
 
        }
        return Result.ok("OK");
    }
}