xin
2025-06-03 9fbd156fa0eb3091dd033c3eebf5fbc53c6ab6de
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
package com.oying.service.impl;
 
import cn.hutool.core.util.ObjectUtil;
import com.obs.services.internal.ObsProperties;
import com.obs.services.model.AccessControlList;
import com.obs.services.model.PutObjectResult;
import com.oying.domain.BucketStorage;
import com.oying.utils.FileUtil;
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 com.oying.utils.PageUtil;
 
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 com.oying.utils.PageResult;
import org.springframework.web.multipart.MultipartFile;
 
/**
* @description 服务实现
* @author lixin
* @date 2025-06-03
**/
@Service
@RequiredArgsConstructor
public class BucketStorageServiceImpl extends ServiceImpl<BucketStorageMapper, BucketStorage> implements BucketStorageService {
 
    private final BucketStorageMapper bucketStorageMapper;
    private final ObsProperties
 
    @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) {
        ObsConfig config = obsConfigService.findById(1L);
        FileUtil.checkSize(properties.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(config, 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())
        );
        return bucketStorageRepository.save(bucketStorage);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BucketStorage create(String name, MultipartFile file) {
        FileUtil.checkSize(properties.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(config, 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())
        );
        return bucketStorageRepository.save(bucketStorage);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(BucketStorage resources) {
        BucketStorage bucketStorage = getById(resources.getBucketId());
        bucketStorage.copy(resources);
        bucketStorageMapper.updateById(bucketStorage);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        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);
    }
}