forked from 201206030/novel-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9975fae
commit c62acc2
Showing
16 changed files
with
211 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
novel-front/src/main/java/com/java2nb/novel/core/config/OssProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.java2nb.novel.core.config; | ||
|
||
import lombok.Data; | ||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* @author 11797 | ||
*/ | ||
@Data | ||
@Component | ||
@ConfigurationProperties(prefix="novel.file") | ||
public class OssProperties{ | ||
|
||
private String endpoint; | ||
|
||
private String keyId; | ||
|
||
private String keySecret; | ||
|
||
private String fileHost; | ||
|
||
private String bucketName; | ||
|
||
private String webUrl; | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
novel-front/src/main/java/com/java2nb/novel/service/FileService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.java2nb.novel.service; | ||
|
||
|
||
/** | ||
* @author 11797 | ||
*/ | ||
public interface FileService { | ||
|
||
/** | ||
* 将爬取的网络图片转存为自己的存储介质(本地、OSS、fastDfs) | ||
* @param picSrc 爬取的网络图片路径 | ||
* @param picSavePath 保存路径 | ||
* @return 新图片地址 | ||
* */ | ||
String transFile(String picSrc, String picSavePath); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
novel-front/src/main/java/com/java2nb/novel/service/impl/FastDfsFileServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.java2nb.novel.service.impl; | ||
|
||
import com.java2nb.novel.service.FileService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* @author 11797 | ||
*/ | ||
@Service | ||
@RequiredArgsConstructor | ||
@ConditionalOnProperty(prefix = "pic.save", name = "storage", havingValue = "fastDfs") | ||
public class FastDfsFileServiceImpl implements FileService { | ||
|
||
@Override | ||
public String transFile(String picSrc, String picSavePath) { | ||
return null; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
novel-front/src/main/java/com/java2nb/novel/service/impl/LocalFileServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.java2nb.novel.service.impl; | ||
|
||
import com.java2nb.novel.core.utils.Constants; | ||
import com.java2nb.novel.core.utils.FileUtil; | ||
import com.java2nb.novel.service.FileService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* @author 11797 | ||
*/ | ||
@Service | ||
@RequiredArgsConstructor | ||
@ConditionalOnProperty(prefix = "pic.save", name = "storage", havingValue = "local") | ||
public class LocalFileServiceImpl implements FileService { | ||
|
||
@Override | ||
public String transFile(String picSrc, String picSavePath){ | ||
|
||
return FileUtil.network2Local(picSrc, picSavePath, Constants.LOCAL_PIC_PREFIX); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
novel-front/src/main/java/com/java2nb/novel/service/impl/OssFileServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.java2nb.novel.service.impl; | ||
|
||
import com.aliyun.oss.OSSClient; | ||
import com.aliyun.oss.model.CannedAccessControlList; | ||
import com.aliyun.oss.model.CreateBucketRequest; | ||
import com.aliyun.oss.model.PutObjectRequest; | ||
import com.aliyun.oss.model.PutObjectResult; | ||
import com.java2nb.novel.core.config.OssProperties; | ||
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.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.File; | ||
|
||
/** | ||
* @author 11797 | ||
*/ | ||
@Service | ||
@RequiredArgsConstructor | ||
@ConditionalOnProperty(prefix = "pic.save", name = "storage", havingValue = "OSS") | ||
@Slf4j | ||
public class OssFileServiceImpl implements FileService { | ||
|
||
private final OssProperties ossProperties; | ||
|
||
@Override | ||
public String transFile(String picSrc, String picSavePath) { | ||
|
||
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; | ||
} | ||
|
||
filePath = filePath.replaceFirst(picSavePath,""); | ||
|
||
filePath = filePath.startsWith("/") ? filePath.replaceFirst("/","") : filePath; | ||
|
||
|
||
OSSClient ossClient = new OSSClient(ossProperties.getEndpoint(), ossProperties.getKeyId(), ossProperties.getKeySecret()); | ||
try { | ||
//容器不存在,就创建 | ||
if (!ossClient.doesBucketExist(ossProperties.getBucketName())) { | ||
ossClient.createBucket(ossProperties.getBucketName()); | ||
CreateBucketRequest createBucketRequest = new CreateBucketRequest(ossProperties.getBucketName()); | ||
createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead); | ||
ossClient.createBucket(createBucketRequest); | ||
} | ||
//上传文件 | ||
PutObjectResult result = ossClient.putObject(new PutObjectRequest(ossProperties.getBucketName(), filePath, file)); | ||
//设置权限 这里是公开读 | ||
ossClient.setBucketAcl(ossProperties.getBucketName(), CannedAccessControlList.PublicRead); | ||
|
||
if(result != null) { | ||
return ossProperties.getWebUrl() + "/" + filePath; | ||
} | ||
} catch (Exception e) { | ||
log.error(e.getMessage(), e); | ||
} finally { | ||
//关闭 | ||
ossClient.shutdown(); | ||
file.delete(); | ||
} | ||
|
||
return "/images/default.gif"; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#OSS | ||
novel: | ||
file: | ||
endpoint: oss-cn-shenzhen.aliyuncs.com #不同的服务器,地址不同 | ||
key-id: dhjjrue6767778878 #去OSS控制台获取 | ||
key-secret: dssdkkrkelrkellk44554 #去OSS控制台获取 | ||
bucket-name: novel #这个自己创建bucket时的命名,控制台创建也行,代码创建也行 | ||
file-host: pic #文件路径 | ||
web-url: https://xxyimg.oss-cn-hangzhou.aliyuncs.com #OSS文件的web访问地址 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ server: | |
spring: | ||
profiles: | ||
active: dev | ||
include: alipay | ||
include: alipay,oss | ||
|
||
|
||
rabbitmq: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters