优化存储

master
mayongjian 12 months ago
parent 1dade3d7d4
commit 0956a5dc3e

@ -0,0 +1,22 @@
package com.hongshu.common.config;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
/**
* @author hongshu
*/
@Configuration
@Slf4j
@Data
public class OssConfig {
/**
* type (0: 1 2minio)
*/
@Value("${oss.type}")
Integer type;
}

@ -25,11 +25,10 @@ public class WebOssController {
*
*
* @param file
* @param type
*/
@PostMapping("save/{type}")
public Result<?> save(MultipartFile file, @PathVariable Integer type) {
String path = ossService.save(file, type);
public Result<?> save(MultipartFile file) {
String path = ossService.save(file);
return Result.ok(path);
}
@ -37,14 +36,13 @@ public class WebOssController {
*
*
* @param files
* @param type
*/
@PostMapping(value = "saveBatch/{type}")
public Result<List<String>> saveBatch(@RequestParam("uploadFiles") MultipartFile[] files, @PathVariable Integer type) {
@PostMapping(value = "saveBatch")
public Result<List<String>> saveBatch(@RequestParam("uploadFiles") MultipartFile[] files) {
if (files.length == 0) {
return Result.fail(null);
}
List<String> stringList = ossService.saveBatch(files, type);
List<String> stringList = ossService.saveBatch(files);
return Result.ok(stringList);
}
@ -52,11 +50,10 @@ public class WebOssController {
*
*
* @param path
* @param type
*/
@GetMapping("delete/{type}")
public Result<?> delete(String path, @PathVariable Integer type) {
ossService.delete(path, type);
@GetMapping("delete")
public Result<?> delete(String path) {
ossService.delete(path);
return Result.ok();
}
@ -64,14 +61,13 @@ public class WebOssController {
*
*
* @param filePaths
* @param type
*/
@PostMapping(value = "deleteBatch/{type}")
public Result<?> deleteBatch(@RequestBody List<String> filePaths, @PathVariable Integer type) {
@PostMapping(value = "deleteBatch")
public Result<?> deleteBatch(@RequestBody List<String> filePaths) {
if (filePaths.isEmpty()) {
return Result.fail(null);
}
ossService.batchDelete(filePaths, type);
ossService.batchDelete(filePaths);
return Result.ok();
}
}

@ -7,7 +7,7 @@ hongshu:
# 版权年份
copyrightYear: 2024
# 文件路径 示例( Windows配置 D:/hongshu/uploadPathLinux配置 /home/hongshu/uploadPath
profile: /hongshu/uploadPath
profile: /Users/mayongjian/Desktop/hongshu-gitee/hongshu/upload
# 获取ip地址开关
addressEnabled: false
# 验证码类型 math 数字计算 char 字符验证
@ -65,6 +65,7 @@ spring:
restart:
# 热部署开关
enabled: true
# redis 配置
redis:
# 地址
@ -139,16 +140,16 @@ xss:
# 匹配链接
urlPatterns: /system/*,/monitor/*,/tool/*
# ES配置
# ElasticSearch配置
es:
url: localhost
port: 9200
username:
password:
# oss配置
# Oss配置
oss:
type: 1 # 0是本地存储1是七牛云存储
type: 0 # 0是本地存储1是七牛云存储
# 七牛云配置
qiNiuYun:

@ -1,13 +1,14 @@
package com.hongshu.web.auth;
import com.hongshu.common.constant.UploadFileConstant;
import com.hongshu.common.config.HongshuConfig;
import com.hongshu.common.constant.Constants;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
/**
* @Author hongshu
* @author: hongshu
*/
@Configuration
public class LoginMvcConfigureAdapter extends WebMvcConfigurationSupport {
@ -31,8 +32,12 @@ public class LoginMvcConfigureAdapter extends WebMvcConfigurationSupport {
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(UploadFileConstant.OSS + "/**") //虚拟url路径
.addResourceLocations("file:" + UploadFileConstant.ADDRESS); //真实本地路径
super.addResourceHandlers(registry);
// registry.addResourceHandler(UploadFileConstant.OSS + "/**") //虚拟url路径
// .addResourceLocations("file:" + UploadFileConstant.ADDRESS); //真实本地路径
// super.addResourceHandlers(registry);
/** 本地文件上传路径 */
registry.addResourceHandler(Constants.RESOURCE_PREFIX + "/**")
.addResourceLocations("file:" + HongshuConfig.getProfile() + "/");
}
}

@ -15,31 +15,27 @@ public interface IWebOssService {
*
*
* @param file
* @param type
*/
String save(MultipartFile file, Integer type);
String save(MultipartFile file);
/**
*
*
* @param files
* @param type
*/
List<String> saveBatch(MultipartFile[] files, Integer type);
List<String> saveBatch(MultipartFile[] files);
/**
*
*
* @param path
* @param type
*/
void delete(String path, Integer type);
void delete(String path);
/**
*
*
* @param filePaths
* @param type
*/
void batchDelete(List<String> filePaths, Integer type);
void batchDelete(List<String> filePaths);
}

@ -15,7 +15,6 @@ import com.hongshu.web.service.IWebOssService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
@ -36,8 +35,6 @@ public class SysMemberServiceImpl implements ISysMemberService {
private SysMemberMapper memberMapper;
@Autowired
private IWebOssService ossService;
@Value("${oss.type}")
Integer type;
/**
@ -85,7 +82,7 @@ public class SysMemberServiceImpl implements ISysMemberService {
public int insertMember(WebUser user, MultipartFile file) {
// 上传头像
if (ObjectUtils.isNotEmpty(file)) {
String avatar = ossService.save(file, type);
String avatar = ossService.save(file);
user.setAvatar(avatar);
}
user.setHsId(Long.valueOf(RandomUtil.randomNumbers(10)));
@ -108,7 +105,7 @@ public class SysMemberServiceImpl implements ISysMemberService {
WebUser webUser = memberMapper.selectById(user.getId());
// 更新头像
if (ObjectUtils.isNotEmpty(file)) {
String avatar = ossService.save(file, type);
String avatar = ossService.save(file);
user.setAvatar(avatar);
}
String newPassword = webUser.getPassword();

@ -9,7 +9,6 @@ import com.hongshu.web.service.ISysNavbarService;
import com.hongshu.web.service.IWebOssService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
@ -31,8 +30,6 @@ public class SysNavbarServiceImpl implements ISysNavbarService {
private IWebOssService ossService;
@Autowired
private SysNavbarMapper navbarMapper;
@Value("${oss.type}")
Integer type;
/**
@ -81,7 +78,7 @@ public class SysNavbarServiceImpl implements ISysNavbarService {
*/
@Override
public int insertNavbar(WebNavbar category, MultipartFile file) {
String normalCover = ossService.save(file, type);
String normalCover = ossService.save(file);
category.setNormalCover(normalCover);
category.setCreator("system");
category.setCreateTime(new Date());
@ -98,7 +95,7 @@ public class SysNavbarServiceImpl implements ISysNavbarService {
@Override
public int updateNavbar(WebNavbar category, MultipartFile file) {
if (ObjectUtils.isNotEmpty(file)) {
String normalCover = ossService.save(file, type);
String normalCover = ossService.save(file);
category.setNormalCover(normalCover);
}
category.setUpdater("system");

@ -13,16 +13,15 @@ import com.hongshu.web.domain.entity.WebNavbar;
import com.hongshu.web.domain.entity.WebNote;
import com.hongshu.web.domain.entity.WebUser;
import com.hongshu.web.domain.vo.NoteSearchVO;
import com.hongshu.web.mapper.WebUserMapper;
import com.hongshu.web.mapper.SysNavbarMapper;
import com.hongshu.web.mapper.SysNoteMapper;
import com.hongshu.web.mapper.WebUserMapper;
import com.hongshu.web.service.ISysNoteService;
import com.hongshu.web.service.IWebEsNoteService;
import com.hongshu.web.service.IWebOssService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
@ -49,8 +48,6 @@ public class SysNoteServiceImpl implements ISysNoteService {
private SysNavbarMapper navbarMapper;
@Autowired
private IWebOssService ossService;
@Value("${oss.type}")
Integer type;
/**
@ -118,7 +115,7 @@ public class SysNoteServiceImpl implements ISysNoteService {
public int insertNote(WebNote note, MultipartFile file) {
// 上传头像
if (ObjectUtils.isNotEmpty(file)) {
String noteCover = ossService.save(file, type);
String noteCover = ossService.save(file);
note.setNoteCover(noteCover);
}
note.setCreator("System");
@ -136,7 +133,7 @@ public class SysNoteServiceImpl implements ISysNoteService {
public int updateNote(WebNote note, MultipartFile file) {
// 上传头像
if (ObjectUtils.isNotEmpty(file)) {
String noteCover = ossService.save(file, type);
String noteCover = ossService.save(file);
note.setNoteCover(noteCover);
}
note.setUpdater("System");

@ -97,8 +97,7 @@ public class WebChatServiceImpl extends ServiceImpl<WebChatMapper, WebChat> impl
*/
@Override
public List<ChatUserRelationVO> getChatUserList() {
String currentUid = WebUtils.getRequestHeader(UserConstant.USER_ID);
// String currentUid = AuthContextHolder.getUserId();
String currentUid = AuthContextHolder.getUserId();
List<ChatUserRelationVO> result = new ArrayList<>();
List<WebChatUserRelation> chatUserRelationList = chatUserRelationMapper.selectList(new QueryWrapper<WebChatUserRelation>().eq("accept_uid", currentUid).orderByDesc("timestamp"));
if (chatUserRelationList.isEmpty()) {

@ -187,8 +187,7 @@ public class WebCommentServiceImpl extends ServiceImpl<WebCommentMapper, WebComm
@Override
public IPage<CommentVO> getNoticeComment(long currentPage, long pageSize) {
Page<CommentVO> result = new Page<>();
String currentUid = WebUtils.getRequestHeader(UserConstant.USER_ID);
// String currentUid = AuthContextHolder.getUserId();
String currentUid = AuthContextHolder.getUserId();
Page<WebComment> commentPage = this.page(new Page<>((int) currentPage, (int) pageSize), new QueryWrapper<WebComment>().or(e -> e.eq("note_uid", currentUid).or().eq("reply_uid", currentUid)).ne("uid", currentUid).orderByDesc("create_time"));

@ -52,8 +52,7 @@ public class WebFollowServiceImpl extends ServiceImpl<WebFollowMapper, WebFollow
public Page<TrendVO> getFollowTrend(long currentPage, long pageSize) {
Page<TrendVO> page = new Page<>();
// 得到当前用户所有关注的用户
String currentUid = WebUtils.getRequestHeader(UserConstant.USER_ID);
// String currentUid = AuthContextHolder.getUserId();
String currentUid = AuthContextHolder.getUserId();
List<WebFollow> followers = this.list(new QueryWrapper<WebFollow>().eq("uid", currentUid));
List<String> fids = followers.stream().map(WebFollow::getFid).collect(Collectors.toList());
fids.add(currentUid);
@ -107,7 +106,7 @@ public class WebFollowServiceImpl extends ServiceImpl<WebFollowMapper, WebFollow
@Override
public Page<TrendVO> getFollowList(long currentPage, long pageSize) {
Page<TrendVO> page = new Page<>();
String currentUid = WebUtils.getRequestHeader(UserConstant.USER_ID);
String currentUid = AuthContextHolder.getUserId();
List<WebFollow> followers = this.list(new QueryWrapper<WebFollow>().eq("uid", currentUid));
List<String> fids = followers.stream().map(WebFollow::getFid).collect(Collectors.toList());
Page<WebNote> notePage = noteMapper.selectPage(new Page<>((int) currentPage, (int) pageSize),

@ -97,8 +97,7 @@ public class WebLikeOrCollectServiceImpl extends ServiceImpl<WebLikeOrCollectMap
@Override
public Page<LikeOrCollectVO> getNoticeLikeOrCollection(long currentPage, long pageSize) {
Page<LikeOrCollectVO> result = new Page<>();
String currentUid = WebUtils.getRequestHeader(UserConstant.USER_ID);
// String currentUid = AuthContextHolder.getUserId();
String currentUid = AuthContextHolder.getUserId();
Page<WebLikeOrCollect> likeOrCollectionPage = this.page(new Page<>((int) currentPage, (int) pageSize), new QueryWrapper<WebLikeOrCollect>().eq("publish_uid", currentUid).ne("uid", currentUid).orderByDesc("create_time"));
List<WebLikeOrCollect> likeOrCollectionList = likeOrCollectionPage.getRecords();

@ -5,9 +5,11 @@ import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hongshu.common.constant.UserConstant;
import com.hongshu.common.enums.ResultCodeEnum;
import com.hongshu.common.exception.web.HongshuException;
import com.hongshu.common.utils.ConvertUtils;
import com.hongshu.common.utils.WebUtils;
import com.hongshu.web.auth.AuthContextHolder;
import com.hongshu.web.domain.dto.NoteDTO;
import com.hongshu.web.domain.entity.*;
@ -17,7 +19,6 @@ import com.hongshu.web.mapper.WebUserMapper;
import com.hongshu.web.service.*;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
@ -58,9 +59,6 @@ public class WebNoteServiceImpl extends ServiceImpl<WebNoteMapper, WebNote> impl
@Autowired
WebNoteMapper noteMapper;
@Value("${oss.type}")
Integer type;
@NotNull
private StringBuilder getTags(WebNote note, NoteDTO noteDTO) {
@ -164,7 +162,7 @@ public class WebNoteServiceImpl extends ServiceImpl<WebNoteMapper, WebNote> impl
// 批量上传图片
List<String> dataList = null;
try {
dataList = ossService.saveBatch(files, type);
dataList = ossService.saveBatch(files);
} catch (Exception e) {
log.error("图片上传失败");
e.printStackTrace();
@ -195,7 +193,7 @@ public class WebNoteServiceImpl extends ServiceImpl<WebNoteMapper, WebNote> impl
for (Object o : array) {
pathArr.add((String) o);
}
ossService.batchDelete(pathArr, type);
ossService.batchDelete(pathArr);
// TODO 可以使用多线程优化,
// 删除点赞图片,评论,标签关系,收藏关系
likeOrCollectionService.remove(new QueryWrapper<WebLikeOrCollect>().eq("like_or_collection_id", noteId));
@ -229,7 +227,7 @@ public class WebNoteServiceImpl extends ServiceImpl<WebNoteMapper, WebNote> impl
WebNavbar parentCategory = categoryService.getById(note.getCpid());
List<String> dataList = null;
try {
dataList = ossService.saveBatch(files, type);
dataList = ossService.saveBatch(files);
} catch (Exception e) {
e.printStackTrace();
}
@ -241,7 +239,7 @@ public class WebNoteServiceImpl extends ServiceImpl<WebNoteMapper, WebNote> impl
for (Object o : array) {
pathArr.add((String) o);
}
ossService.batchDelete(pathArr, type);
ossService.batchDelete(pathArr);
String[] urlArr = Objects.requireNonNull(dataList).toArray(new String[dataList.size()]);
String newUrls = JSONUtil.toJsonStr(urlArr);

@ -1,9 +1,14 @@
package com.hongshu.web.service.impl;
import com.hongshu.common.config.HongshuConfig;
import com.hongshu.common.config.OssConfig;
import com.hongshu.common.utils.MinioUtil;
import com.hongshu.common.utils.QiniuUtil;
import com.hongshu.common.utils.file.FileUploadUtils;
import com.hongshu.common.utils.file.MimeTypeUtils;
import com.hongshu.web.service.IWebOssService;
import com.hongshu.web.websocket.factory.OssFactory;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
@ -23,51 +28,45 @@ public class WebOssServiceImpl implements IWebOssService {
QiniuUtil qiniuUtil;
@Autowired
MinioUtil minioUtil;
@Autowired
private OssConfig ossConfig;
/**
*
*
* @param file
* @param type (0: 1 2minio)
*/
@SneakyThrows
@Override
public String save(MultipartFile file, Integer type) {
OssFactory factory = null;
public String save(MultipartFile file) {
Integer type = ossConfig.getType();
switch (type) {
case 0:
// 本地上传图片
factory = new UploadFileToLoacl();
break;
// 本地存储
return FileUploadUtils.upload(HongshuConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
case 1:
// 七牛云
factory = new QiNiuYunUploadFile();
// qiniuUtil.uploadQiniu();
break;
// 七牛云存储
OssFactory qiniuFactory = new QiNiuYunUploadFile();
return qiniuFactory.save(file);
case 2:
// Minio
// minioUtil.uploadFile(file);
break;
// Minio存储
return minioUtil.uploadFile(file);
default:
break;
}
if (factory != null) {
return factory.save(file);
throw new IllegalArgumentException("不支持的存储类型: " + type);
}
return null;
}
/**
*
*
* @param files
* @param type
*/
@Override
public List<String> saveBatch(MultipartFile[] files, Integer type) {
public List<String> saveBatch(MultipartFile[] files) {
List<String> result = new ArrayList<>();
// 需要进行加锁,不然会出现多次添加
for (MultipartFile file : files) {
result.add(this.save(file, type));
result.add(this.save(file));
}
return result;
}
@ -76,11 +75,11 @@ public class WebOssServiceImpl implements IWebOssService {
*
*
* @param path
* @param type
*/
@Override
public void delete(String path, Integer type) {
public void delete(String path) {
OssFactory factory = null;
Integer type = ossConfig.getType();
switch (type) {
case 0:
// 本地上传图片
@ -99,12 +98,11 @@ public class WebOssServiceImpl implements IWebOssService {
*
*
* @param filePaths
* @param type
*/
@Override
public void batchDelete(List<String> filePaths, Integer type) {
public void batchDelete(List<String> filePaths) {
for (String path : filePaths) {
delete(path, type);
delete(path);
}
}
}

Loading…
Cancel
Save