All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.rexsheng.springboot.faster.system.file.application.FileServiceImpl Maven / Gradle / Ivy

The newest version!
package com.github.rexsheng.springboot.faster.system.file.application;

import com.github.rexsheng.springboot.faster.function.Tuple2;
import com.github.rexsheng.springboot.faster.io.file.FileSystemService;
import com.github.rexsheng.springboot.faster.io.file.FileSystemServiceFactory;
import com.github.rexsheng.springboot.faster.io.file.model.DownloadFileRequest;
import com.github.rexsheng.springboot.faster.io.file.model.DownloadFileResponse;
import com.github.rexsheng.springboot.faster.io.file.model.UploadFileRequest;
import com.github.rexsheng.springboot.faster.io.file.model.UploadFileResponse;
import com.github.rexsheng.springboot.faster.system.file.application.dto.*;
import com.github.rexsheng.springboot.faster.system.file.domain.SysFile;
import com.github.rexsheng.springboot.faster.system.file.domain.gateway.DeleteFileDO;
import com.github.rexsheng.springboot.faster.system.file.domain.gateway.FileGateway;
import com.github.rexsheng.springboot.faster.system.file.domain.gateway.QueryFileDO;
import jakarta.annotation.Resource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@ConditionalOnClass({SqlSessionFactoryBean.class})
public class FileServiceImpl implements FileService {

    private static final Logger logger= LoggerFactory.getLogger(FileServiceImpl.class);

    @Resource
    @Lazy
    private FileSystemService fileSystemService;

    @Resource
    @Lazy
    private FileSystemServiceFactory fileSystemServiceFactory;

    @Resource
    private FileGateway fileGateway;

    @Override
    @Transactional
    public AddFileResponse addFile(AddFileRequest request) {
        SysFile domin=request.toDomain();
        domin.setServiceName(fileSystemService.serviceName());
        Long fileId=fileGateway.insertFile(domin);
        if(fileId==null){
            logger.warn("保存文件失败: {}", domin.getOriginalFileName());
            throw new RuntimeException("保存失败");
        }
        UploadFileRequest uploadFileRequest=new UploadFileRequest(domin.getBucketName(),domin.getObjectKey(),new ByteArrayInputStream(domin.getFileContent()));
        try {
            UploadFileResponse uploadFileResponse=fileSystemService.uploadFile(uploadFileRequest);
            if(uploadFileResponse!=null){
                return AddFileResponse.of(SysFile.encryptFileId(fileId));
            }
            else{
                logger.warn("fileservice保存文件失败: {}", domin.getOriginalFileName());
                throw new RuntimeException("保存失败");
            }
        } catch (IOException e) {
            throw new RuntimeException("保存文件失败",e);
        }
    }

    @Override
    public FileDetailResponse getFile(String fileId) {
        SysFile domain=fileGateway.getFile(SysFile.decryptFileId(fileId));
        if(domain!=null && !Boolean.TRUE.equals(domain.getDel())){
            domain.setFileContent(downloadFileContent(domain));
            return FileDetailResponse.of(domain);
        }
        throw new IllegalArgumentException("文件不存在");
    }

    private byte[] downloadFileContent(SysFile sysFile){
        DownloadFileRequest downloadFileRequest=new DownloadFileRequest(sysFile.getBucketName(),sysFile.getObjectKey());
        try {
            DownloadFileResponse downloadFileResponse=fileSystemServiceFactory.getFileSystemService(sysFile.getServiceName()).downloadFile(downloadFileRequest);
            if(downloadFileResponse!=null){
                return downloadFileResponse.getContent();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    @Override
    public FileDetailResponse getFileMeta(String fileId) {
        SysFile domain=fileGateway.getFile(SysFile.decryptFileId(fileId));
        if(domain!=null && !Boolean.TRUE.equals(domain.getDel())){
            return FileDetailResponse.of(domain);
        }
        throw new IllegalArgumentException("文件不存在");
    }

    @Override
    public List queryList(QueryFileRequest request) {
        QueryFileDO queryFileDO=new QueryFileDO();
        queryFileDO.setBusinessType(request.getBusinessType());
        queryFileDO.setBusinessId(request.getBusinessId());
        queryFileDO.setFileIds(request.getFileIds());
        List dataList=fileGateway.queryFiles(queryFileDO);
        if(Boolean.TRUE.equals(request.getFetchContent())){
            return dataList.stream().peek(sysFile -> sysFile.setFileContent(downloadFileContent(sysFile))).map(FileDetailResponse::of).collect(Collectors.toList());
        }
        return dataList.stream().map(FileDetailResponse::of).collect(Collectors.toList());
    }

    @Override
    public void deleteFile(String fileId) {
        deleteFiles(Arrays.asList(fileId));
    }

    @Override
    @Transactional
    public void deleteFiles(List fileIds) {
        QueryFileDO queryFileDO=new QueryFileDO();
        queryFileDO.setFileIds(fileIds);
        List dataList=fileGateway.queryFiles(queryFileDO);
        fileGateway.deleteFiles(fileIds.stream().map(fileId -> SysFile.of(fileId,true)).collect(Collectors.toList()));
        if(!ObjectUtils.isEmpty(dataList)){
            for(SysFile sysFile:dataList){
                com.github.rexsheng.springboot.faster.io.file.model.DeleteFileRequest deleteFileRequest=
                        new com.github.rexsheng.springboot.faster.io.file.model.DeleteFileRequest(sysFile.getBucketName(),sysFile.getObjectKey());
                try {
                    fileSystemServiceFactory.getFileSystemService(sysFile.getServiceName()).deleteFile(deleteFileRequest);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    @Override
    @Transactional
    public void deleteFilesIfNotExists(List newestFileIds, List rangeFileIds) {
        List arr1=newestFileIds.stream().map(id -> SysFile.decryptFileId(id)).collect(Collectors.toList());
        List> arr2=rangeFileIds.stream().map(id -> new Tuple2(id,SysFile.decryptFileId(id))).collect(Collectors.toList());
        List> deleteIds=arr2.stream().filter(a->!arr1.contains(a.getSecond())).collect(Collectors.toList());
        if(!ObjectUtils.isEmpty(deleteIds)){
            deleteFiles(deleteIds.stream().map(Tuple2::getFirst).collect(Collectors.toList()));
        }
    }

    @Override
    @Transactional
    public void deleteFiles(DeleteFileRequest request) {
        QueryFileDO queryFileDO=new QueryFileDO();
        queryFileDO.setBusinessType(request.getBusinessType());
        queryFileDO.setBusinessId(request.getBusinessId());
        queryFileDO.setFileIds(request.getFileIds());
        List dataList=fileGateway.queryFiles(queryFileDO);

        DeleteFileDO deleteFileDO=new DeleteFileDO();
        deleteFileDO.setBusinessType(request.getBusinessType());
        deleteFileDO.setBusinessId(request.getBusinessId());
        deleteFileDO.setFileIds(request.getFileIds());
        fileGateway.deleteFiles(deleteFileDO);

        if(!ObjectUtils.isEmpty(dataList)){
            for(SysFile sysFile:dataList){
                com.github.rexsheng.springboot.faster.io.file.model.DeleteFileRequest deleteFileRequest=
                        new com.github.rexsheng.springboot.faster.io.file.model.DeleteFileRequest(sysFile.getBucketName(),sysFile.getObjectKey());
                try {
                    fileSystemServiceFactory.getFileSystemService(sysFile.getServiceName()).deleteFile(deleteFileRequest);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy