refactor(文件服务): 接口修改返回值
This commit is contained in:
@@ -2,7 +2,7 @@ package cn.celess.blog.controller;
|
||||
|
||||
import cn.celess.blog.enmu.ResponseEnum;
|
||||
import cn.celess.blog.entity.Response;
|
||||
import cn.celess.blog.entity.model.QiniuResponse;
|
||||
import cn.celess.blog.entity.model.FileResponse;
|
||||
import cn.celess.blog.exception.MyException;
|
||||
import cn.celess.blog.service.CountService;
|
||||
import cn.celess.blog.service.FileService;
|
||||
@@ -161,10 +161,10 @@ 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 = fileService.getFileManager().uploadFile(file.getInputStream(), "img_" + System.currentTimeMillis() + mime);
|
||||
FileResponse fileResponse = 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);
|
||||
map.put("url", "http://cdn.celess.cn/" + fileResponse.key);
|
||||
response.getWriter().println(mapper.writeValueAsString(map));
|
||||
redisUtil.setEx(request.getRemoteAddr() + "-ImgUploadTimes", uploadTimes + 1 + "", 2, TimeUnit.HOURS);
|
||||
return;
|
||||
@@ -208,12 +208,12 @@ public class CommonController {
|
||||
assert fileName != null;
|
||||
String mime = fileName.substring(fileName.lastIndexOf("."));
|
||||
String name = fileName.replace(mime, "").replaceAll(" ", "");
|
||||
QiniuResponse qiniuResponse = fileService.getFileManager().uploadFile(file.getInputStream(), "file_" + name + '_' + System.currentTimeMillis() + mime);
|
||||
FileResponse fileResponse = fileService.getFileManager().uploadFile(file.getInputStream(), "file_" + name + '_' + System.currentTimeMillis() + mime);
|
||||
resp.put("originalFilename", fileName);
|
||||
resp.put("success", qiniuResponse != null);
|
||||
if (qiniuResponse != null) {
|
||||
resp.put("success", fileResponse != null);
|
||||
if (fileResponse != null) {
|
||||
resp.put("host", "http://cdn.celess.cn/");
|
||||
resp.put("path", qiniuResponse.key);
|
||||
resp.put("path", fileResponse.key);
|
||||
}
|
||||
result.add(resp);
|
||||
}
|
||||
|
||||
25
src/main/java/cn/celess/blog/entity/model/FileInfo.java
Normal file
25
src/main/java/cn/celess/blog/entity/model/FileInfo.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package cn.celess.blog.entity.model;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2020/10/15 22:16
|
||||
* @desc :
|
||||
*/
|
||||
public class FileInfo {
|
||||
/**
|
||||
* 文件名
|
||||
*/
|
||||
public String key;
|
||||
/**
|
||||
* 文件hash值
|
||||
*/
|
||||
public String hash;
|
||||
/**
|
||||
* 文件大小,单位:字节
|
||||
*/
|
||||
public long size;
|
||||
/**
|
||||
* 文件上传时间,单位为:100纳秒
|
||||
*/
|
||||
public long uploadTime;
|
||||
}
|
||||
@@ -5,9 +5,9 @@ package cn.celess.blog.entity.model;
|
||||
* @author : xiaohai
|
||||
* @date : 2019/04/21 22:43
|
||||
*/
|
||||
public class QiniuResponse {
|
||||
public class FileResponse {
|
||||
public String key;
|
||||
public String hash;
|
||||
public String bucket;
|
||||
public long fsize;
|
||||
public long size;
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
package cn.celess.blog.service;
|
||||
|
||||
import cn.celess.blog.entity.model.QiniuResponse;
|
||||
import com.qiniu.storage.model.FileInfo;
|
||||
import cn.celess.blog.entity.model.FileInfo;
|
||||
import cn.celess.blog.entity.model.FileResponse;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
@@ -18,10 +19,10 @@ public interface FileManager {
|
||||
* 解决语法错误 占位方法
|
||||
*
|
||||
*/
|
||||
QiniuResponse uploadFile(InputStream is, String fileName);
|
||||
FileResponse uploadFile(InputStream is, String fileName);
|
||||
|
||||
/**
|
||||
* 解决语法错误 占位方法
|
||||
*/
|
||||
FileInfo[] getFileList();
|
||||
List<FileInfo> getFileList();
|
||||
}
|
||||
|
||||
@@ -1,19 +1,25 @@
|
||||
package cn.celess.blog.service.fileserviceimpl;
|
||||
|
||||
import cn.celess.blog.entity.model.QiniuResponse;
|
||||
import cn.celess.blog.entity.model.FileInfo;
|
||||
import cn.celess.blog.entity.model.FileResponse;
|
||||
import cn.celess.blog.service.FileManager;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
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 com.qiniu.util.StringMap;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
@@ -42,7 +48,7 @@ public class QiniuFileServiceImpl implements FileManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public QiniuResponse uploadFile(InputStream is, String fileName) {
|
||||
public FileResponse uploadFile(InputStream is, String fileName) {
|
||||
init();
|
||||
//文件存在则删除文件
|
||||
if (continueFile(fileName)) {
|
||||
@@ -55,7 +61,15 @@ public class QiniuFileServiceImpl implements FileManager {
|
||||
//上传
|
||||
try {
|
||||
Response response = uploadManager.put(is, fileName, auth.uploadToken(bucket), null, null);
|
||||
return response.jsonToObject(QiniuResponse.class);
|
||||
FileResponse fileResponse = new FileResponse();
|
||||
StringMap stringMap = response.jsonToMap();
|
||||
|
||||
fileResponse.key = (String) stringMap.get("key");
|
||||
fileResponse.bucket = (String) stringMap.get("bucket");
|
||||
fileResponse.size = (long) stringMap.get("fsize");
|
||||
fileResponse.hash = (String) stringMap.get("hash");
|
||||
|
||||
return fileResponse;
|
||||
} catch (QiniuException e) {
|
||||
Response r = e.response;
|
||||
System.err.println(r.toString());
|
||||
@@ -64,20 +78,30 @@ public class QiniuFileServiceImpl implements FileManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileInfo[] getFileList() {
|
||||
public List<FileInfo> getFileList() {
|
||||
init();
|
||||
List<FileInfo> infoList = null;
|
||||
BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(bucket, "", 1000, "");
|
||||
FileInfo[] items = null;
|
||||
while (fileListIterator.hasNext()) {
|
||||
//处理获取的file list结果
|
||||
items = fileListIterator.next();
|
||||
infoList = new ArrayList<com.qiniu.storage.model.FileInfo>(Arrays.asList(fileListIterator.next()))
|
||||
.stream()
|
||||
.map(item -> {
|
||||
FileInfo fileInfo = new FileInfo();
|
||||
fileInfo.key = item.key;
|
||||
fileInfo.hash = item.hash;
|
||||
fileInfo.size = item.fsize;
|
||||
fileInfo.uploadTime = item.putTime;
|
||||
return fileInfo;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
return items;
|
||||
return infoList;
|
||||
}
|
||||
|
||||
private boolean continueFile(String key) {
|
||||
FileInfo[] allFile = getFileList();
|
||||
for (FileInfo fileInfo : allFile) {
|
||||
List<FileInfo> fileList = getFileList();
|
||||
for (FileInfo fileInfo : fileList) {
|
||||
if (key.equals(fileInfo.key)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@ import cn.celess.blog.enmu.RoleEnum;
|
||||
import cn.celess.blog.enmu.UserAccountStatusEnum;
|
||||
import cn.celess.blog.entity.Response;
|
||||
import cn.celess.blog.entity.User;
|
||||
import cn.celess.blog.entity.model.FileResponse;
|
||||
import cn.celess.blog.entity.model.PageData;
|
||||
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.entity.request.UserReq;
|
||||
@@ -186,7 +186,7 @@ public class UserServiceImpl implements UserService {
|
||||
@Override
|
||||
public Object updateUserAavatarImg(InputStream is, String mime) {
|
||||
User user = redisUserUtil.get();
|
||||
QiniuResponse upload = fileService.getFileManager().uploadFile(is, user.getEmail() + "_" + user.getId() + mime.toLowerCase());
|
||||
FileResponse upload = fileService.getFileManager().uploadFile(is, user.getEmail() + "_" + user.getId() + mime.toLowerCase());
|
||||
user.setAvatarImgUrl(upload.key);
|
||||
userMapper.updateAvatarImgUrl(upload.key, user.getId());
|
||||
redisUserUtil.set(user);
|
||||
|
||||
@@ -2,7 +2,8 @@ package cn.celess.blog;
|
||||
|
||||
|
||||
import cn.celess.blog.entity.Response;
|
||||
import cn.celess.blog.entity.model.QiniuResponse;
|
||||
import cn.celess.blog.entity.model.FileInfo;
|
||||
import cn.celess.blog.entity.model.FileResponse;
|
||||
import cn.celess.blog.entity.model.UserModel;
|
||||
import cn.celess.blog.entity.request.LoginReq;
|
||||
import cn.celess.blog.service.MailService;
|
||||
@@ -10,7 +11,6 @@ import cn.celess.blog.service.FileManager;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.qiniu.storage.model.FileInfo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
@@ -37,6 +37,8 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
@@ -327,21 +329,21 @@ public class BaseTest {
|
||||
@Slf4j
|
||||
public static class TestQiniuFileServiceImpl implements FileManager {
|
||||
@Override
|
||||
public QiniuResponse uploadFile(InputStream is, String fileName) {
|
||||
QiniuResponse response = new QiniuResponse();
|
||||
public FileResponse uploadFile(InputStream is, String fileName) {
|
||||
FileResponse response = new FileResponse();
|
||||
log.debug("上传文件请求,[fileName:{}]", fileName);
|
||||
|
||||
response.key = "key";
|
||||
response.bucket = "bucket";
|
||||
response.hash = "hash";
|
||||
response.fsize = 1;
|
||||
response.size = 1;
|
||||
return response;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileInfo[] getFileList() {
|
||||
public List<FileInfo> getFileList() {
|
||||
log.debug("获取文件列表请求");
|
||||
return new FileInfo[0];
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user