diff --git a/novel-front/pom.xml b/novel-front/pom.xml index 74fbe24..97a7352 100644 --- a/novel-front/pom.xml +++ b/novel-front/pom.xml @@ -51,6 +51,18 @@ 1.3.1 + + + org.springframework + spring-mock + 2.0.8 + + + com.github.tobato + fastdfs-client + 1.26.1-RELEASE + + org.springframework.boot spring-boot-starter-data-elasticsearch diff --git a/novel-front/src/main/java/com/java2nb/novel/FrontNovelApplication.java b/novel-front/src/main/java/com/java2nb/novel/FrontNovelApplication.java index f09652d..db642d8 100644 --- a/novel-front/src/main/java/com/java2nb/novel/FrontNovelApplication.java +++ b/novel-front/src/main/java/com/java2nb/novel/FrontNovelApplication.java @@ -1,10 +1,12 @@ package com.java2nb.novel; +import com.github.tobato.fastdfs.FdfsClientConfig; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Import; import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; @@ -18,6 +20,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; @EnableScheduling @EnableCaching @MapperScan(basePackages = {"com.java2nb.novel.mapper"}) +@Import(FdfsClientConfig.class) public class FrontNovelApplication { public static void main(String[] args) { diff --git a/novel-front/src/main/java/com/java2nb/novel/core/config/FdfsConfiguration.java b/novel-front/src/main/java/com/java2nb/novel/core/config/FdfsConfiguration.java new file mode 100644 index 0000000..485ad08 --- /dev/null +++ b/novel-front/src/main/java/com/java2nb/novel/core/config/FdfsConfiguration.java @@ -0,0 +1,13 @@ +package com.java2nb.novel.core.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.EnableMBeanExport; +import org.springframework.jmx.support.RegistrationPolicy; + +/** + * @author 11797 + */ +@Configuration +@EnableMBeanExport(registration= RegistrationPolicy.IGNORE_EXISTING) +public class FdfsConfiguration { +} diff --git a/novel-front/src/main/java/com/java2nb/novel/service/impl/FastDfsFileServiceImpl.java b/novel-front/src/main/java/com/java2nb/novel/service/impl/FastDfsFileServiceImpl.java index b356609..9f43bd8 100644 --- a/novel-front/src/main/java/com/java2nb/novel/service/impl/FastDfsFileServiceImpl.java +++ b/novel-front/src/main/java/com/java2nb/novel/service/impl/FastDfsFileServiceImpl.java @@ -1,20 +1,60 @@ package com.java2nb.novel.service.impl; +import com.github.tobato.fastdfs.domain.StorePath; +import com.github.tobato.fastdfs.service.FastFileStorageClient; +import com.java2nb.novel.core.utils.Constants; +import com.java2nb.novel.core.utils.FileUtil; import com.java2nb.novel.service.FileService; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.io.FilenameUtils; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.stereotype.Service; +import java.io.File; +import java.io.FileInputStream; + /** * @author 11797 */ @Service @RequiredArgsConstructor +@Slf4j @ConditionalOnProperty(prefix = "pic.save", name = "storage", havingValue = "fastDfs") public class FastDfsFileServiceImpl implements FileService { + private final FastFileStorageClient storageClient; + + @Value("${fdfs.webUrl}") + private String webUrl; + @Override public String transFile(String picSrc, String picSavePath) { - return null; + + File file; + String filePath = FileUtil.network2Local(picSrc, picSavePath, Constants.LOCAL_PIC_PREFIX); + if (filePath.contains(Constants.LOCAL_PIC_PREFIX)) { + file = new File(picSavePath + filePath); + } else { + //默认图片不存储 + return filePath; + } + + try { + FileInputStream inputStream = new FileInputStream(file); + StorePath storePath = storageClient.uploadFile(inputStream, file.length(), + FilenameUtils.getExtension(file.getName()), null); + //这里额外加上LOCAL_PIC_PREFIX路径,表明该图片是个人资源,而不是爬虫爬取的网络资源,不需要再次进行转换, + // 实际访问时,再通过nginx的rewite指令来重写路径,去掉LOCAL_PIC_PREFIX + return webUrl+Constants.LOCAL_PIC_PREFIX+storePath.getFullPath(); + } catch (Exception e) { + log.error(e.getMessage(), e); + } finally { + //删除 + file.delete(); + } + + return "/images/default.gif"; } } diff --git a/novel-front/src/main/resources/application-fastdfs.yml b/novel-front/src/main/resources/application-fastdfs.yml new file mode 100644 index 0000000..132db23 --- /dev/null +++ b/novel-front/src/main/resources/application-fastdfs.yml @@ -0,0 +1,16 @@ +#FastDfs的配置 ==================================== +#读取inputsream阻塞时间 +fdfs: + connect-timeout: 600 + so-timeout: 1500 + #tracker地址 + trackerList: 127.0.0.1:22122 + #缩略图配置 + thumbImage: + height: 150 + width: 150 + #通过nginx 访问地址 + webUrl: http://127.0.0.1/ + #获取连接池最大数量 + pool.max-total: 200 + diff --git a/novel-front/src/main/resources/application.yml b/novel-front/src/main/resources/application.yml index a75d6bb..cf1f7f9 100644 --- a/novel-front/src/main/resources/application.yml +++ b/novel-front/src/main/resources/application.yml @@ -4,7 +4,7 @@ server: spring: profiles: active: dev - include: alipay,oss + include: alipay,oss,fastdfs rabbitmq: