aix
2024-07-24 2cba01a6b6f67cfa97d0c4e0cc589302fab817ba
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package cn.gistack.resource.endpoint;
 
import cn.gistack.resource.builder.oss.OssBuilder;
import cn.gistack.resource.entity.TextualResearchAttach;
import cn.gistack.resource.panoramaUtils.Img4JavaUtil;
import cn.gistack.resource.panoramaUtils.QietuResult;
import cn.gistack.resource.service.IAttachService;
import cn.gistack.resource.service.TextualResearchAttachService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.apache.commons.io.FileUtils;
import org.springblade.core.oss.model.BladeFile;
import org.springblade.core.oss.model.OssFile;
import org.springblade.core.secure.annotation.PreAuth;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.RoleConstant;
import org.springblade.core.tool.utils.FileUtil;
import org.springblade.core.tool.utils.Func;
import cn.gistack.resource.entity.Attach;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.File;
import java.io.FileInputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
 
/**
 * 对象存储端点
 *
 * @author Chill
 */
@NonDS
@RestController
@AllArgsConstructor
@RequestMapping("/oss/endpoint")
@Api(value = "对象存储端点", tags = "对象存储端点")
public class OssEndpoint {
 
    /**
     * 对象存储构建类
     */
    private final OssBuilder ossBuilder;
 
    /**
     * 附件表服务
     */
    private final IAttachService attachService;
 
    /**
     * 附件表服务
     */
    private final TextualResearchAttachService textualResearchAttachService;
 
    /**
     * 创建存储桶
     *
     * @param bucketName 存储桶名称
     * @return Bucket
     */
    @SneakyThrows
    @PostMapping("/make-bucket")
    @PreAuth(RoleConstant.HAS_ROLE_ADMIN)
    public R makeBucket(@RequestParam String bucketName) {
        ossBuilder.template().makeBucket(bucketName);
        return R.success("创建成功");
    }
 
    /**
     * 创建存储桶
     *
     * @param bucketName 存储桶名称
     * @return R
     */
    @SneakyThrows
    @PostMapping("/remove-bucket")
    @PreAuth(RoleConstant.HAS_ROLE_ADMIN)
    public R removeBucket(@RequestParam String bucketName) {
        ossBuilder.template().removeBucket(bucketName);
        return R.success("删除成功");
    }
 
    /**
     * 拷贝文件
     *
     * @param fileName       存储桶对象名称
     * @param destBucketName 目标存储桶名称
     * @param destFileName   目标存储桶对象名称
     * @return R
     */
    @SneakyThrows
    @PostMapping("/copy-file")
    public R copyFile(@RequestParam String fileName, @RequestParam String destBucketName, String destFileName) {
        ossBuilder.template().copyFile(fileName, destBucketName, destFileName);
        return R.success("操作成功");
    }
 
    /**
     * 获取文件信息
     *
     * @param fileName 存储桶对象名称
     * @return InputStream
     */
    @SneakyThrows
    @GetMapping("/stat-file")
    public R<OssFile> statFile(@RequestParam String fileName) {
        return R.data(ossBuilder.template().statFile(fileName));
    }
 
    /**
     * 获取文件相对路径
     *
     * @param fileName 存储桶对象名称
     * @return String
     */
    @SneakyThrows
    @GetMapping("/file-path")
    public R<String> filePath(@RequestParam String fileName) {
        return R.data(ossBuilder.template().filePath(fileName));
    }
 
 
    /**
     * 获取文件外链
     *
     * @param fileName 存储桶对象名称
     * @return String
     */
    @SneakyThrows
    @GetMapping("/file-link")
    public R<String> fileLink(@RequestParam String fileName) {
        return R.data(ossBuilder.template().fileLink(fileName));
    }
 
    /**
     * 上传文件
     *
     * @param file 文件
     * @return ObjectStat
     */
    @SneakyThrows
    @PostMapping("/put-file")
    public R<BladeFile> putFile(@RequestParam MultipartFile file) {
        BladeFile bladeFile = ossBuilder.template().putFile(file.getOriginalFilename(), file.getInputStream());
        return R.data(bladeFile);
    }
 
    /**
     * 自定义前缀上传文件
     *
     * @param file 文件
     * @param prefixPath 文件
     * @return ObjectStat
     */
    @SneakyThrows
    @PostMapping("/put-file-by-prefix-path")
    public R<BladeFile> putFileByPrefixPath(@RequestParam MultipartFile file,@RequestParam String prefixPath) {
        BladeFile bladeFile = ossBuilder.templateByPrefixPath(prefixPath).putFile(file.getOriginalFilename(), file.getInputStream());
        return R.data(bladeFile);
    }
 
    /**
     * 批量自定义前缀上传文件
     *
     * @param files 文件
     * @param prefixPath 文件
     * @return ObjectStat
     */
    @SneakyThrows
    @PostMapping("/batch-put-file-by-prefix-path")
    public R batchPutFileByPrefixPath(@RequestParam MultipartFile[] files,@RequestParam String prefixPath) {
        List<BladeFile> list = new ArrayList<>();
        for (MultipartFile file : files) {
            BladeFile bladeFile = ossBuilder.templateByPrefixPath(prefixPath).putFile(file.getOriginalFilename(), file.getInputStream());
            list.add(bladeFile);
        }
        return R.data(list);
    }
 
    /**
     * 考证附件上传并保存
     * @param file 文件
     * @param resGuid 水库编号
     * @return
     */
    @SneakyThrows
    @PostMapping("/put-file-by-textualResearchAttach")
    public R putFileByPrefixTextualResearchAttach(@RequestParam MultipartFile file,String resGuid,Integer type) {
        BladeFile bladeFile = ossBuilder.templateByPrefixPath("textualResearchAttach").putFile(file.getOriginalFilename(), file.getInputStream());
        buildTextualResearchAttach(resGuid,bladeFile,type);
        return R.data(bladeFile);
    }
 
    /**
     * 全景图片上传,并切图
     * @param multipartFile 文件
     * @param resGuid 水库编号
     * @return
     */
    @SneakyThrows
    @PostMapping("/put-file-by-panorama")
    public R putFileByPanorama(@RequestParam MultipartFile multipartFile, String resGuid) {
 
        // 临时文件路径
        String  linShiBaseUrl = "/data/panorama/qietu/";
//        String fileName = multipartFile.getOriginalFilename();
        // 获取文件名
        String picName = multipartFile.getOriginalFilename();// 原始全景图名称
        String dirPath = linShiBaseUrl + resGuid + "/"; //临时文件夹路径
 
        File dir = new File(dirPath);
        if (dir.isDirectory()) {
            // 清除该目录下的文件及子目录文件而不删除该目录文件夹。该目录不存在会报错
            FileUtils.cleanDirectory(dir);
        } else {
            dir.mkdirs();
        }
        String picUrl = dirPath + picName; // 原始全景图文件路径
        File file = new File(picUrl); // 文件上传的原始全景图,存入临时路径
        multipartFile.transferTo(file);
        FileInputStream ysfileInputStream = new FileInputStream(file);
        BladeFile qjbladeFile = ossBuilder.templateByPrefixPathPanorama("panorama", resGuid).putFile(picName, ysfileInputStream);
        String yuanshiQJurl = qjbladeFile.getLink();
        ysfileInputStream.close();
 
        /**
         * 这里开始切图
         */
        HashMap<String,String> qietuHashMap = new HashMap<String,String>();
        int col = 8;
        int row = 4;
        String[] split = picName.split("\\.");
        String picHeadName = split[0];
        String picLastName = split[1];
 
        // 获取原始全景图图片信息
        HashMap<String, Integer> imgInfoRT = Img4JavaUtil.getImgInfoRT(file);
        Integer width = imgInfoRT.get("width");
        Integer height = imgInfoRT.get("height");
        // 计算切成小图的长和宽
        Integer biliWidth = width/col;
        Integer biliHeight = height/row;
 
        for (int i=0 ; i < col ; i++) {
            // 计算图片的横坐标(长边)
            int x = i * biliWidth;
            for (int j=0 ; j<row ; j++){
                // 计算图片的纵坐标(宽边)
                int  y = j * biliHeight;
                // 切图名称
                String outpicName = picHeadName + "_" + i + "_" +j + "." + picLastName;
                // 切图后临时图片的的路径
                String lastiUrl = dirPath + outpicName;
                Img4JavaUtil.cropImg(picUrl ,lastiUrl ,biliWidth,biliHeight,x,y);// 切图存放进临时图片中
                qietuHashMap.put(outpicName,lastiUrl);
            }
        }
 
        // 切图完成后删除原始全景图
        if (file.exists()) {
            // 删除指定文件,不存在报异常
            FileUtils.forceDelete(file);
        }
        /************************************切图结束********************************************/
 
 
        List<String> qieTuUrlList = new ArrayList<String>();
        String urlHead = null;
        Set<String> mapKey = qietuHashMap.keySet();
        if(qietuHashMap != null && qietuHashMap.size()>0) {
            for (String picQName : mapKey) {
                String url = qietuHashMap.get(picQName);
                Path qietuPath = Paths.get(url);
                File qietufile = qietuPath.toFile();
                FileInputStream fileInputStream = new FileInputStream(qietufile);
                BladeFile bladeFile = ossBuilder.templateByPrefixPathPanorama("panorama", resGuid).putFile(picQName, fileInputStream);
                fileInputStream.close(); // 关闭文件流
 
                // 切图上传完成后删除
                if (qietufile.exists()) {
                    // 删除指定文件,不存在报异常
                    FileUtils.forceDelete(qietufile);
                }
                String link = bladeFile.getLink();
                qieTuUrlList.add(link);// 上传后的图片路径
                urlHead = link.replace(picQName,"");
            }
        }
        QietuResult qietuResult = new QietuResult();
        qietuResult.setQieTuUrlList(qieTuUrlList);
        qietuResult.setUrlHead(urlHead);
        qietuResult.setRow(row);
        qietuResult.setCol(col);
        qietuResult.setQuanJingWidth(width);
        qietuResult.setQuanJingHeight(height);
        qietuResult.setQjName(picName);
        qietuResult.setQjUrl(yuanshiQJurl);
 
 
        return R.data(qietuResult) ;
    }
 
    @SneakyThrows
    @PostMapping("/put-file-by-panorama2")
    public R putFileByPanorama2(@RequestParam MultipartFile multipartFile, String resGuid){
 
        return null;
    }
 
 
    /**
     * 保存考证附件上传数据
     * @param bladeFile
     */
    private void buildTextualResearchAttach(String resGuid,BladeFile bladeFile,Integer type) {
        TextualResearchAttach attach = new TextualResearchAttach();
        attach.setResGuid(resGuid);
        attach.setType(type);
        attach.setPath(bladeFile.getName());
        attach.setName(bladeFile.getOriginalName());
        attach.setCreateTime(new Date());
        // 保存
        textualResearchAttachService.save(attach);
    }
 
    /**
     * 上传文件
     *
     * @param fileName 存储桶对象名称
     * @param file     文件
     * @return ObjectStat
     */
    @SneakyThrows
    @PostMapping("/put-file-by-name")
    public R<BladeFile> putFile(@RequestParam String fileName, @RequestParam MultipartFile file) {
        BladeFile bladeFile = ossBuilder.template().putFile(fileName, file.getInputStream());
        return R.data(bladeFile);
    }
 
    /**
     * 上传文件并保存至附件表
     *
     * @param file 文件
     * @return ObjectStat
     */
    @SneakyThrows
    @PostMapping("/put-file-attach")
    public R<BladeFile> putFileAttach(@RequestParam MultipartFile file) {
        String fileName = file.getOriginalFilename();
        BladeFile bladeFile = ossBuilder.template().putFile(fileName, file.getInputStream());
        Long attachId = buildAttach(fileName, file.getSize(), bladeFile);
        bladeFile.setAttachId(attachId);
        return R.data(bladeFile);
    }
 
 
 
    /**
     * 上传文件并保存至附件表
     *
     * @param fileName 存储桶对象名称
     * @param file     文件
     * @return ObjectStat
     */
    @SneakyThrows
    @PostMapping("/put-file-attach-by-name")
    public R<BladeFile> putFileAttach(@RequestParam String fileName, @RequestParam MultipartFile file) {
        BladeFile bladeFile = ossBuilder.template().putFile(fileName, file.getInputStream());
        Long attachId = buildAttach(fileName, file.getSize(), bladeFile);
        bladeFile.setAttachId(attachId);
        return R.data(bladeFile);
    }
 
    /**
     * 构建附件表
     *
     * @param fileName  文件名
     * @param fileSize  文件大小
     * @param bladeFile 对象存储文件
     * @return attachId
     */
    private Long buildAttach(String fileName, Long fileSize, BladeFile bladeFile) {
        String fileExtension = FileUtil.getFileExtension(fileName);
        Attach attach = new Attach();
        attach.setDomainUrl(bladeFile.getDomain());
        attach.setLink(bladeFile.getLink());
        attach.setName(bladeFile.getName());
        attach.setOriginalName(bladeFile.getOriginalName());
        attach.setAttachSize(fileSize);
        attach.setExtension(fileExtension);
        attachService.save(attach);
        return attach.getId();
    }
 
    /**
     * 删除文件
     *
     * @param fileName 存储桶对象名称
     * @return R
     */
    @SneakyThrows
    @PostMapping("/remove-file")
    @PreAuth(RoleConstant.HAS_ROLE_ADMIN)
    public R removeFile(@RequestParam String fileName) {
        ossBuilder.template().removeFile(fileName);
        return R.success("操作成功");
    }
 
    /**
     * 批量删除文件
     *
     * @param fileNames 存储桶对象名称集合
     * @return R
     */
    @SneakyThrows
    @PostMapping("/remove-files")
    @PreAuth(RoleConstant.HAS_ROLE_ADMIN)
    public R removeFiles(@RequestParam String fileNames) {
        ossBuilder.template().removeFiles(Func.toStrList(fileNames));
        return R.success("操作成功");
    }
 
}