refactor: 重命名QiniuService ->FileService
重命名为FileService 并修改方法为获取File Manager来处理文件
This commit is contained in:
@@ -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,7 +1,8 @@
|
|||||||
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 cn.celess.blog.service.interfaces.FileService;
|
||||||
import com.qiniu.common.QiniuException;
|
import com.qiniu.common.QiniuException;
|
||||||
import com.qiniu.common.Zone;
|
import com.qiniu.common.Zone;
|
||||||
import com.qiniu.http.Response;
|
import com.qiniu.http.Response;
|
||||||
@@ -20,7 +21,7 @@ import java.io.InputStream;
|
|||||||
* @date : 2019/04/25 18:15
|
* @date : 2019/04/25 18:15
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class QiniuServiceImpl implements QiniuService {
|
public class QiniuFileServiceImpl implements FileManager {
|
||||||
private static final Configuration cfg = new Configuration(Zone.zone2());
|
private static final Configuration cfg = new Configuration(Zone.zone2());
|
||||||
private static UploadManager uploadManager;
|
private static UploadManager uploadManager;
|
||||||
private static BucketManager bucketManager;
|
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.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