OSSFileService.java 2.72 KB
Newer Older
张辰's avatar
张辰 committed
1 2 3 4
package com.tanpu.community.service;

import com.tanpu.common.constant.BizStatus;
import com.tanpu.common.util.AliyunOSSHelper;
刘基明's avatar
刘基明 committed
5
import com.tanpu.common.uuid.UuidGenHelper;
张辰's avatar
张辰 committed
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
import com.tanpu.community.api.enums.OssRelType;
import com.tanpu.community.dao.entity.community.FileRecordEntity;
import com.tanpu.community.dao.mapper.community.FileRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

@Slf4j
@Service
public class OSSFileService {

    @Value("${aliyun.oss.endpoint}")
    public String endpoint;

    @Value("${aliyun.oss.accessId}")
    public String accessId;

    @Value("${aliyun.oss.accessSK}")
    public String accessSK;

    @Value("${aliyun.oss.bucketName}")
    public String bucketName;

    private AliyunOSSHelper ossHelper;

    @Resource
    private FileRecordMapper fileRecordMapper;

刘基明's avatar
刘基明 committed
39 40 41 42

    @Autowired
    private UuidGenHelper uuidGenHelper;

张辰's avatar
张辰 committed
43 44 45 46 47
    @PostConstruct
    public void init() {
        ossHelper = AliyunOSSHelper.build(endpoint, accessId, accessSK);
    }

张辰's avatar
张辰 committed
48 49 50 51 52 53
    @Transactional
    public FileRecordEntity uploadFile(byte[] data, String fileName) {
        String fileSuffix = fileName.substring(fileName.lastIndexOf('.') + 1);
        return uploadFile(data, fileName, fileSuffix, null, OssRelType.None);
    }

张辰's avatar
张辰 committed
54 55 56 57 58 59 60 61 62 63
    @Transactional
    public FileRecordEntity uploadFile(byte[] data, String fileName, String relId, OssRelType relType) {
        String fileSuffix = fileName.substring(fileName.lastIndexOf('.') + 1);
        return uploadFile(data, fileName, fileSuffix, relId, relType);
    }

    @Transactional
    public FileRecordEntity uploadFile(byte[] data, String fileName, String fileSuffix,
                                       String relId, OssRelType relType) {
        // todo uniqueLong
张辰's avatar
张辰 committed
64 65
        String key = String.valueOf(System.currentTimeMillis());
        String id = String.valueOf(System.currentTimeMillis());
张辰's avatar
张辰 committed
66 67 68 69

        ossHelper.writeFile(bucketName, key, data, fileSuffix);

        FileRecordEntity record = new FileRecordEntity();
刘基明's avatar
刘基明 committed
70
        record.setFileId(uuidGenHelper.getUuidStr());
张辰's avatar
张辰 committed
71
        record.setDeleteTag(BizStatus.DeleteTag.tag_init);
刘基明's avatar
刘基明 committed
72 73 74 75
        record.setFileOssKey(key);
        record.setFileId(fileName);
        record.setPreviewUrl(ossHelper.getPreSignedUrl(bucketName, key));
        record.setFileType(relType.type);
张辰's avatar
张辰 committed
76 77 78 79 80 81

        fileRecordMapper.insert(record);

        return record;
    }

刘基明's avatar
刘基明 committed
82 83 84 85
    public FileRecordEntity queryById(String fileId) {
        return fileRecordMapper.selectById(fileId);
    }

张辰's avatar
张辰 committed
86
}