| ¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.basic.utils; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.ruoyi.basic.dto.StorageAttachmentDTO; |
| | | import com.ruoyi.basic.dto.StorageAttachmentVO; |
| | | import com.ruoyi.basic.dto.StorageBlobDTO; |
| | | import com.ruoyi.basic.dto.StorageBlobVO; |
| | | import com.ruoyi.basic.enums.ApplicationTypeEnum; |
| | | import com.ruoyi.basic.enums.RecordTypeEnum; |
| | | import com.ruoyi.basic.mapper.StorageAttachmentMapper; |
| | | import com.ruoyi.basic.mapper.StorageBlobMapper; |
| | | import com.ruoyi.basic.pojo.StorageAttachment; |
| | | import com.ruoyi.basic.pojo.StorageBlob; |
| | | import com.ruoyi.common.config.FileProperties; |
| | | import io.jsonwebtoken.Jwts; |
| | | import io.jsonwebtoken.security.Keys; |
| | | import lombok.RequiredArgsConstructor; |
| | | import net.coobird.thumbnailator.Thumbnails; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.data.redis.core.StringRedisTemplate; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.StringUtils; |
| | | |
| | | import javax.crypto.SecretKey; |
| | | import java.io.File; |
| | | import java.math.BigDecimal; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.time.LocalDate; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.*; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Component |
| | | @RequiredArgsConstructor |
| | | public class FileUtil { |
| | | private final FileProperties properties; |
| | | private final StorageAttachmentMapper storageAttachmentMapper; |
| | | private final StringRedisTemplate stringRedisTemplate; |
| | | private final StorageBlobMapper storageBlobMapper; |
| | | |
| | | private static final String TOKEN_USAGE_KEY_PREFIX = "file:token:usage:"; |
| | | private static final DateTimeFormatter YEAR_PATH_FORMATTER = DateTimeFormatter.ofPattern("yyyy"); |
| | | private static final DateTimeFormatter MONTH_DAY_PATH_FORMATTER = DateTimeFormatter.ofPattern("MMdd"); |
| | | |
| | | /** |
| | | * ä¿åéä»¶ä¿¡æ¯ |
| | | * |
| | | * @param application æä»¶ç¨é |
| | | * @param recordType å
³èè®°å½ç±»å |
| | | * @param recordId å
³èè®°å½id |
| | | * @param storageBlobDTOS æä»¶ä¿¡æ¯ |
| | | */ |
| | | public void saveStorageAttachment(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId, List<StorageBlobDTO> storageBlobDTOS) { |
| | | if (application == null) { |
| | | throw new RuntimeException("æä»¶ç¨éä¸è½ä¸ºç©º"); |
| | | } |
| | | if (recordType == null) { |
| | | throw new RuntimeException("å
³èè®°å½ç±»åä¸è½ä¸ºç©º"); |
| | | } |
| | | if (recordId == null || recordId <= 0) { |
| | | throw new RuntimeException("å
³èè®°å½idä¸è½ä¸ºç©º"); |
| | | } |
| | | // å 餿§éä»¶ä¿¡æ¯ |
| | | deleteStorageAttachmentsByApplicationAndRecordTypeAndRecordId(application, recordType, recordId); |
| | | if (CollectionUtils.isEmpty(storageBlobDTOS)) { |
| | | return; |
| | | } |
| | | List<StorageAttachment> storageAttachments = new ArrayList<>(); |
| | | for (StorageBlobDTO storageBlobDTO : storageBlobDTOS) { |
| | | StorageAttachment storageAttachment = new StorageAttachment(); |
| | | storageAttachment.setApplication(application.getType()); |
| | | storageAttachment.setRecordType(recordType.getType()); |
| | | storageAttachment.setRecordId(recordId); |
| | | storageAttachment.setStorageBlobId(storageBlobDTO.getId()); |
| | | storageAttachment.setDeleted(0L); |
| | | storageAttachments.add(storageAttachment); |
| | | } |
| | | storageAttachmentMapper.insert(storageAttachments); |
| | | } |
| | | |
| | | /** |
| | | * ä¿åéä»¶ä¿¡æ¯ |
| | | * |
| | | * @param recordType å
³èè®°å½ç±»å |
| | | * @param recordId å
³èè®°å½id |
| | | * @param storageBlobDTOS æä»¶ä¿¡æ¯ |
| | | */ |
| | | public void saveStorageAttachmentByRecordTypeAndRecordId(String application,RecordTypeEnum recordType, Long recordId, List<StorageBlobDTO> storageBlobDTOS) { |
| | | if (recordType == null) { |
| | | throw new RuntimeException("å
³èè®°å½ç±»åä¸è½ä¸ºç©º"); |
| | | } |
| | | if (recordId == null) { |
| | | throw new RuntimeException("å
³èè®°å½idä¸è½ä¸ºç©º"); |
| | | } |
| | | |
| | | // å 餿§éä»¶ä¿¡æ¯ |
| | | if (application == null) { |
| | | for (StorageBlobDTO storageBlobDTO : storageBlobDTOS) { |
| | | deleteStorageAttachmentsByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum.getByType(storageBlobDTO.getApplication()), recordType, recordId); |
| | | } |
| | | } else { |
| | | deleteStorageAttachmentsByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum.getByType(application), recordType, recordId); |
| | | } |
| | | |
| | | if (CollectionUtils.isEmpty(storageBlobDTOS)) { |
| | | deleteStorageAttachmentsByRecordTypeAndRecordId(recordType, recordId); |
| | | } |
| | | |
| | | List<StorageAttachment> storageAttachments = new ArrayList<>(); |
| | | for (StorageBlobDTO storageBlobDTO : storageBlobDTOS) { |
| | | StorageAttachment storageAttachment = new StorageAttachment(); |
| | | storageAttachment.setApplication(Objects.requireNonNullElseGet(application, () -> ApplicationTypeEnum.getByType(storageBlobDTO.getApplication()).getType())); |
| | | storageAttachment.setRecordType(recordType.getType()); |
| | | storageAttachment.setRecordId(recordId); |
| | | storageAttachment.setStorageBlobId(storageBlobDTO.getId()); |
| | | storageAttachment.setDeleted(0L); |
| | | storageAttachments.add(storageAttachment); |
| | | } |
| | | storageAttachmentMapper.insert(storageAttachments); |
| | | } |
| | | |
| | | /** |
| | | * å é¤æä»¶ä¿¡æ¯ |
| | | * |
| | | * @param storageBlobIds æä»¶id |
| | | */ |
| | | public void deleteStorageBlobs(List<Long> storageBlobIds) { |
| | | storageBlobMapper.deleteByIds(storageBlobIds); |
| | | } |
| | | |
| | | /** |
| | | * éè¿æä»¶å
³èidå é¤æä»¶ä¿¡æ¯ |
| | | * |
| | | * @param storageAttachmentIds æä»¶id |
| | | */ |
| | | public void deleteStorageBlobsByStorageAttachmentIds(List<Long> storageAttachmentIds) { |
| | | List<StorageAttachment> storageAttachments = storageAttachmentMapper.selectByIds(storageAttachmentIds); |
| | | List<Long> storageBlobIds = storageAttachments.stream().map(StorageAttachment::getStorageBlobId).collect(Collectors.toList()); |
| | | deleteStorageBlobs(storageBlobIds); |
| | | } |
| | | |
| | | /** |
| | | * éè¿æä»¶ç¨éãå
³èè®°å½ç±»åãå
³èè®°å½idå é¤æä»¶ä¿¡æ¯ |
| | | * |
| | | * @param application æä»¶ç¨é |
| | | * @param recordType å
³èè®°å½ç±»å |
| | | * @param recordIds å
³èè®°å½id |
| | | */ |
| | | public void deleteStorageBlobsByApplicationAndRecordTypeAndRecordIds(ApplicationTypeEnum application, RecordTypeEnum recordType, List<Long> recordIds) { |
| | | if (recordIds == null || recordIds.isEmpty()) { |
| | | throw new RuntimeException("å
³èè®°å½idä¸è½ä¸ºç©º"); |
| | | } |
| | | if (application == null) { |
| | | throw new RuntimeException("æä»¶ç¨éä¸è½ä¸ºç©º"); |
| | | } |
| | | if (recordType == null) { |
| | | throw new RuntimeException("å
³èè®°å½ç±»åä¸è½ä¸ºç©º"); |
| | | } |
| | | List<StorageAttachment> storageAttachments = storageAttachmentMapper.selectList(new LambdaQueryWrapper<StorageAttachment>() |
| | | .eq(StorageAttachment::getRecordType, recordType.getType()) |
| | | .in(StorageAttachment::getRecordId, recordIds) |
| | | .eq(StorageAttachment::getApplication, application.getType())); |
| | | if (CollectionUtils.isNotEmpty(storageAttachments)) { |
| | | List<Long> storageAttachmentIds = storageAttachments.stream().map(StorageAttachment::getStorageBlobId) |
| | | .collect(Collectors.toList()); |
| | | deleteStorageBlobsByStorageAttachmentIds(storageAttachmentIds); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * éè¿å
³èè®°å½ç±»åãå
³èè®°å½idå é¤æä»¶ä¿¡æ¯ |
| | | * |
| | | * @param recordType å
³èè®°å½ç±»å |
| | | * @param recordId å
³èè®°å½id |
| | | */ |
| | | public void deleteStorageBlobsByRecordTypeAndRecordId(RecordTypeEnum recordType, Long recordId) { |
| | | if (recordId == null || recordId <= 0) { |
| | | throw new RuntimeException("å
³èè®°å½idä¸è½ä¸ºç©º"); |
| | | } |
| | | if (recordType == null) { |
| | | throw new RuntimeException("å
³èè®°å½ç±»åä¸è½ä¸ºç©º"); |
| | | } |
| | | List<StorageAttachment> storageAttachments = storageAttachmentMapper.selectList(new LambdaQueryWrapper<StorageAttachment>() |
| | | .eq(StorageAttachment::getRecordType, recordType.getType()) |
| | | .eq(StorageAttachment::getRecordId, recordId)); |
| | | if (CollectionUtils.isNotEmpty(storageAttachments)) { |
| | | List<Long> storageAttachmentIds = storageAttachments.stream().map(StorageAttachment::getStorageBlobId) |
| | | .collect(Collectors.toList()); |
| | | deleteStorageBlobsByStorageAttachmentIds(storageAttachmentIds); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å 餿件å
³èä¿¡æ¯ |
| | | * |
| | | * @param storageAttachmentIds æä»¶å
³èid |
| | | */ |
| | | public void deleteStorageAttachmentsByStorageAttachmentIds(List<Long> storageAttachmentIds) { |
| | | deleteStorageBlobsByStorageAttachmentIds(storageAttachmentIds); |
| | | storageAttachmentMapper.deleteByIds(storageAttachmentIds); |
| | | } |
| | | |
| | | /** |
| | | * å 餿件å
³èä¿¡æ¯ |
| | | * |
| | | * @param application æä»¶ç¨é |
| | | * @param recordType å
³èè®°å½ç±»å |
| | | * @param recordId å
³èè®°å½id |
| | | */ |
| | | public void deleteStorageAttachmentsByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId) { |
| | | if (recordId == null || recordId <= 0) { |
| | | throw new RuntimeException("å
³èè®°å½idä¸è½ä¸ºç©º"); |
| | | } |
| | | if (application == null) { |
| | | throw new RuntimeException("æä»¶ç¨éä¸è½ä¸ºç©º"); |
| | | } |
| | | if (recordType == null) { |
| | | throw new RuntimeException("å
³èè®°å½ç±»åä¸è½ä¸ºç©º"); |
| | | } |
| | | deleteStorageBlobsByApplicationAndRecordTypeAndRecordIds(application, recordType, Arrays.asList(recordId)); |
| | | storageAttachmentMapper.delete(new LambdaQueryWrapper<StorageAttachment>() |
| | | .eq(StorageAttachment::getRecordType, recordType.getType()) |
| | | .eq(StorageAttachment::getRecordId, recordId) |
| | | .eq(StorageAttachment::getApplication, application.getType())); |
| | | } |
| | | |
| | | /** |
| | | * å 餿件å
³èä¿¡æ¯ |
| | | * |
| | | * @param recordType å
³èè®°å½ç±»å |
| | | * @param recordId å
³èè®°å½id |
| | | */ |
| | | public void deleteStorageAttachmentsByRecordTypeAndRecordId(RecordTypeEnum recordType, Long recordId) { |
| | | if (recordId == null || recordId <= 0) { |
| | | throw new RuntimeException("å
³èè®°å½idä¸è½ä¸ºç©º"); |
| | | } |
| | | if (recordType == null) { |
| | | throw new RuntimeException("å
³èè®°å½ç±»åä¸è½ä¸ºç©º"); |
| | | } |
| | | deleteStorageBlobsByRecordTypeAndRecordId(recordType, recordId); |
| | | storageAttachmentMapper.delete(new LambdaQueryWrapper<StorageAttachment>() |
| | | .eq(StorageAttachment::getRecordType, recordType.getType()) |
| | | .eq(StorageAttachment::getRecordId, recordId)); |
| | | } |
| | | |
| | | /** |
| | | * æ¹éå 餿件å
³èä¿¡æ¯ attachment |
| | | * |
| | | * @param application æä»¶ç¨é |
| | | * @param recordType å
³èè®°å½ç±»å |
| | | * @param recordIds å
³èè®°å½id |
| | | */ |
| | | public void deleteStorageAttachmentsByApplicationAndRecordTypeAndRecordIds(ApplicationTypeEnum application, RecordTypeEnum recordType, List<Long> recordIds) { |
| | | if (recordIds == null || recordIds.isEmpty()) { |
| | | throw new RuntimeException("å
³èè®°å½idä¸è½ä¸ºç©º"); |
| | | } |
| | | if (application == null) { |
| | | throw new RuntimeException("æä»¶ç¨éä¸è½ä¸ºç©º"); |
| | | } |
| | | if (recordType == null) { |
| | | throw new RuntimeException("å
³èè®°å½ç±»åä¸è½ä¸ºç©º"); |
| | | } |
| | | deleteStorageBlobsByApplicationAndRecordTypeAndRecordIds(application, recordType, recordIds); |
| | | storageAttachmentMapper.delete(new LambdaQueryWrapper<StorageAttachment>() |
| | | .eq(StorageAttachment::getRecordType, recordType.getType()) |
| | | .in(StorageAttachment::getRecordId, recordIds) |
| | | .eq(StorageAttachment::getApplication, application.getType())); |
| | | } |
| | | |
| | | /** |
| | | * éè¿æä»¶å
³èidè·åæä»¶ä¿¡æ¯ attachment |
| | | * |
| | | * @param storageAttachmentIds æä»¶id |
| | | */ |
| | | public List<StorageAttachment> getStorageAttachmentsByStorageAttachmentIds(List<Long> storageAttachmentIds) { |
| | | if (CollectionUtils.isEmpty(storageAttachmentIds)) { |
| | | throw new RuntimeException("æä»¶idä¸è½ä¸ºç©º"); |
| | | } |
| | | return storageAttachmentMapper.selectByIds(storageAttachmentIds); |
| | | } |
| | | |
| | | /** |
| | | * éè¿è®°å½ç±»åè·åæä»¶ä¿¡æ¯ attachmentï¼åé¡µï¼ |
| | | * |
| | | * @param storageAttachmentDTO å
³èè®°å½ä¿¡æ¯ |
| | | */ |
| | | public List<StorageBlobVO> getStorageBlobVOsByApplicationAndRecordTypeAndRecordId(StorageAttachmentDTO storageAttachmentDTO) { |
| | | LambdaQueryWrapper<StorageAttachment> queryWrapper = new LambdaQueryWrapper<StorageAttachment>() |
| | | .eq(StorageAttachment::getRecordType, storageAttachmentDTO.getRecordType()) |
| | | .eq(StorageAttachment::getRecordId, storageAttachmentDTO.getRecordId()); |
| | | if (storageAttachmentDTO.getApplication() != null) { |
| | | queryWrapper.eq(StorageAttachment::getApplication, storageAttachmentDTO.getApplication()); |
| | | } |
| | | List<StorageAttachment> storageAttachments = storageAttachmentMapper.selectList(queryWrapper); |
| | | if (CollectionUtils.isEmpty(storageAttachments)) { |
| | | return null; |
| | | } |
| | | return getStorageBlobVOsByStorageAttachmentIds(storageAttachments.stream().map(StorageAttachment::getId).collect(Collectors.toList())); |
| | | } |
| | | |
| | | /** |
| | | * éè¿æä»¶ç¨éãå
³èè®°å½ç±»åãå
³èè®°å½idè·åæä»¶å
³èä¿¡æ¯ attachment |
| | | * |
| | | * @param application æä»¶ç¨é |
| | | * @param recordType å
³èè®°å½ç±»å |
| | | * @param recordId å
³èè®°å½id |
| | | */ |
| | | public List<StorageAttachment> getStorageAttachmentsByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId) { |
| | | if (recordId == null || recordId <= 0) { |
| | | throw new RuntimeException("å
³èè®°å½idä¸è½ä¸ºç©º"); |
| | | } if (application == null) { |
| | | throw new RuntimeException("æä»¶ç¨éä¸è½ä¸ºç©º"); |
| | | } |
| | | if (recordType == null) { |
| | | throw new RuntimeException("å
³èè®°å½ç±»åä¸è½ä¸ºç©º"); |
| | | } |
| | | return storageAttachmentMapper.selectList(new LambdaQueryWrapper<StorageAttachment>() |
| | | .eq(StorageAttachment::getRecordType, recordType.getType()) |
| | | .eq(StorageAttachment::getRecordId, recordId) |
| | | .eq(StorageAttachment::getApplication, application.getType())); |
| | | } |
| | | |
| | | /** |
| | | * éè¿æä»¶å
³èidè·åæä»¶ä¿¡æ¯ blob |
| | | * |
| | | * @param storageAttachmentIds æä»¶id |
| | | */ |
| | | public List<StorageBlobVO> getStorageBlobVOsByStorageAttachmentIds(List<Long> storageAttachmentIds) { |
| | | List<StorageAttachment> storageAttachments = getStorageAttachmentsByStorageAttachmentIds(storageAttachmentIds); |
| | | if (CollectionUtils.isEmpty(storageAttachments)) { |
| | | return null; |
| | | } |
| | | Map<Long, Long> blobIdToAttachmentIdMap = storageAttachments.stream() |
| | | .collect(Collectors.toMap(StorageAttachment::getStorageBlobId, StorageAttachment::getId)); |
| | | |
| | | List<Long> storageBlobIds = storageAttachments.stream().map(StorageAttachment::getStorageBlobId).collect(Collectors.toList()); |
| | | List<StorageBlob> storageBlobs = storageBlobMapper.selectByIds(storageBlobIds); |
| | | List<StorageBlobVO> storageBlobDTOS = new ArrayList<>(); |
| | | for (StorageBlob storageBlob : storageBlobs) { |
| | | StorageBlobVO storageBlobVO = new StorageBlobVO(); |
| | | BeanUtils.copyProperties(storageBlob, storageBlobVO); |
| | | storageBlobVO.setPreviewURL(buildSignedPreviewUrl(storageBlobVO)); |
| | | storageBlobVO.setDownloadURL(buildSignedDownloadUrl(storageBlobVO)); |
| | | storageBlobVO.setStorageAttachmentId(blobIdToAttachmentIdMap.get(storageBlob.getId())); |
| | | storageBlobDTOS.add(storageBlobVO); |
| | | } |
| | | return storageBlobDTOS; |
| | | } |
| | | |
| | | /** |
| | | * éè¿æä»¶ç¨éãå
³èè®°å½ç±»åãå
³èè®°å½idè·åæä»¶å
³èä¿¡æ¯ attachment |
| | | * |
| | | * @param recordType å
³èè®°å½ç±»å |
| | | * @param recordId å
³èè®°å½id |
| | | */ |
| | | public List<StorageAttachment> getStorageAttachmentsByRecordTypeAndRecordId(RecordTypeEnum recordType, Long recordId) { |
| | | if (recordId == null || recordId <= 0) { |
| | | throw new RuntimeException("å
³èè®°å½idä¸è½ä¸ºç©º"); |
| | | } |
| | | if (recordType == null) { |
| | | throw new RuntimeException("å
³èè®°å½ç±»åä¸è½ä¸ºç©º"); |
| | | } |
| | | return storageAttachmentMapper.selectList(new LambdaQueryWrapper<StorageAttachment>() |
| | | .eq(StorageAttachment::getRecordType, recordType.getType()) |
| | | .eq(StorageAttachment::getRecordId, recordId)); |
| | | } |
| | | |
| | | /** |
| | | * éè¿æä»¶ç¨éãå
³èè®°å½ç±»åãå
³èè®°å½idè·åæä»¶ä¿¡æ¯ blob |
| | | * |
| | | * @param application æä»¶ç¨é |
| | | * @param recordType å
³èè®°å½ç±»å |
| | | * @param recordId å
³èè®°å½id |
| | | */ |
| | | public List<StorageBlobVO> getStorageBlobVOsByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId) { |
| | | List<StorageAttachment> storageAttachments = getStorageAttachmentsByApplicationAndRecordTypeAndRecordId(application, recordType, recordId); |
| | | if (CollectionUtils.isEmpty(storageAttachments)) { |
| | | return null; |
| | | } |
| | | // æå»º storageBlobId -> storageAttachmentId çæ å° |
| | | Map<Long, Long> blobIdToAttachmentIdMap = storageAttachments.stream() |
| | | .collect(Collectors.toMap(StorageAttachment::getStorageBlobId, StorageAttachment::getId)); |
| | | |
| | | List<Long> storageBlobIds = storageAttachments.stream().map(StorageAttachment::getStorageBlobId).collect(Collectors.toList()); |
| | | List<StorageBlob> storageBlobs = storageBlobMapper.selectByIds(storageBlobIds); |
| | | List<StorageBlobVO> storageBlobDTOS = new ArrayList<>(); |
| | | for (StorageBlob storageBlob : storageBlobs) { |
| | | StorageBlobVO storageBlobVO = new StorageBlobVO(); |
| | | BeanUtils.copyProperties(storageBlob, storageBlobVO); |
| | | storageBlobVO.setPreviewURL(buildSignedPreviewUrl(storageBlobVO)); |
| | | storageBlobVO.setDownloadURL(buildSignedDownloadUrl(storageBlobVO)); |
| | | storageBlobVO.setStorageAttachmentId(blobIdToAttachmentIdMap.get(storageBlob.getId())); |
| | | storageBlobDTOS.add(storageBlobVO); |
| | | } |
| | | return storageBlobDTOS; |
| | | } |
| | | |
| | | /** |
| | | * éè¿æä»¶ç¨éãå
³èè®°å½ç±»åãå
³èè®°å½idè·åæä»¶ä¿¡æ¯ blob |
| | | * |
| | | * @param recordType å
³èè®°å½ç±»å |
| | | * @param recordId å
³èè®°å½id |
| | | */ |
| | | public List<StorageBlobVO> getStorageBlobVOsByRecordTypeAndRecordId(RecordTypeEnum recordType, Long recordId) { |
| | | List<StorageAttachment> storageAttachments = getStorageAttachmentsByRecordTypeAndRecordId(recordType, recordId); |
| | | if (CollectionUtils.isEmpty(storageAttachments)) { |
| | | return null; |
| | | } |
| | | List<Long> storageBlobIds = storageAttachments.stream().map(StorageAttachment::getStorageBlobId).collect(Collectors.toList()); |
| | | List<StorageBlob> storageBlobs = storageBlobMapper.selectByIds(storageBlobIds); |
| | | List<StorageBlobVO> storageBlobDTOS = new ArrayList<>(); |
| | | for (StorageBlob storageBlob : storageBlobs) { |
| | | StorageBlobVO storageBlobVO = new StorageBlobVO(); |
| | | BeanUtils.copyProperties(storageBlob, storageBlobVO); |
| | | storageBlobVO.setPreviewURL(buildSignedPreviewUrl(storageBlobVO)); |
| | | storageBlobVO.setDownloadURL(buildSignedDownloadUrl(storageBlobVO)); |
| | | storageBlobDTOS.add(storageBlobVO); |
| | | } |
| | | return storageBlobDTOS; |
| | | } |
| | | |
| | | /** |
| | | * éè¿æä»¶ç¨éãå
³èè®°å½ç±»åãå
³èè®°å½idè·åæä»¶ä¿¡æ¯ èªå®ä¹è¿ææ¶é´ï¼åéï¼ blob |
| | | * |
| | | * @param application æä»¶ç¨é |
| | | * @param recordType å
³èè®°å½ç±»å |
| | | * @param recordId å
³èè®°å½id |
| | | * @param expired è¿ææ¶é´ |
| | | */ |
| | | public List<StorageBlobVO> getStorageBlobVOsByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId, BigDecimal expired) { |
| | | List<StorageAttachment> storageAttachments = getStorageAttachmentsByApplicationAndRecordTypeAndRecordId(application, recordType, recordId); |
| | | if (CollectionUtils.isEmpty(storageAttachments)) { |
| | | return null; |
| | | } |
| | | // æå»º storageBlobId -> storageAttachmentId çæ å° |
| | | Map<Long, Long> blobIdToAttachmentIdMap = storageAttachments.stream() |
| | | .collect(Collectors.toMap(StorageAttachment::getStorageBlobId, StorageAttachment::getId)); |
| | | |
| | | List<Long> storageBlobIds = storageAttachments.stream().map(StorageAttachment::getStorageBlobId).collect(Collectors.toList()); |
| | | List<StorageBlob> storageBlobs = storageBlobMapper.selectByIds(storageBlobIds); |
| | | List<StorageBlobVO> storageBlobDTOS = new ArrayList<>(); |
| | | for (StorageBlob storageBlob : storageBlobs) { |
| | | StorageBlobVO storageBlobVO = new StorageBlobVO(); |
| | | BeanUtils.copyProperties(storageBlob, storageBlobVO); |
| | | storageBlobVO.setPreviewURL(buildSignedUrl(storageBlobVO, "/preview/", expired)); |
| | | storageBlobVO.setDownloadURL(buildSignedUrl(storageBlobVO, "/download/", expired)); |
| | | storageBlobVO.setStorageAttachmentId(blobIdToAttachmentIdMap.get(storageBlob.getId())); |
| | | storageBlobDTOS.add(storageBlobVO); |
| | | } |
| | | return storageBlobDTOS; |
| | | } |
| | | |
| | | /** |
| | | * éè¿æä»¶å
³èidè·åæä»¶ä¿¡æ¯åå¨è¿ææ¶é´ èªå®ä¹è¿ææ¶é´ï¼åéï¼ blob |
| | | * |
| | | * @param storageAttachmentIds æä»¶id |
| | | * @param expired è¿ææ¶é´ |
| | | */ |
| | | public List<StorageBlobVO> getStorageBlobVOsByStorageAttachmentIds(List<Long> storageAttachmentIds, BigDecimal expired) { |
| | | List<StorageAttachment> storageAttachments = getStorageAttachmentsByStorageAttachmentIds(storageAttachmentIds); |
| | | if (CollectionUtils.isEmpty(storageAttachments)) { |
| | | return null; |
| | | } |
| | | List<Long> storageBlobIds = storageAttachments.stream().map(StorageAttachment::getStorageBlobId).collect(Collectors.toList()); |
| | | List<StorageBlob> storageBlobs = storageBlobMapper.selectByIds(storageBlobIds); |
| | | List<StorageBlobVO> storageBlobDTOS = new ArrayList<>(); |
| | | for (StorageBlob storageBlob : storageBlobs) { |
| | | StorageBlobVO storageBlobVO = new StorageBlobVO(); |
| | | BeanUtils.copyProperties(storageBlob, storageBlobVO); |
| | | storageBlobVO.setPreviewURL(buildSignedUrl(storageBlobVO, "/preview/", expired)); |
| | | storageBlobVO.setDownloadURL(buildSignedUrl(storageBlobVO, "/download/", expired)); |
| | | storageBlobDTOS.add(storageBlobVO); |
| | | } |
| | | return storageBlobDTOS; |
| | | } |
| | | |
| | | /** |
| | | * éè¿æä»¶å
³èidè·åæä»¶ä¿¡æ¯ attachment |
| | | * |
| | | * @param storageAttachmentIds æä»¶id |
| | | */ |
| | | public List<StorageAttachmentVO> getStorageAttachmentVOSByStorageAttachmentIds(List<Long> storageAttachmentIds) { |
| | | List<StorageAttachment> storageAttachments = getStorageAttachmentsByStorageAttachmentIds(storageAttachmentIds); |
| | | if (CollectionUtils.isEmpty(storageAttachments)) { |
| | | return new ArrayList<>(); |
| | | } |
| | | List<StorageAttachmentVO> storageAttachmentVOS = new ArrayList<>(); |
| | | for (StorageAttachment storageAttachment : storageAttachments) { |
| | | StorageAttachmentVO storageAttachmentVO = new StorageAttachmentVO(); |
| | | BeanUtils.copyProperties(storageAttachment, storageAttachmentVO); |
| | | List<StorageBlobVO> storageBlobVOS = getStorageBlobVOsByStorageAttachmentIds(Collections.singletonList(storageAttachment.getId())); |
| | | if (CollectionUtils.isEmpty(storageBlobVOS)) { |
| | | storageAttachmentVO.setStorageBlobVOS(new ArrayList<>()); |
| | | } else { |
| | | storageAttachmentVO.setStorageBlobVOS(storageBlobVOS); |
| | | } |
| | | storageAttachmentVOS.add(storageAttachmentVO); |
| | | } |
| | | return storageAttachmentVOS; |
| | | } |
| | | |
| | | /** |
| | | * éè¿æä»¶å
³èidè·åæä»¶ä¿¡æ¯åå¨è¿ææ¶é´ èªå®ä¹è¿ææ¶é´ï¼åéï¼ attachment |
| | | * |
| | | * @param storageAttachmentIds æä»¶id |
| | | * @param expired è¿ææ¶é´ |
| | | */ |
| | | public List<StorageAttachmentVO> getStorageAttachmentVOSByStorageAttachmentIds(List<Long> storageAttachmentIds, BigDecimal expired) { |
| | | List<StorageAttachment> storageAttachments = getStorageAttachmentsByStorageAttachmentIds(storageAttachmentIds); |
| | | if (CollectionUtils.isEmpty(storageAttachments)) { |
| | | return new ArrayList<>(); |
| | | } |
| | | List<StorageAttachmentVO> storageAttachmentVOS = new ArrayList<>(); |
| | | for (StorageAttachment storageAttachment : storageAttachments) { |
| | | StorageAttachmentVO storageAttachmentVO = new StorageAttachmentVO(); |
| | | BeanUtils.copyProperties(storageAttachment, storageAttachmentVO); |
| | | List<StorageBlobVO> storageBlobVOS = getStorageBlobVOsByStorageAttachmentIds(Collections.singletonList(storageAttachment.getId()), expired); |
| | | if (CollectionUtils.isEmpty(storageBlobVOS)) { |
| | | storageAttachmentVO.setStorageBlobVOS(new ArrayList<>()); |
| | | } else { |
| | | storageAttachmentVO.setStorageBlobVOS(storageBlobVOS); |
| | | } |
| | | storageAttachmentVOS.add(storageAttachmentVO); |
| | | } |
| | | return storageAttachmentVOS; |
| | | } |
| | | |
| | | /** |
| | | * éè¿æä»¶å
³èidè·åæä»¶ä¿¡æ¯ attachment |
| | | * |
| | | * @param application åºç¨ |
| | | * @param recordType è®°å½ç±»å |
| | | * @param recordId è®°å½id |
| | | */ |
| | | public List<StorageAttachmentVO> getStorageAttachmentVOSByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId) { |
| | | List<StorageAttachment> storageAttachments = getStorageAttachmentsByApplicationAndRecordTypeAndRecordId(application, recordType, recordId); |
| | | if (CollectionUtils.isEmpty(storageAttachments)) { |
| | | return new ArrayList<>(); |
| | | } |
| | | List<StorageAttachmentVO> storageAttachmentVOS = new ArrayList<>(); |
| | | for (StorageAttachment storageAttachment : storageAttachments) { |
| | | StorageAttachmentVO storageAttachmentVO = new StorageAttachmentVO(); |
| | | BeanUtils.copyProperties(storageAttachment, storageAttachmentVO); |
| | | List<StorageBlobVO> storageBlobVOS = getStorageBlobVOsByStorageAttachmentIds(Collections.singletonList(storageAttachment.getId())); |
| | | if (CollectionUtils.isEmpty(storageBlobVOS)) { |
| | | storageAttachmentVO.setStorageBlobVOS(new ArrayList<>()); |
| | | } else { |
| | | storageAttachmentVO.setStorageBlobVOS(storageBlobVOS); |
| | | } |
| | | storageAttachmentVOS.add(storageAttachmentVO); |
| | | } |
| | | return storageAttachmentVOS; |
| | | } |
| | | |
| | | /** |
| | | * éè¿æä»¶å
³èidè·åæä»¶ä¿¡æ¯åå¨è¿ææ¶é´ èªå®ä¹è¿ææ¶é´ï¼åéï¼ attachment |
| | | * |
| | | * @param application åºç¨ |
| | | * @param recordType è®°å½ç±»å |
| | | * @param recordId è®°å½id |
| | | * @param expired è¿ææ¶é´ |
| | | */ |
| | | public List<StorageAttachmentVO> getStorageAttachmentVOSByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId, BigDecimal expired) { |
| | | List<StorageAttachment> storageAttachments = getStorageAttachmentsByApplicationAndRecordTypeAndRecordId(application, recordType, recordId); |
| | | if (CollectionUtils.isEmpty(storageAttachments)) { |
| | | return new ArrayList<>(); |
| | | } |
| | | List<StorageAttachmentVO> storageAttachmentVOS = new ArrayList<>(); |
| | | for (StorageAttachment storageAttachment : storageAttachments) { |
| | | StorageAttachmentVO storageAttachmentVO = new StorageAttachmentVO(); |
| | | BeanUtils.copyProperties(storageAttachment, storageAttachmentVO); |
| | | List<StorageBlobVO> storageBlobVOS = getStorageBlobVOsByStorageAttachmentIds(Collections.singletonList(storageAttachment.getId()), expired); |
| | | if (CollectionUtils.isEmpty(storageBlobVOS)) { |
| | | storageAttachmentVO.setStorageBlobVOS(new ArrayList<>()); |
| | | } else { |
| | | storageAttachmentVO.setStorageBlobVOS(storageBlobVOS); |
| | | } |
| | | storageAttachmentVOS.add(storageAttachmentVO); |
| | | } |
| | | return storageAttachmentVOS; |
| | | } |
| | | |
| | | /** |
| | | * éè¿æä»¶å
³èidè·åæä»¶é¢è§å°å |
| | | * |
| | | * @param storageAttachmentIds æä»¶å
³èid |
| | | */ |
| | | public List<String> getFilePreviewURLByStorageAttachmentIds(List<Long> storageAttachmentIds) { |
| | | List<String> res = new ArrayList<>(); |
| | | List<StorageBlobVO> storageBlobVOS = getStorageBlobVOsByStorageAttachmentIds(storageAttachmentIds); |
| | | for (StorageBlobVO storageBlobVO : storageBlobVOS) { |
| | | res.add(buildSignedPreviewUrl(storageBlobVO)); |
| | | } |
| | | return res; |
| | | } |
| | | |
| | | /** |
| | | * éè¿æä»¶å
³èidè·åæä»¶é¢è§å°ååå¨è¿ææ¶é´ èªå®ä¹è¿ææ¶é´ï¼åéï¼ |
| | | * |
| | | * @param storageAttachmentIds æä»¶å
³èid |
| | | * @param expired è¿ææ¶é´ |
| | | */ |
| | | public List<String> getFilePreviewURLByStorageAttachmentIds(List<Long> storageAttachmentIds, BigDecimal expired) { |
| | | List<String> res = new ArrayList<>(); |
| | | List<StorageBlobVO> storageBlobVOS = getStorageBlobVOsByStorageAttachmentIds(storageAttachmentIds); |
| | | for (StorageBlobVO storageBlobVO : storageBlobVOS) { |
| | | res.add(buildSignedUrl(storageBlobVO, "/preview/", expired)); |
| | | } |
| | | return res; |
| | | } |
| | | |
| | | /** |
| | | * éè¿æä»¶å
³èidè·åæä»¶ä¸è½½å°å |
| | | * |
| | | * @param storageAttachmentIds æä»¶å
³èid |
| | | */ |
| | | public List<String> getFileDownloadURLByStorageAttachmentIds(List<Long> storageAttachmentIds) { |
| | | List<String> res = new ArrayList<>(); |
| | | List<StorageBlobVO> storageBlobVOS = getStorageBlobVOsByStorageAttachmentIds(storageAttachmentIds); |
| | | for (StorageBlobVO storageBlobVO : storageBlobVOS) { |
| | | res.add(buildSignedDownloadUrl(storageBlobVO)); |
| | | } |
| | | return res; |
| | | } |
| | | |
| | | /** |
| | | * éè¿æä»¶å
³èidè·åæä»¶ä¸è½½å°ååå¨è¿ææ¶é´ èªå®ä¹è¿ææ¶é´ï¼åéï¼ |
| | | * |
| | | * @param storageAttachmentIds æä»¶å
³èid |
| | | * @param expired è¿ææ¶é´ |
| | | */ |
| | | public List<String> getFileDownloadURLByStorageAttachmentIds(List<Long> storageAttachmentIds, BigDecimal expired) { |
| | | List<String> res = new ArrayList<>(); |
| | | List<StorageBlobVO> storageBlobVOS = getStorageBlobVOsByStorageAttachmentIds(storageAttachmentIds); |
| | | for (StorageBlobVO storageBlobVO : storageBlobVOS) { |
| | | res.add(buildSignedUrl(storageBlobVO, "/download/", expired)); |
| | | } |
| | | return res; |
| | | } |
| | | |
| | | /** |
| | | * éè¿æä»¶å
³èidè·åæä»¶é¢è§å°å |
| | | * |
| | | * @param application åºç¨ |
| | | * @param recordType è®°å½ç±»å |
| | | * @param recordId è®°å½id |
| | | */ |
| | | public List<String> getFilePreviewURLByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId) { |
| | | List<StorageAttachment> storageAttachments = getStorageAttachmentsByApplicationAndRecordTypeAndRecordId(application, recordType, recordId); |
| | | if (CollectionUtils.isEmpty(storageAttachments)) { |
| | | return new ArrayList<>(); |
| | | } |
| | | return getFilePreviewURLByStorageAttachmentIds(storageAttachments.stream().map(StorageAttachment::getId).collect(Collectors.toList())); |
| | | } |
| | | |
| | | /** |
| | | * éè¿æä»¶å
³èidè·åæä»¶é¢è§å°ååå¨è¿ææ¶é´ |
| | | * |
| | | * @param application åºç¨ |
| | | * @param recordType è®°å½ç±»å |
| | | * @param recordId è®°å½id |
| | | * @param expired è¿ææ¶é´ï¼åéï¼ |
| | | */ |
| | | public List<String> getFilePreviewURLByApplicationAndRecordTypeAndRecordIdAndExpired(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId, BigDecimal expired) { |
| | | List<StorageAttachment> storageAttachments = getStorageAttachmentsByApplicationAndRecordTypeAndRecordId(application, recordType, recordId); |
| | | if (CollectionUtils.isEmpty(storageAttachments)) { |
| | | return new ArrayList<>(); |
| | | } |
| | | return getFilePreviewURLByStorageAttachmentIds(storageAttachments.stream().map(StorageAttachment::getId).collect(Collectors.toList()), expired); |
| | | } |
| | | |
| | | /** |
| | | * éè¿æä»¶å
³èidè·åæä»¶ä¸è½½å°å |
| | | * |
| | | * @param application åºç¨ |
| | | * @param recordType è®°å½ç±»å |
| | | * @param recordId è®°å½id |
| | | */ |
| | | public List<String> getFileDownloadURLByApplicationAndRecordTypeAndRecordId(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId) { |
| | | List<StorageAttachment> storageAttachments = getStorageAttachmentsByApplicationAndRecordTypeAndRecordId(application, recordType, recordId); |
| | | if (CollectionUtils.isEmpty(storageAttachments)) { |
| | | return new ArrayList<>(); |
| | | } |
| | | return getFileDownloadURLByStorageAttachmentIds(storageAttachments.stream().map(StorageAttachment::getId).collect(Collectors.toList())); |
| | | } |
| | | |
| | | /** |
| | | * éè¿æä»¶å
³èidè·åæä»¶ä¸è½½å°ååå¨è¿ææ¶é´ |
| | | * |
| | | * @param application åºç¨ |
| | | * @param recordType è®°å½ç±»å |
| | | * @param recordId è®°å½id |
| | | * @param expired è¿ææ¶é´ï¼åéï¼ |
| | | */ |
| | | public List<String> getFileDownloadURLByApplicationAndRecordTypeAndRecordIdAndExpired(ApplicationTypeEnum application, RecordTypeEnum recordType, Long recordId, BigDecimal expired) { |
| | | List<StorageAttachment> storageAttachments = getStorageAttachmentsByApplicationAndRecordTypeAndRecordId(application, recordType, recordId); |
| | | if (CollectionUtils.isEmpty(storageAttachments)) { |
| | | return new ArrayList<>(); |
| | | } |
| | | return getFileDownloadURLByStorageAttachmentIds(storageAttachments.stream().map(StorageAttachment::getId).collect(Collectors.toList()), expired); |
| | | } |
| | | |
| | | public String buildSignedPreviewUrl(StorageBlobVO storageBlob) { |
| | | return buildSignedUrl(storageBlob, "/preview/", properties.getExpired()); |
| | | } |
| | | public String buildSignedDownloadUrl(StorageBlobVO storageBlob) { |
| | | return buildSignedUrl(storageBlob, "/download/", properties.getExpired()); |
| | | } |
| | | |
| | | /** |
| | | * æå»ºå¸¦ç¾åçURL |
| | | * |
| | | * @param storageBlob æä»¶å
æ°æ® |
| | | * @param actionPath æä½è·¯å¾ "/preview/" or "/download/" |
| | | * @param expired è¿ææ¶é´ 妿ä¸é
ç½®ï¼ä¸ä¼ åï¼å°ä½¿ç¨é»è®¤å¼120åé |
| | | * @return 带ç¾åçURL |
| | | */ |
| | | public String buildSignedUrl(StorageBlobVO storageBlob, String actionPath, BigDecimal expired) { |
| | | if (!Arrays.asList("/preview/", "/download/").contains(actionPath)) { |
| | | throw new IllegalArgumentException("æä½è·¯å¾åæ°é误"); |
| | | } |
| | | if (storageBlob == null || !StringUtils.hasText(storageBlob.getUidFilename())) { |
| | | throw new IllegalArgumentException("æä»¶ä¿¡æ¯ä¸å®æ´"); |
| | | } |
| | | String domain = StringUtils.trimTrailingCharacter(properties.getDomain(), '/'); |
| | | String prefix = properties.getUrlPrefix().startsWith("/") ? properties.getUrlPrefix() : "/" + properties.getUrlPrefix(); |
| | | String normalizedActionPath = StringUtils.hasText(actionPath) ? actionPath : "/preview/"; |
| | | if (!normalizedActionPath.startsWith("/")) { |
| | | normalizedActionPath = "/" + normalizedActionPath; |
| | | } |
| | | if (!normalizedActionPath.endsWith("/")) { |
| | | normalizedActionPath = normalizedActionPath + "/"; |
| | | } |
| | | String baseUrl = domain + prefix + normalizedActionPath + storageBlob.getUidFilename(); |
| | | |
| | | // -1 表示永ä¹
ææï¼ä¸çæ tokenï¼æ¹ä¸º publicKey ç»åæ ¡éª |
| | | if (expired != null && BigDecimal.valueOf(-1L).compareTo(expired) == 0) { |
| | | if (!StringUtils.hasText(storageBlob.getResourceKey())) { |
| | | throw new IllegalArgumentException("å
¬å¼é¾æ¥ç¼ºå°publicKey"); |
| | | } |
| | | return baseUrl + "?publicKey=" + storageBlob.getResourceKey(); |
| | | } |
| | | |
| | | long now = System.currentTimeMillis(); |
| | | BigDecimal expiredValue = expired == null ? new BigDecimal("120") : expired; |
| | | long expiredMillis = expiredValue.multiply(new BigDecimal("60000")).longValue(); |
| | | if (expiredMillis <= 0L) { |
| | | expiredMillis = 2L * 60L * 60L * 1000L; |
| | | } |
| | | Date issuedAt = new Date(now); |
| | | Date expiration = new Date(now + expiredMillis); |
| | | SecretKey key = Keys.hmacShaKeyFor(properties.getJwtSecret().getBytes(StandardCharsets.UTF_8)); |
| | | |
| | | String token = Jwts.builder() |
| | | .subject(storageBlob.getUidFilename()) |
| | | .issuedAt(issuedAt) // æ°çå»ºè®®ç´æ¥è°ç¨ .issuedAt() |
| | | .expiration(expiration) // æ°çå»ºè®®ç´æ¥è°ç¨ .expiration() |
| | | .claim("path", storageBlob.getPath()) |
| | | .claim("resourceKey", storageBlob.getResourceKey()) |
| | | .signWith(key) // éç¹ï¼ä¼ å
¥ä¸é¢çæç key 对象ï¼è䏿¯ String |
| | | .compact(); |
| | | cacheTokenUsage(token, expiredMillis); |
| | | return baseUrl + "?token=" + token; |
| | | } |
| | | |
| | | private void cacheTokenUsage(String token, long expiredMillis) { |
| | | if (!StringUtils.hasText(token)) { |
| | | return; |
| | | } |
| | | long ttl = expiredMillis > 0L ? expiredMillis : 2L * 60L * 60L * 1000L; |
| | | stringRedisTemplate.opsForValue().set(buildTokenUsageKey(token), "0", ttl, TimeUnit.MILLISECONDS); |
| | | } |
| | | |
| | | private String buildTokenUsageKey(String token) { |
| | | return TOKEN_USAGE_KEY_PREFIX + token; |
| | | } |
| | | |
| | | public String buildRelativePath() { |
| | | LocalDate now = LocalDate.now(); |
| | | return now.format(YEAR_PATH_FORMATTER) + "/" + now.format(MONTH_DAY_PATH_FORMATTER); |
| | | } |
| | | |
| | | public void validateTokenUsage(String token) { |
| | | String redisKey = buildTokenUsageKey(token); |
| | | String currentCountValue = stringRedisTemplate.opsForValue().get(redisKey); |
| | | if (!StringUtils.hasText(currentCountValue)) { |
| | | throw new IllegalArgumentException("龿¥å·²è¿ææè¾¾å°ä½¿ç¨æ¬¡æ°å¤±æ"); |
| | | } |
| | | long currentCount = Long.parseLong(currentCountValue); |
| | | int limit = resolveLimit(); |
| | | if (currentCount >= limit) { |
| | | stringRedisTemplate.delete(redisKey); |
| | | throw new IllegalArgumentException("龿¥è¾¾å°ä½¿ç¨æ¬¡æ°å¤±æ"); |
| | | } |
| | | Long updatedCount = stringRedisTemplate.opsForValue().increment(redisKey); |
| | | if (updatedCount != null && updatedCount >= limit) { |
| | | stringRedisTemplate.delete(redisKey); |
| | | } |
| | | } |
| | | |
| | | private int resolveLimit() { |
| | | return properties.getUseLimit() == null || properties.getUseLimit() <= 0 ? 10 : properties.getUseLimit(); |
| | | } |
| | | |
| | | /** |
| | | * å缩æä»¶ å¾ç |
| | | * |
| | | * @param file æä»¶ |
| | | * @return å缩åçæä»¶ |
| | | */ |
| | | public File compressFile(File file) { |
| | | if (properties.getCompress() && isImage(file.getName()) && (file.length() > properties.getNeedCompressSize().toBytes())) { |
| | | try { |
| | | // å建ä¸ä¸ªä¸´æ¶æä»¶åæ¾å缩åçå¾çï¼é¿å
ç ´ååå¾ |
| | | File compressedFile = new File(file.getParent(), "thumb_" + file.getName()); |
| | | |
| | | // 1. 妿已ç»åå¨å缩è¿çæä»¶ï¼ç´æ¥è¿åï¼ä¸åæ¶è CPU å缩 |
| | | if (compressedFile.exists()) { |
| | | return compressedFile; |
| | | } |
| | | |
| | | // ä½¿ç¨ Thumbnailator è¿è¡å缩 |
| | | Thumbnails.of(file) |
| | | .scale(1.0f) // ä¿æå尺寸 |
| | | .outputQuality(properties.getCompressQuality()) // æ ¸å¿ï¼è®¾ç½®ç»è´¨ (0.0~1.0) |
| | | .toFile(compressedFile); |
| | | |
| | | return compressedFile; |
| | | } catch (Exception e) { |
| | | // 妿å缩失败ï¼é级å¤çï¼è¿ååå¾ |
| | | return file; |
| | | } |
| | | } |
| | | return file; |
| | | } |
| | | // ç®åçåç¼å¤æ |
| | | private boolean isImage(String fileName) { |
| | | String ext = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase(); |
| | | return "jpg".equals(ext) || "jpeg".equals(ext) || "png".equals(ext); |
| | | } |
| | | |
| | | |
| | | } |