yangys
2024-04-04 ed4a5236bab800094be4a8378f5098eebe3de6ac
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
package com.qianwen.smartman.modules.resource.endpoint;
 
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import io.swagger.annotations.Api;
import me.zhyd.oauth.log.Log;
 
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.time.DateUtils;
import com.qianwen.core.log.exception.ServiceException;
import com.qianwen.core.oss.model.BladeFile;
import com.qianwen.core.oss.model.OssFile;
import com.qianwen.core.scanner.modular.annotation.GetResource;
import com.qianwen.core.scanner.modular.annotation.PostResource;
import com.qianwen.core.scanner.modular.stereotype.ApiResource;
import com.qianwen.core.tenant.annotation.NonDS;
import com.qianwen.core.tool.api.R;
import com.qianwen.core.tool.utils.FileUtil;
import com.qianwen.core.tool.utils.Func;
import com.qianwen.smartman.modules.resource.builder.oss.OssBuilder;
import com.qianwen.smartman.modules.resource.entity.Attach;
import com.qianwen.smartman.modules.resource.service.IAttachService;
import com.qianwen.smartman.modules.resource.vo.OssFileVO;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
 
@Api(value = "对象存储端点", tags = {"对象存储端点"})
@RestController
@ApiResource({"blade-resource/oss/endpoint"})
@NonDS
/* loaded from: blade-api.jar:BOOT-INF/classes/org/springblade/modules/resource/endpoint/OssEndpointController.class */
public class OssEndpointController {
    private final OssBuilder ossBuilder;
    private final IAttachService attachService;
 
    public OssEndpointController(final OssBuilder ossBuilder, final IAttachService attachService) {
        this.ossBuilder = ossBuilder;
        this.attachService = attachService;
    }
 
    @PostResource({"/make-bucket"})
    public R makeBucket(@RequestParam String bucketName) {
        this.ossBuilder.template().makeBucket(bucketName);
        return R.success("创建成功");
    }
 
    @PostResource({"/remove-bucket"})
    public R removeBucket(@RequestParam String bucketName) {
        this.ossBuilder.template().removeBucket(bucketName);
        return R.success("删除成功");
    }
 
    @PostResource({"/copy-file"})
    public R copyFile(@RequestParam String fileName, @RequestParam String destBucketName, String destFileName) {
        this.ossBuilder.template().copyFile(fileName, destBucketName, destFileName);
        return R.success("操作成功");
    }
 
    @GetResource({"/stat-file"})
    public R<OssFile> statFile(@RequestParam String fileName) {
        return R.data(this.ossBuilder.template().statFile(fileName));
    }
 
    @GetResource({"/file-path"})
    public R<String> filePath(@RequestParam String fileName) {
        return R.data(this.ossBuilder.template().filePath(fileName));
    }
 
    @GetResource({"/file-link"})
    public R<String> fileLink(@RequestParam String fileName) {
        return R.data(this.ossBuilder.template().fileLink(fileName));
    }
 
    @GetResource({"/file-content"})
    public void getFileContent(@RequestParam String fileName, HttpServletResponse response) {
       
        try (InputStream objectStream = this.ossBuilder.template().getObject(fileName)) {
            response.setContentType("application/octet-stream; charset=UTF-8");
            response.addHeader("Expires", "0");
            response.addHeader("Pragma", "no-cache");
            response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
            IoUtil.copy(objectStream, response.getOutputStream());
            
        } catch (IOException e) {
            Log.error("获取文件内容错误",e);
            e.printStackTrace();
        } catch (Exception e) {
            throw new ServiceException("文件不存在");
        } 
       
    }
 
    @PostResource({"/put-file"})
    public R<BladeFile> putFile(@RequestParam MultipartFile file) {
        BladeFile bladeFile = this.ossBuilder.template().putFile(file.getOriginalFilename(), file);
        return R.data(bladeFile);
    }
 
    @PostResource({"/put-file-detail"})
    public R<OssFileVO> putFileWithDeatil(@RequestParam MultipartFile file) {
        BladeFile bladeFile = this.ossBuilder.template().putFile(file.getOriginalFilename(), file);
        OssFile ossFile = this.ossBuilder.template().statFile(bladeFile.getName());
        OssFileVO ossFileVO = OssFileVO.builder().length(ossFile.getLength()).putTimeStr(DateUtil.format(DateUtils.addHours(ossFile.getPutTime(), 8), DatePattern.NORM_DATETIME_MINUTE_FORMAT)).oriFileName(bladeFile.getOriginalName()).link(bladeFile.getLink()).build();
        return R.data(ossFileVO);
    }
 
    @PostResource({"/put-file-by-name"})
    public R<BladeFile> putFileByName(@RequestParam String fileName, @RequestParam MultipartFile file) {
        BladeFile bladeFile = this.ossBuilder.template().putFile(fileName, file);
        return R.data(bladeFile);
    }
 
    @PostResource({"/put-file-attach"})
    public R<BladeFile> putFileAttach(@RequestParam MultipartFile file) {
        try {
            String fileName = file.getOriginalFilename();
            BladeFile bladeFile = this.ossBuilder.template().putFile(fileName, file.getInputStream());
            Long attachId = buildAttach(fileName, Long.valueOf(file.getSize()), bladeFile);
            bladeFile.setAttachId(attachId);
            return R.data(bladeFile);
        } catch (Throwable ex) {
                //log.error("putFileAttachByName,异常,file={}",fileName,ex);
                throw new ServiceException("putFileAttach异常"+ex.getMessage());
            
             } 
    }
 
    @PostResource({"/put-file-attach-by-name"})
    public R<BladeFile> putFileAttachByName(@RequestParam String fileName, @RequestParam MultipartFile file) {
        try {
            BladeFile bladeFile = this.ossBuilder.template().putFile(fileName, file.getInputStream());
            Long attachId = buildAttach(fileName, Long.valueOf(file.getSize()), bladeFile);
            bladeFile.setAttachId(attachId);
            return R.data(bladeFile);
         } catch (Throwable ex) {
             //log.error("putFileAttachByName,异常,file={}",fileName,ex);
             throw new ServiceException("putFileAttachByName异常"+ex.getMessage());
             
          } 
    }
 
    private Long buildAttach(String fileName, Long fileSize, BladeFile bladeFile) {
        String fileExtension = FileUtil.getFileExtension(fileName);
        Attach attach = new Attach();
        attach.setDomainName(bladeFile.getDomain());
        attach.setLink(bladeFile.getLink());
        attach.setName(bladeFile.getName());
        attach.setOriginalName(bladeFile.getOriginalName());
        attach.setAttachSize(fileSize);
        attach.setExtension(fileExtension);
        this.attachService.save(attach);
        return attach.getId();
    }
 
    @PostResource({"/remove-file"})
    public R removeFile(@RequestParam String fileName) {
        this.ossBuilder.template().removeFile(fileName);
        return R.success("操作成功");
    }
 
    @PostResource({"/remove-files"})
    public R removeFiles(@RequestParam String fileNames) {
        this.ossBuilder.template().removeFiles(Func.toStrList(fileNames));
        return R.success("操作成功");
    }
 
    @PostResource({"/get-file-detail"})
    public R<BladeFile> getFileDetail(@RequestParam MultipartFile file) {
        try {
            BladeFile bladeFile = this.ossBuilder.template().putFile(file.getName(), file.getInputStream());
            long size = file.getSize();
            Long attachId = buildAttach(file.getName(), Long.valueOf(size), bladeFile);
            bladeFile.setAttachId(attachId);
            return R.data(bladeFile);
        } catch (Throwable ex) {
             //log.error("putFileAttachByName,异常,file={}",fileName,ex);
             throw new ServiceException("getFileDetail异常"+ex.getMessage());
             
        } 
    }
}