| | |
| | | package com.oying.service.impl; |
| | | |
| | | import cn.hutool.core.util.IdUtil; |
| | | 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.config.properties.FileProperties; |
| | | import com.oying.domain.BucketStorage; |
| | | import com.oying.utils.FileUtil; |
| | | 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.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 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 |
| | | **/ |
| | | * @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 |
| | | private final ObsProperties properties; |
| | | private final FileProperties fileProperties; |
| | | |
| | | @Override |
| | | public PageResult<BucketStorage> queryAll(BucketStorageQueryCriteria criteria, Page<Object> page){ |
| | | public PageResult<BucketStorage> queryAll(BucketStorageQueryCriteria criteria, Page<Object> page) { |
| | | return PageUtil.toPage(bucketStorageMapper.findAll(criteria, page)); |
| | | } |
| | | |
| | | @Override |
| | | public List<BucketStorage> queryAll(BucketStorageQueryCriteria criteria){ |
| | | 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()); |
| | | 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(config, file, objectKey, AccessControlList.REST_CANNED_PUBLIC_READ); |
| | | PutObjectResult result = ObsUtils.putObject(properties, file, objectKey, AccessControlList.REST_CANNED_PUBLIC_READ); |
| | | if (ObjectUtil.isNull(result)) { |
| | | throw new BadRequestException("上传失败"); |
| | | } |
| | |
| | | type, |
| | | FileUtil.getSize(file.length()) |
| | | ); |
| | | return bucketStorageRepository.save(bucketStorage); |
| | | bucketStorageMapper.insert(bucketStorage); |
| | | return bucketStorage; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public BucketStorage create(String name, MultipartFile file) { |
| | | FileUtil.checkSize(properties.getMaxSize(), file.getSize()); |
| | | FileUtil.checkSize(fileProperties.getMaxSize(), file.getSize()); |
| | | String suffix = FileUtil.getExtensionName(file.getOriginalFilename()); |
| | | String type = FileUtil.getFileType(suffix); |
| | | String reaName = System.currentTimeMillis() + "." + suffix; |
| | | String reaName = IdUtil.getSnowflake(1, 1).nextId() + "." + suffix; |
| | | String objectKey = type + "/" + reaName; |
| | | PutObjectResult result = ObsUtils.putObject(config, FileUtil.toFile(file), objectKey,AccessControlList.REST_CANNED_PUBLIC_READ); |
| | | PutObjectResult result = ObsUtils.putObject(properties, FileUtil.toFile(file), objectKey, AccessControlList.REST_CANNED_PUBLIC_READ); |
| | | if (ObjectUtil.isNull(result)) { |
| | | throw new BadRequestException("上传失败"); |
| | | } |
| | |
| | | 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); |
| | | 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 |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void deleteById(String path) { |
| | | BucketStorage storage = bucketStorageMapper.findByPath(path); |
| | | ObsUtils.deleteObject(properties, storage.getPath()); |
| | | bucketStorageMapper.deleteById(storage.getBucketId()); |
| | | } |
| | | |
| | | @Override |
| | |
| | | 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.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); |