xin
2025-06-03 51df6e262c2d625e700a8194cbe0ec1e40b80843
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
package com.oying.rest;
 
import com.oying.annotation.Log;
import com.oying.domain.BucketStorage;
import com.oying.exception.BadRequestException;
import com.oying.service.BucketStorageService;
import com.oying.domain.dto.BucketStorageQueryCriteria;
import com.oying.utils.FileUtil;
import com.oying.utils.R;
import lombok.RequiredArgsConstructor;
 
import java.util.List;
 
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
 
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.multipart.MultipartFile;
 
/**
 * @author lixin
 * @date 2025-06-03
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "工具:存储桶")
@RequestMapping("/api/bucketStorage")
public class BucketStorageController {
 
    private final BucketStorageService bucketStorageService;
 
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('bucketStorage:list')")
    public void exportBucketStorage(HttpServletResponse response, BucketStorageQueryCriteria criteria) throws IOException {
        bucketStorageService.download(bucketStorageService.queryAll(criteria), response);
    }
 
    @GetMapping
    @ApiOperation("查询存储桶")
    @PreAuthorize("@el.check('bucketStorage:list')")
    public ResponseEntity<Object> queryBucketStorage(BucketStorageQueryCriteria criteria) {
        Page<Object> page = new Page<>(criteria.getPage(), criteria.getSize());
        return new ResponseEntity<>(R.success(bucketStorageService.queryAll(criteria, page)), HttpStatus.OK);
    }
 
    @PostMapping
    @Log("上传存储桶文件")
    @ApiOperation("上传存储桶文件")
    @PreAuthorize("@el.check('bucketStorage:add')")
    public ResponseEntity<Object> create(@RequestParam String name, @RequestParam("file") MultipartFile file) {
        return new ResponseEntity<>(R.success(bucketStorageService.create(name, file)), HttpStatus.CREATED);
    }
 
    @PostMapping("/pictures")
    @ApiOperation("上传图片")
    public ResponseEntity<Object> upload(@RequestParam MultipartFile file) {
        // 判断文件是否为图片
        String suffix = FileUtil.getExtensionName(file.getOriginalFilename());
        FileUtil.checkSize(5, file.getSize());
        if (!FileUtil.IMAGE.equals(FileUtil.getFileType(suffix))) {
            throw new BadRequestException("只能上传图片");
        }
        BucketStorage bucketStorage = bucketStorageService.create(null, file);
        return new ResponseEntity<>(R.success(bucketStorage), HttpStatus.OK);
    }
 
    @DeleteMapping
    @Log("删除存储桶")
    @ApiOperation("删除存储桶")
    @PreAuthorize("@el.check('bucketStorage:del')")
    public ResponseEntity<Object> deleteBucketStorage(@ApiParam(value = "传ID数组[]") @RequestBody List<Long> ids) {
        bucketStorageService.deleteAll(ids);
        return new ResponseEntity<>(R.success(), HttpStatus.OK);
    }
}