FastDFSAttachmentServiceImpl.java 4.72 KB
package com.hotent.file.attachmentService;

import cn.hutool.core.io.IoUtil;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.FileInfo;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.hotent.base.attachment.Attachment;
import com.hotent.base.attachment.AttachmentService;
import com.hotent.base.attachment.MultipartFileParam;
import com.hotent.base.attachment.UploadShardResult;
import com.hotent.base.util.AppUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.*;
import java.util.Map;

@Service
public class FastDFSAttachmentServiceImpl implements AttachmentService {


    @Resource
    private FastFileStorageClient storageClient;


    @Override
    public String getStoreType() {
        return "fastDFS";
    }

    private Logger logger = LoggerFactory.getLogger(FastDFSAttachmentServiceImpl.class);
    @Override
    public void remove(Attachment attachment, String propertiesId) throws Exception {
        if (StringUtils.isEmpty(attachment.getFilePath())) {
            return;
        }
        try {
            StorePath storePath = StorePath.parseFromUrl(attachment.getFilePath());
            storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
        } catch (FdfsUnsupportStorePathException e) {
            logger.error("文件删除失败{}", e.getMessage());
            throw e;
        }
    }

    @Override
    public void upload(Attachment attachment, InputStream inputStream, String propertiesId) throws Exception {
        Long byteCount = attachment.getByteCount();
        StorePath storePath = storageClient.uploadFile(inputStream, byteCount, attachment.getExtensionName(), null);
        attachment.setFilePath(storePath.getFullPath());
        System.out.println(storePath);
    }

    @Override
    public void download(Attachment attachment, OutputStream outStream, String propertiesId) throws Exception {
        String fileUrl = attachment.getFilePath();
        String group = fileUrl.substring(0, fileUrl.indexOf("/"));
        String path = fileUrl.substring(fileUrl.indexOf("/") + 1);
        DownloadByteArray downloadByteArray = new DownloadByteArray();
        byte[] bytes = storageClient.downloadFile(group, path, downloadByteArray);
        //将文件流写入到输出流中
        IoUtil.write(outStream, true, bytes);

    }

    @Override
    public boolean chekckFile(Attachment attachment, String propertiesId) throws Exception {
        String fileUrl = attachment.getFilePath();
        String group = fileUrl.substring(0, fileUrl.indexOf("/"));
        String path = fileUrl.substring(fileUrl.indexOf("/") + 1);
        FileInfo fileInfo = storageClient.queryFileInfo(group, path);
        return fileInfo != null;
    }

    @Override
    public byte[] getFileBytes(Attachment sysFile) throws Exception {
        String fileUrl = sysFile.getFilePath();
        String group = fileUrl.substring(0, fileUrl.indexOf("/"));
        String path = fileUrl.substring(fileUrl.indexOf("/") + 1);
        DownloadByteArray downloadByteArray = new DownloadByteArray();
        byte[] bytes = storageClient.downloadFile(group, path, downloadByteArray);
        return bytes;
    }

    @Override
    public UploadShardResult uploadByShard(MultipartFileParam multipartFileParam, Attachment attachment, String propertiesId) throws Exception {
        return null;
    }

    @Override
    public Map<String, Object> generatePolicy(Attachment attachment, String propertiesId) {
        return null;
    }

    @Override
    public String getUrl(Attachment attachment, String propertiesId) throws UnsupportedEncodingException {
        FdfsWebServer fdfsWebServer= AppUtil.getBean(FdfsWebServer.class);
        String fileUrl = fdfsWebServer.getWebServerUrl() + attachment.getFilePath();
        return fileUrl;
    }

    @Override
    public InputStream getFileInputStream(Attachment attachment) throws FileNotFoundException {
        String fileUrl = attachment.getFilePath();
        String group = fileUrl.substring(0, fileUrl.indexOf("/"));
        String path = fileUrl.substring(fileUrl.indexOf("/") + 1);
        DownloadByteArray downloadByteArray = new DownloadByteArray();
        byte[] bytes = storageClient.downloadFile(group, path, downloadByteArray);
        return new ByteArrayInputStream(bytes);
    }

    @Override
    public String getFilePath(String account, String fileName) {
        return null;
    }
}