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
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
package com.oying.service.impl;
 
import cn.hutool.core.util.ObjectUtil;
import com.obs.services.model.AccessControlList;
import com.obs.services.model.PutObjectResult;
import com.oying.config.properties.FileProperties;
import com.oying.domain.BucketStorage;
import com.oying.exception.BadRequestException;
import com.oying.utils.*;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oying.service.BucketStorageService;
import com.oying.domain.dto.BucketStorageQueryCriteria;
import com.oying.mapper.BucketStorageMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.io.File;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
 
import org.springframework.web.multipart.MultipartFile;
 
/**
 * @author lixin
 * @description 服务实现
 * @date 2025-06-03
 **/
@Service
@RequiredArgsConstructor
public class BucketStorageServiceImpl extends ServiceImpl<BucketStorageMapper, BucketStorage> implements BucketStorageService {
 
    private final BucketStorageMapper bucketStorageMapper;
    private final ObsProperties properties;
    private final FileProperties fileProperties;
 
    @Override
    public PageResult<BucketStorage> queryAll(BucketStorageQueryCriteria criteria, Page<Object> page) {
        return PageUtil.toPage(bucketStorageMapper.findAll(criteria, page));
    }
 
    @Override
    public List<BucketStorage> queryAll(BucketStorageQueryCriteria criteria) {
        return bucketStorageMapper.findAll(criteria);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BucketStorage createFile(String name, File file) {
        FileUtil.checkSize(fileProperties.getMaxSize(), file.length());
        String suffix = FileUtil.getExtensionName(file.getName());
        String type = FileUtil.getFileType(suffix);
        String reaName = System.currentTimeMillis() + "." + suffix;
        String objectKey = type + "/" + reaName;
        PutObjectResult result = ObsUtils.putObject(properties, file, objectKey, AccessControlList.REST_CANNED_PUBLIC_READ);
        if (ObjectUtil.isNull(result)) {
            throw new BadRequestException("上传失败");
        }
        name = StringUtils.isBlank(name) ? FileUtil.getFileNameNoEx(reaName) : name;
        BucketStorage bucketStorage = new BucketStorage(
                reaName,
                name,
                suffix,
                objectKey,
                type,
                FileUtil.getSize(file.length())
        );
        bucketStorageMapper.insert(bucketStorage);
        return bucketStorage;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BucketStorage create(String name, MultipartFile file) {
        FileUtil.checkSize(fileProperties.getMaxSize(), file.getSize());
        String suffix = FileUtil.getExtensionName(file.getOriginalFilename());
        String type = FileUtil.getFileType(suffix);
        String reaName = System.currentTimeMillis() + "." + suffix;
        String objectKey = type + "/" + reaName;
        PutObjectResult result = ObsUtils.putObject(properties, FileUtil.toFile(file), objectKey, AccessControlList.REST_CANNED_PUBLIC_READ);
        if (ObjectUtil.isNull(result)) {
            throw new BadRequestException("上传失败");
        }
        name = StringUtils.isBlank(name) ? FileUtil.getFileNameNoEx(reaName) : name;
        BucketStorage bucketStorage = new BucketStorage(
                reaName,
                name,
                suffix,
                objectKey,
                type,
                FileUtil.getSize(file.getSize())
        );
        bucketStorageMapper.insert(bucketStorage);
        return bucketStorage;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        for(Long id : ids) {
            BucketStorage storage = bucketStorageMapper.selectById(id);
            ObsUtils.deleteObject(properties, storage.getPath());
        }
        bucketStorageMapper.deleteBatchIds(ids);
    }
 
    @Override
    public void download(List<BucketStorage> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (BucketStorage bucketStorage : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("文件真实的名称", bucketStorage.getRealName());
            map.put("文件名", bucketStorage.getName());
            map.put("后缀", bucketStorage.getSuffix());
            map.put("路径", bucketStorage.getPath());
            map.put("类型", bucketStorage.getType());
            map.put("大小", bucketStorage.getSize());
            map.put("创建人", bucketStorage.getCreateBy());
            map.put("创建时间", bucketStorage.getCreateTime());
            map.put("修改者", bucketStorage.getUpdateBy());
            map.put("修改时间", bucketStorage.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}