| | |
| | | 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.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 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); |
| | | } |
| | | |