package com.farriver.bwf.web.initializer.config; import com.github.tobato.fastdfs.FdfsClientConfig; import com.github.tobato.fastdfs.domain.fdfs.StorePath; import com.github.tobato.fastdfs.domain.proto.storage.DownloadCallback; import com.github.tobato.fastdfs.domain.upload.ThumbImage; import com.github.tobato.fastdfs.service.FastFileStorageClient; import jakarta.annotation.Resource; import org.apache.commons.io.FilenameUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Import; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.io.InputStream; @Import(FdfsClientConfig.class) @Component public class FastDFSClientConfig { private static final Logger logger = LoggerFactory.getLogger(FastDFSClientConfig.class); @Resource FastFileStorageClient storageClient; @Value("${nginx.host}") private String nginxHost; @Value("${nginx.port}") private String nginxPort; @Value("${fdfs.thumbImage.width}") private int thumbImageWeight = 150; @Value("${fdfs.thumbImage.height}") private int thumbImageHeight = 150; @Value("${fdfs.soTimeout}") private long soTimeout = 1500; @Value("${fdfs.connectTimeout}") private long connectTimeout = 600; private ThumbImage thumbImage; public ThumbImage getThumbImage() { if (thumbImage == null) { thumbImage = new ThumbImage(thumbImageWeight, thumbImageHeight); } return thumbImage; } public String uploadFile(MultipartFile file) throws IOException { StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null); String path = thumbImage.getThumbImagePath(storePath.getPath()); System.out.println("thumbImage :" + path); return getResAccessUrl(storePath); } public void delFile(String filePath) { storageClient.deleteFile(filePath); } public InputStream download(String groupName, String path) { InputStream ins = storageClient.downloadFile(groupName, path, new DownloadCallback() { @Override public InputStream recv(InputStream ins) throws IOException { return ins; } }); return ins; } private String getResAccessUrl(StorePath storePath) { String fileUrl = "http://" + nginxHost + ":" + nginxPort + "/" + storePath.getFullPath(); return fileUrl; } }