添加多运行环境支持 #14
@@ -5,7 +5,7 @@ import cn.celess.blog.entity.Response;
|
||||
import cn.celess.blog.entity.model.QiniuResponse;
|
||||
import cn.celess.blog.exception.MyException;
|
||||
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.RedisUserUtil;
|
||||
import cn.celess.blog.util.RedisUtil;
|
||||
@@ -42,7 +42,7 @@ public class CommonController {
|
||||
@Autowired
|
||||
CountService countService;
|
||||
@Autowired
|
||||
QiniuService qiniuService;
|
||||
FileService fileService;
|
||||
@Autowired
|
||||
RedisUtil redisUtil;
|
||||
@Autowired
|
||||
@@ -161,7 +161,7 @@ public class CommonController {
|
||||
String mime = fileName.substring(fileName.lastIndexOf("."));
|
||||
if (".png".equals(mime.toLowerCase()) || ".jpg".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("message", "上传成功");
|
||||
map.put("url", "http://cdn.celess.cn/" + qiniuResponse.key);
|
||||
@@ -208,7 +208,7 @@ public class CommonController {
|
||||
assert fileName != null;
|
||||
String mime = fileName.substring(fileName.lastIndexOf("."));
|
||||
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("success", 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,7 +1,8 @@
|
||||
package cn.celess.blog.service.serviceimpl;
|
||||
|
||||
import cn.celess.blog.entity.model.QiniuResponse;
|
||||
import cn.celess.blog.service.QiniuService;
|
||||
import cn.celess.blog.service.interfaces.FileManager;
|
||||
import cn.celess.blog.service.interfaces.FileService;
|
||||
import com.qiniu.common.QiniuException;
|
||||
import com.qiniu.common.Zone;
|
||||
import com.qiniu.http.Response;
|
||||
@@ -20,7 +21,7 @@ import java.io.InputStream;
|
||||
* @date : 2019/04/25 18:15
|
||||
*/
|
||||
@Service
|
||||
public class QiniuServiceImpl implements QiniuService {
|
||||
public class QiniuFileServiceImpl implements FileManager {
|
||||
private static final Configuration cfg = new Configuration(Zone.zone2());
|
||||
private static UploadManager uploadManager;
|
||||
private static BucketManager bucketManager;
|
||||
@@ -12,8 +12,8 @@ import cn.celess.blog.entity.request.LoginReq;
|
||||
import cn.celess.blog.entity.request.UserReq;
|
||||
import cn.celess.blog.exception.MyException;
|
||||
import cn.celess.blog.mapper.UserMapper;
|
||||
import cn.celess.blog.service.interfaces.FileService;
|
||||
import cn.celess.blog.service.MailService;
|
||||
import cn.celess.blog.service.QiniuService;
|
||||
import cn.celess.blog.service.UserService;
|
||||
import cn.celess.blog.util.*;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
@@ -46,7 +46,7 @@ public class UserServiceImpl implements UserService {
|
||||
@Autowired
|
||||
MailService mailService;
|
||||
@Autowired
|
||||
QiniuService qiniuService;
|
||||
FileService fileService;
|
||||
@Autowired
|
||||
RedisUtil redisUtil;
|
||||
@Autowired
|
||||
@@ -185,7 +185,7 @@ public class UserServiceImpl implements UserService {
|
||||
@Override
|
||||
public Object updateUserAavatarImg(InputStream is, String mime) {
|
||||
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);
|
||||
userMapper.updateAvatarImgUrl(upload.key, user.getId());
|
||||
redisUserUtil.set(user);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
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.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.request.LoginReq;
|
||||
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.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
@@ -325,7 +326,7 @@ public class BaseTest {
|
||||
}
|
||||
|
||||
@Slf4j
|
||||
public static class TestQiNiuServiceImpl implements QiniuService {
|
||||
public static class TestQiniuFileServiceImpl implements FileManager {
|
||||
@Override
|
||||
public QiniuResponse uploadFile(InputStream is, String fileName) {
|
||||
QiniuResponse response = new QiniuResponse();
|
||||
|
||||
@@ -115,7 +115,8 @@ public class UserControllerTest extends BaseTest {
|
||||
assertNotNull(inputStream);
|
||||
|
||||
// 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);
|
||||
getMockData(multipart("/user/imgUpload").file(file), userLogin()).andDo(result -> {
|
||||
|
||||
Reference in New Issue
Block a user