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.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,87 +1,88 @@
|
||||
package cn.celess.blog.service.serviceimpl;
|
||||
|
||||
import cn.celess.blog.entity.model.QiniuResponse;
|
||||
import cn.celess.blog.service.QiniuService;
|
||||
import com.qiniu.common.QiniuException;
|
||||
import com.qiniu.common.Zone;
|
||||
import com.qiniu.http.Response;
|
||||
import com.qiniu.storage.BucketManager;
|
||||
import com.qiniu.storage.Configuration;
|
||||
import com.qiniu.storage.UploadManager;
|
||||
import com.qiniu.storage.model.FileInfo;
|
||||
import com.qiniu.util.Auth;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/04/25 18:15
|
||||
*/
|
||||
@Service
|
||||
public class QiniuServiceImpl implements QiniuService {
|
||||
private static final Configuration cfg = new Configuration(Zone.zone2());
|
||||
private static UploadManager uploadManager;
|
||||
private static BucketManager bucketManager;
|
||||
private static Auth auth;
|
||||
|
||||
@Value("${qiniu.accessKey}")
|
||||
private String accessKey;
|
||||
@Value("${qiniu.secretKey}")
|
||||
private String secretKey;
|
||||
@Value("${qiniu.bucket}")
|
||||
private String bucket;
|
||||
|
||||
private void init() {
|
||||
if (auth == null || uploadManager == null || bucketManager == null) {
|
||||
auth = Auth.create(accessKey, secretKey);
|
||||
uploadManager = new UploadManager(cfg);
|
||||
bucketManager = new BucketManager(auth, cfg);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public QiniuResponse uploadFile(InputStream is, String fileName) {
|
||||
init();
|
||||
//文件存在则删除文件
|
||||
if (continueFile(fileName)) {
|
||||
try {
|
||||
System.out.println(bucketManager.delete(bucket, fileName).toString());
|
||||
} catch (QiniuException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
//上传
|
||||
try {
|
||||
Response response = uploadManager.put(is, fileName, auth.uploadToken(bucket), null, null);
|
||||
return response.jsonToObject(QiniuResponse.class);
|
||||
} catch (QiniuException e) {
|
||||
Response r = e.response;
|
||||
System.err.println(r.toString());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileInfo[] getFileList() {
|
||||
init();
|
||||
BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(bucket, "", 1000, "");
|
||||
FileInfo[] items = null;
|
||||
while (fileListIterator.hasNext()) {
|
||||
//处理获取的file list结果
|
||||
items = fileListIterator.next();
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
private boolean continueFile(String key) {
|
||||
FileInfo[] allFile = getFileList();
|
||||
for (FileInfo fileInfo : allFile) {
|
||||
if (key.equals(fileInfo.key)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
package cn.celess.blog.service.serviceimpl;
|
||||
|
||||
import cn.celess.blog.entity.model.QiniuResponse;
|
||||
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;
|
||||
import com.qiniu.storage.BucketManager;
|
||||
import com.qiniu.storage.Configuration;
|
||||
import com.qiniu.storage.UploadManager;
|
||||
import com.qiniu.storage.model.FileInfo;
|
||||
import com.qiniu.util.Auth;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/04/25 18:15
|
||||
*/
|
||||
@Service
|
||||
public class QiniuFileServiceImpl implements FileManager {
|
||||
private static final Configuration cfg = new Configuration(Zone.zone2());
|
||||
private static UploadManager uploadManager;
|
||||
private static BucketManager bucketManager;
|
||||
private static Auth auth;
|
||||
|
||||
@Value("${qiniu.accessKey}")
|
||||
private String accessKey;
|
||||
@Value("${qiniu.secretKey}")
|
||||
private String secretKey;
|
||||
@Value("${qiniu.bucket}")
|
||||
private String bucket;
|
||||
|
||||
private void init() {
|
||||
if (auth == null || uploadManager == null || bucketManager == null) {
|
||||
auth = Auth.create(accessKey, secretKey);
|
||||
uploadManager = new UploadManager(cfg);
|
||||
bucketManager = new BucketManager(auth, cfg);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public QiniuResponse uploadFile(InputStream is, String fileName) {
|
||||
init();
|
||||
//文件存在则删除文件
|
||||
if (continueFile(fileName)) {
|
||||
try {
|
||||
System.out.println(bucketManager.delete(bucket, fileName).toString());
|
||||
} catch (QiniuException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
//上传
|
||||
try {
|
||||
Response response = uploadManager.put(is, fileName, auth.uploadToken(bucket), null, null);
|
||||
return response.jsonToObject(QiniuResponse.class);
|
||||
} catch (QiniuException e) {
|
||||
Response r = e.response;
|
||||
System.err.println(r.toString());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileInfo[] getFileList() {
|
||||
init();
|
||||
BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(bucket, "", 1000, "");
|
||||
FileInfo[] items = null;
|
||||
while (fileListIterator.hasNext()) {
|
||||
//处理获取的file list结果
|
||||
items = fileListIterator.next();
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
private boolean continueFile(String key) {
|
||||
FileInfo[] allFile = getFileList();
|
||||
for (FileInfo fileInfo : allFile) {
|
||||
if (key.equals(fileInfo.key)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user