添加多运行环境支持 #14
@@ -5,7 +5,7 @@ import cn.celess.blog.entity.Response;
|
|||||||
import cn.celess.blog.entity.model.QiniuResponse;
|
import cn.celess.blog.entity.model.QiniuResponse;
|
||||||
import cn.celess.blog.exception.MyException;
|
import cn.celess.blog.exception.MyException;
|
||||||
import cn.celess.blog.service.CountService;
|
import cn.celess.blog.service.CountService;
|
||||||
import cn.celess.blog.service.QiniuService;
|
import cn.celess.blog.service.interfaces.FileService;
|
||||||
import cn.celess.blog.util.HttpUtil;
|
import cn.celess.blog.util.HttpUtil;
|
||||||
import cn.celess.blog.util.RedisUserUtil;
|
import cn.celess.blog.util.RedisUserUtil;
|
||||||
import cn.celess.blog.util.RedisUtil;
|
import cn.celess.blog.util.RedisUtil;
|
||||||
@@ -42,7 +42,7 @@ public class CommonController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
CountService countService;
|
CountService countService;
|
||||||
@Autowired
|
@Autowired
|
||||||
QiniuService qiniuService;
|
FileService fileService;
|
||||||
@Autowired
|
@Autowired
|
||||||
RedisUtil redisUtil;
|
RedisUtil redisUtil;
|
||||||
@Autowired
|
@Autowired
|
||||||
@@ -161,7 +161,7 @@ public class CommonController {
|
|||||||
String mime = fileName.substring(fileName.lastIndexOf("."));
|
String mime = fileName.substring(fileName.lastIndexOf("."));
|
||||||
if (".png".equals(mime.toLowerCase()) || ".jpg".equals(mime.toLowerCase()) ||
|
if (".png".equals(mime.toLowerCase()) || ".jpg".equals(mime.toLowerCase()) ||
|
||||||
".jpeg".equals(mime.toLowerCase()) || ".bmp".equals(mime.toLowerCase())) {
|
".jpeg".equals(mime.toLowerCase()) || ".bmp".equals(mime.toLowerCase())) {
|
||||||
QiniuResponse qiniuResponse = qiniuService.uploadFile(file.getInputStream(), "img_" + System.currentTimeMillis() + mime);
|
QiniuResponse qiniuResponse = fileService.getFileManager().uploadFile(file.getInputStream(), "img_" + System.currentTimeMillis() + mime);
|
||||||
map.put("success", 1);
|
map.put("success", 1);
|
||||||
map.put("message", "上传成功");
|
map.put("message", "上传成功");
|
||||||
map.put("url", "http://cdn.celess.cn/" + qiniuResponse.key);
|
map.put("url", "http://cdn.celess.cn/" + qiniuResponse.key);
|
||||||
@@ -208,7 +208,7 @@ public class CommonController {
|
|||||||
assert fileName != null;
|
assert fileName != null;
|
||||||
String mime = fileName.substring(fileName.lastIndexOf("."));
|
String mime = fileName.substring(fileName.lastIndexOf("."));
|
||||||
String name = fileName.replace(mime, "").replaceAll(" ", "");
|
String name = fileName.replace(mime, "").replaceAll(" ", "");
|
||||||
QiniuResponse qiniuResponse = qiniuService.uploadFile(file.getInputStream(), "file_" + name + '_' + System.currentTimeMillis() + mime);
|
QiniuResponse qiniuResponse = fileService.getFileManager().uploadFile(file.getInputStream(), "file_" + name + '_' + System.currentTimeMillis() + mime);
|
||||||
resp.put("originalFilename", fileName);
|
resp.put("originalFilename", fileName);
|
||||||
resp.put("success", qiniuResponse != null);
|
resp.put("success", qiniuResponse != null);
|
||||||
if (qiniuResponse != null) {
|
if (qiniuResponse != null) {
|
||||||
|
|||||||
@@ -1,31 +0,0 @@
|
|||||||
package cn.celess.blog.service;
|
|
||||||
|
|
||||||
import cn.celess.blog.entity.model.QiniuResponse;
|
|
||||||
import com.qiniu.storage.model.FileInfo;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import java.io.InputStream;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author : xiaohai
|
|
||||||
* @date : 2019/04/25 18:15
|
|
||||||
*/
|
|
||||||
@Service
|
|
||||||
public interface QiniuService {
|
|
||||||
/**
|
|
||||||
* 上传文件
|
|
||||||
*
|
|
||||||
* @param is InputStream流
|
|
||||||
* @param fileName 文件名
|
|
||||||
* @return 响应数据
|
|
||||||
*/
|
|
||||||
QiniuResponse uploadFile(InputStream is, String fileName);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取文件列表
|
|
||||||
*
|
|
||||||
* @return 文件列表
|
|
||||||
*/
|
|
||||||
FileInfo[] getFileList();
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package cn.celess.blog.service.interfaces;
|
||||||
|
|
||||||
|
import cn.celess.blog.entity.model.QiniuResponse;
|
||||||
|
import com.qiniu.storage.model.FileInfo;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : xiaohai
|
||||||
|
* @date : 2020/10/15 18:19
|
||||||
|
* @desc : 文件管理器 定义操作文件的方法
|
||||||
|
*/
|
||||||
|
public interface FileManager {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解决语法错误 占位方法
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
QiniuResponse uploadFile(InputStream is, String fileName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解决语法错误 占位方法
|
||||||
|
*/
|
||||||
|
FileInfo[] getFileList();
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package cn.celess.blog.service.interfaces;
|
||||||
|
|
||||||
|
import cn.celess.blog.entity.model.QiniuResponse;
|
||||||
|
import com.qiniu.storage.model.FileInfo;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : xiaohai
|
||||||
|
* @date : 2019/04/25 18:15
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public interface FileService {
|
||||||
|
/**
|
||||||
|
* 获取文件管理器
|
||||||
|
*
|
||||||
|
* @return 文件管理器 可以操作文件
|
||||||
|
*/
|
||||||
|
FileManager getFileManager();
|
||||||
|
}
|
||||||
@@ -1,87 +1,88 @@
|
|||||||
package cn.celess.blog.service.serviceimpl;
|
package cn.celess.blog.service.serviceimpl;
|
||||||
|
|
||||||
import cn.celess.blog.entity.model.QiniuResponse;
|
import cn.celess.blog.entity.model.QiniuResponse;
|
||||||
import cn.celess.blog.service.QiniuService;
|
import cn.celess.blog.service.interfaces.FileManager;
|
||||||
import com.qiniu.common.QiniuException;
|
import cn.celess.blog.service.interfaces.FileService;
|
||||||
import com.qiniu.common.Zone;
|
import com.qiniu.common.QiniuException;
|
||||||
import com.qiniu.http.Response;
|
import com.qiniu.common.Zone;
|
||||||
import com.qiniu.storage.BucketManager;
|
import com.qiniu.http.Response;
|
||||||
import com.qiniu.storage.Configuration;
|
import com.qiniu.storage.BucketManager;
|
||||||
import com.qiniu.storage.UploadManager;
|
import com.qiniu.storage.Configuration;
|
||||||
import com.qiniu.storage.model.FileInfo;
|
import com.qiniu.storage.UploadManager;
|
||||||
import com.qiniu.util.Auth;
|
import com.qiniu.storage.model.FileInfo;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import com.qiniu.util.Auth;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
import java.io.InputStream;
|
|
||||||
|
import java.io.InputStream;
|
||||||
/**
|
|
||||||
* @author : xiaohai
|
/**
|
||||||
* @date : 2019/04/25 18:15
|
* @author : xiaohai
|
||||||
*/
|
* @date : 2019/04/25 18:15
|
||||||
@Service
|
*/
|
||||||
public class QiniuServiceImpl implements QiniuService {
|
@Service
|
||||||
private static final Configuration cfg = new Configuration(Zone.zone2());
|
public class QiniuFileServiceImpl implements FileManager {
|
||||||
private static UploadManager uploadManager;
|
private static final Configuration cfg = new Configuration(Zone.zone2());
|
||||||
private static BucketManager bucketManager;
|
private static UploadManager uploadManager;
|
||||||
private static Auth auth;
|
private static BucketManager bucketManager;
|
||||||
|
private static Auth auth;
|
||||||
@Value("${qiniu.accessKey}")
|
|
||||||
private String accessKey;
|
@Value("${qiniu.accessKey}")
|
||||||
@Value("${qiniu.secretKey}")
|
private String accessKey;
|
||||||
private String secretKey;
|
@Value("${qiniu.secretKey}")
|
||||||
@Value("${qiniu.bucket}")
|
private String secretKey;
|
||||||
private String bucket;
|
@Value("${qiniu.bucket}")
|
||||||
|
private String bucket;
|
||||||
private void init() {
|
|
||||||
if (auth == null || uploadManager == null || bucketManager == null) {
|
private void init() {
|
||||||
auth = Auth.create(accessKey, secretKey);
|
if (auth == null || uploadManager == null || bucketManager == null) {
|
||||||
uploadManager = new UploadManager(cfg);
|
auth = Auth.create(accessKey, secretKey);
|
||||||
bucketManager = new BucketManager(auth, cfg);
|
uploadManager = new UploadManager(cfg);
|
||||||
}
|
bucketManager = new BucketManager(auth, cfg);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
@Override
|
|
||||||
public QiniuResponse uploadFile(InputStream is, String fileName) {
|
@Override
|
||||||
init();
|
public QiniuResponse uploadFile(InputStream is, String fileName) {
|
||||||
//文件存在则删除文件
|
init();
|
||||||
if (continueFile(fileName)) {
|
//文件存在则删除文件
|
||||||
try {
|
if (continueFile(fileName)) {
|
||||||
System.out.println(bucketManager.delete(bucket, fileName).toString());
|
try {
|
||||||
} catch (QiniuException e) {
|
System.out.println(bucketManager.delete(bucket, fileName).toString());
|
||||||
e.printStackTrace();
|
} catch (QiniuException e) {
|
||||||
}
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
//上传
|
}
|
||||||
try {
|
//上传
|
||||||
Response response = uploadManager.put(is, fileName, auth.uploadToken(bucket), null, null);
|
try {
|
||||||
return response.jsonToObject(QiniuResponse.class);
|
Response response = uploadManager.put(is, fileName, auth.uploadToken(bucket), null, null);
|
||||||
} catch (QiniuException e) {
|
return response.jsonToObject(QiniuResponse.class);
|
||||||
Response r = e.response;
|
} catch (QiniuException e) {
|
||||||
System.err.println(r.toString());
|
Response r = e.response;
|
||||||
return null;
|
System.err.println(r.toString());
|
||||||
}
|
return null;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
@Override
|
|
||||||
public FileInfo[] getFileList() {
|
@Override
|
||||||
init();
|
public FileInfo[] getFileList() {
|
||||||
BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(bucket, "", 1000, "");
|
init();
|
||||||
FileInfo[] items = null;
|
BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(bucket, "", 1000, "");
|
||||||
while (fileListIterator.hasNext()) {
|
FileInfo[] items = null;
|
||||||
//处理获取的file list结果
|
while (fileListIterator.hasNext()) {
|
||||||
items = fileListIterator.next();
|
//处理获取的file list结果
|
||||||
}
|
items = fileListIterator.next();
|
||||||
return items;
|
}
|
||||||
}
|
return items;
|
||||||
|
}
|
||||||
private boolean continueFile(String key) {
|
|
||||||
FileInfo[] allFile = getFileList();
|
private boolean continueFile(String key) {
|
||||||
for (FileInfo fileInfo : allFile) {
|
FileInfo[] allFile = getFileList();
|
||||||
if (key.equals(fileInfo.key)) {
|
for (FileInfo fileInfo : allFile) {
|
||||||
return true;
|
if (key.equals(fileInfo.key)) {
|
||||||
}
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
}
|
||||||
}
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
@@ -12,8 +12,8 @@ import cn.celess.blog.entity.request.LoginReq;
|
|||||||
import cn.celess.blog.entity.request.UserReq;
|
import cn.celess.blog.entity.request.UserReq;
|
||||||
import cn.celess.blog.exception.MyException;
|
import cn.celess.blog.exception.MyException;
|
||||||
import cn.celess.blog.mapper.UserMapper;
|
import cn.celess.blog.mapper.UserMapper;
|
||||||
|
import cn.celess.blog.service.interfaces.FileService;
|
||||||
import cn.celess.blog.service.MailService;
|
import cn.celess.blog.service.MailService;
|
||||||
import cn.celess.blog.service.QiniuService;
|
|
||||||
import cn.celess.blog.service.UserService;
|
import cn.celess.blog.service.UserService;
|
||||||
import cn.celess.blog.util.*;
|
import cn.celess.blog.util.*;
|
||||||
import com.github.pagehelper.PageHelper;
|
import com.github.pagehelper.PageHelper;
|
||||||
@@ -46,7 +46,7 @@ public class UserServiceImpl implements UserService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
MailService mailService;
|
MailService mailService;
|
||||||
@Autowired
|
@Autowired
|
||||||
QiniuService qiniuService;
|
FileService fileService;
|
||||||
@Autowired
|
@Autowired
|
||||||
RedisUtil redisUtil;
|
RedisUtil redisUtil;
|
||||||
@Autowired
|
@Autowired
|
||||||
@@ -185,7 +185,7 @@ public class UserServiceImpl implements UserService {
|
|||||||
@Override
|
@Override
|
||||||
public Object updateUserAavatarImg(InputStream is, String mime) {
|
public Object updateUserAavatarImg(InputStream is, String mime) {
|
||||||
User user = redisUserUtil.get();
|
User user = redisUserUtil.get();
|
||||||
QiniuResponse upload = qiniuService.uploadFile(is, user.getEmail() + "_" + user.getId() + mime.toLowerCase());
|
QiniuResponse upload = fileService.getFileManager().uploadFile(is, user.getEmail() + "_" + user.getId() + mime.toLowerCase());
|
||||||
user.setAvatarImgUrl(upload.key);
|
user.setAvatarImgUrl(upload.key);
|
||||||
userMapper.updateAvatarImgUrl(upload.key, user.getId());
|
userMapper.updateAvatarImgUrl(upload.key, user.getId());
|
||||||
redisUserUtil.set(user);
|
redisUserUtil.set(user);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
spring.profiles.active=easyDeploy
|
spring.profiles.active=easyDeploy
|
||||||
####七牛的配置
|
####七牛的配置
|
||||||
####cn.celess.blog.service.serviceimpl.QiniuServiceImpl
|
####cn.celess.blog.service.serviceimpl.QiniuFileServiceImpl
|
||||||
logging.level.cn.celess.blog=debug
|
logging.level.cn.celess.blog=debug
|
||||||
logging.level.cn.celess.blog.mapper=info
|
logging.level.cn.celess.blog.mapper=info
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,8 @@ import cn.celess.blog.entity.model.QiniuResponse;
|
|||||||
import cn.celess.blog.entity.model.UserModel;
|
import cn.celess.blog.entity.model.UserModel;
|
||||||
import cn.celess.blog.entity.request.LoginReq;
|
import cn.celess.blog.entity.request.LoginReq;
|
||||||
import cn.celess.blog.service.MailService;
|
import cn.celess.blog.service.MailService;
|
||||||
import cn.celess.blog.service.QiniuService;
|
import cn.celess.blog.service.interfaces.FileManager;
|
||||||
|
import cn.celess.blog.service.interfaces.FileService;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
@@ -325,7 +326,7 @@ public class BaseTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public static class TestQiNiuServiceImpl implements QiniuService {
|
public static class TestQiniuFileServiceImpl implements FileManager {
|
||||||
@Override
|
@Override
|
||||||
public QiniuResponse uploadFile(InputStream is, String fileName) {
|
public QiniuResponse uploadFile(InputStream is, String fileName) {
|
||||||
QiniuResponse response = new QiniuResponse();
|
QiniuResponse response = new QiniuResponse();
|
||||||
|
|||||||
@@ -115,7 +115,8 @@ public class UserControllerTest extends BaseTest {
|
|||||||
assertNotNull(inputStream);
|
assertNotNull(inputStream);
|
||||||
|
|
||||||
// mock 实现类
|
// mock 实现类
|
||||||
mockInjectInstance(userService, "qiniuService", new TestQiNiuServiceImpl());
|
// FIXME :: mock 时不应该为 qiniuService
|
||||||
|
mockInjectInstance(userService, "qiniuService", new TestQiniuFileServiceImpl());
|
||||||
|
|
||||||
MockMultipartFile file = new MockMultipartFile("file", "logo.png", MediaType.IMAGE_PNG_VALUE, inputStream);
|
MockMultipartFile file = new MockMultipartFile("file", "logo.png", MediaType.IMAGE_PNG_VALUE, inputStream);
|
||||||
getMockData(multipart("/user/imgUpload").file(file), userLogin()).andDo(result -> {
|
getMockData(multipart("/user/imgUpload").file(file), userLogin()).andDo(result -> {
|
||||||
|
|||||||
Reference in New Issue
Block a user