优化存储

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

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

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

@ -15,31 +15,27 @@ public interface IWebOssService {
* *
* *
* @param file * @param file
* @param type
*/ */
String save(MultipartFile file, Integer type); String save(MultipartFile file);
/** /**
* *
* *
* @param files * @param files
* @param type
*/ */
List<String> saveBatch(MultipartFile[] files, Integer type); List<String> saveBatch(MultipartFile[] files);
/** /**
* *
* *
* @param path * @param path
* @param type
*/ */
void delete(String path, Integer type); void delete(String path);
/** /**
* *
* *
* @param filePaths * @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 lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@ -36,8 +35,6 @@ public class SysMemberServiceImpl implements ISysMemberService {
private SysMemberMapper memberMapper; private SysMemberMapper memberMapper;
@Autowired @Autowired
private IWebOssService ossService; private IWebOssService ossService;
@Value("${oss.type}")
Integer type;
/** /**
@ -85,7 +82,7 @@ public class SysMemberServiceImpl implements ISysMemberService {
public int insertMember(WebUser user, MultipartFile file) { public int insertMember(WebUser user, MultipartFile file) {
// 上传头像 // 上传头像
if (ObjectUtils.isNotEmpty(file)) { if (ObjectUtils.isNotEmpty(file)) {
String avatar = ossService.save(file, type); String avatar = ossService.save(file);
user.setAvatar(avatar); user.setAvatar(avatar);
} }
user.setHsId(Long.valueOf(RandomUtil.randomNumbers(10))); user.setHsId(Long.valueOf(RandomUtil.randomNumbers(10)));
@ -108,7 +105,7 @@ public class SysMemberServiceImpl implements ISysMemberService {
WebUser webUser = memberMapper.selectById(user.getId()); WebUser webUser = memberMapper.selectById(user.getId());
// 更新头像 // 更新头像
if (ObjectUtils.isNotEmpty(file)) { if (ObjectUtils.isNotEmpty(file)) {
String avatar = ossService.save(file, type); String avatar = ossService.save(file);
user.setAvatar(avatar); user.setAvatar(avatar);
} }
String newPassword = webUser.getPassword(); String newPassword = webUser.getPassword();

@ -9,7 +9,6 @@ import com.hongshu.web.service.ISysNavbarService;
import com.hongshu.web.service.IWebOssService; import com.hongshu.web.service.IWebOssService;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@ -31,8 +30,6 @@ public class SysNavbarServiceImpl implements ISysNavbarService {
private IWebOssService ossService; private IWebOssService ossService;
@Autowired @Autowired
private SysNavbarMapper navbarMapper; private SysNavbarMapper navbarMapper;
@Value("${oss.type}")
Integer type;
/** /**
@ -81,7 +78,7 @@ public class SysNavbarServiceImpl implements ISysNavbarService {
*/ */
@Override @Override
public int insertNavbar(WebNavbar category, MultipartFile file) { public int insertNavbar(WebNavbar category, MultipartFile file) {
String normalCover = ossService.save(file, type); String normalCover = ossService.save(file);
category.setNormalCover(normalCover); category.setNormalCover(normalCover);
category.setCreator("system"); category.setCreator("system");
category.setCreateTime(new Date()); category.setCreateTime(new Date());
@ -98,7 +95,7 @@ public class SysNavbarServiceImpl implements ISysNavbarService {
@Override @Override
public int updateNavbar(WebNavbar category, MultipartFile file) { public int updateNavbar(WebNavbar category, MultipartFile file) {
if (ObjectUtils.isNotEmpty(file)) { if (ObjectUtils.isNotEmpty(file)) {
String normalCover = ossService.save(file, type); String normalCover = ossService.save(file);
category.setNormalCover(normalCover); category.setNormalCover(normalCover);
} }
category.setUpdater("system"); 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.WebNote;
import com.hongshu.web.domain.entity.WebUser; import com.hongshu.web.domain.entity.WebUser;
import com.hongshu.web.domain.vo.NoteSearchVO; import com.hongshu.web.domain.vo.NoteSearchVO;
import com.hongshu.web.mapper.WebUserMapper;
import com.hongshu.web.mapper.SysNavbarMapper; import com.hongshu.web.mapper.SysNavbarMapper;
import com.hongshu.web.mapper.SysNoteMapper; import com.hongshu.web.mapper.SysNoteMapper;
import com.hongshu.web.mapper.WebUserMapper;
import com.hongshu.web.service.ISysNoteService; import com.hongshu.web.service.ISysNoteService;
import com.hongshu.web.service.IWebEsNoteService; import com.hongshu.web.service.IWebEsNoteService;
import com.hongshu.web.service.IWebOssService; import com.hongshu.web.service.IWebOssService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@ -49,8 +48,6 @@ public class SysNoteServiceImpl implements ISysNoteService {
private SysNavbarMapper navbarMapper; private SysNavbarMapper navbarMapper;
@Autowired @Autowired
private IWebOssService ossService; private IWebOssService ossService;
@Value("${oss.type}")
Integer type;
/** /**
@ -118,7 +115,7 @@ public class SysNoteServiceImpl implements ISysNoteService {
public int insertNote(WebNote note, MultipartFile file) { public int insertNote(WebNote note, MultipartFile file) {
// 上传头像 // 上传头像
if (ObjectUtils.isNotEmpty(file)) { if (ObjectUtils.isNotEmpty(file)) {
String noteCover = ossService.save(file, type); String noteCover = ossService.save(file);
note.setNoteCover(noteCover); note.setNoteCover(noteCover);
} }
note.setCreator("System"); note.setCreator("System");
@ -136,7 +133,7 @@ public class SysNoteServiceImpl implements ISysNoteService {
public int updateNote(WebNote note, MultipartFile file) { public int updateNote(WebNote note, MultipartFile file) {
// 上传头像 // 上传头像
if (ObjectUtils.isNotEmpty(file)) { if (ObjectUtils.isNotEmpty(file)) {
String noteCover = ossService.save(file, type); String noteCover = ossService.save(file);
note.setNoteCover(noteCover); note.setNoteCover(noteCover);
} }
note.setUpdater("System"); note.setUpdater("System");

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

@ -187,8 +187,7 @@ public class WebCommentServiceImpl extends ServiceImpl<WebCommentMapper, WebComm
@Override @Override
public IPage<CommentVO> getNoticeComment(long currentPage, long pageSize) { public IPage<CommentVO> getNoticeComment(long currentPage, long pageSize) {
Page<CommentVO> result = new Page<>(); 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")); 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) { public Page<TrendVO> getFollowTrend(long currentPage, long pageSize) {
Page<TrendVO> page = new Page<>(); 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<WebFollow> followers = this.list(new QueryWrapper<WebFollow>().eq("uid", currentUid));
List<String> fids = followers.stream().map(WebFollow::getFid).collect(Collectors.toList()); List<String> fids = followers.stream().map(WebFollow::getFid).collect(Collectors.toList());
fids.add(currentUid); fids.add(currentUid);
@ -107,7 +106,7 @@ public class WebFollowServiceImpl extends ServiceImpl<WebFollowMapper, WebFollow
@Override @Override
public Page<TrendVO> getFollowList(long currentPage, long pageSize) { public Page<TrendVO> getFollowList(long currentPage, long pageSize) {
Page<TrendVO> page = new Page<>(); 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<WebFollow> followers = this.list(new QueryWrapper<WebFollow>().eq("uid", currentUid));
List<String> fids = followers.stream().map(WebFollow::getFid).collect(Collectors.toList()); List<String> fids = followers.stream().map(WebFollow::getFid).collect(Collectors.toList());
Page<WebNote> notePage = noteMapper.selectPage(new Page<>((int) currentPage, (int) pageSize), Page<WebNote> notePage = noteMapper.selectPage(new Page<>((int) currentPage, (int) pageSize),

@ -97,8 +97,7 @@ public class WebLikeOrCollectServiceImpl extends ServiceImpl<WebLikeOrCollectMap
@Override @Override
public Page<LikeOrCollectVO> getNoticeLikeOrCollection(long currentPage, long pageSize) { public Page<LikeOrCollectVO> getNoticeLikeOrCollection(long currentPage, long pageSize) {
Page<LikeOrCollectVO> result = new Page<>(); 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")); 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(); 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.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hongshu.common.constant.UserConstant;
import com.hongshu.common.enums.ResultCodeEnum; import com.hongshu.common.enums.ResultCodeEnum;
import com.hongshu.common.exception.web.HongshuException; import com.hongshu.common.exception.web.HongshuException;
import com.hongshu.common.utils.ConvertUtils; import com.hongshu.common.utils.ConvertUtils;
import com.hongshu.common.utils.WebUtils;
import com.hongshu.web.auth.AuthContextHolder; import com.hongshu.web.auth.AuthContextHolder;
import com.hongshu.web.domain.dto.NoteDTO; import com.hongshu.web.domain.dto.NoteDTO;
import com.hongshu.web.domain.entity.*; import com.hongshu.web.domain.entity.*;
@ -17,7 +19,6 @@ import com.hongshu.web.mapper.WebUserMapper;
import com.hongshu.web.service.*; import com.hongshu.web.service.*;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@ -58,9 +59,6 @@ public class WebNoteServiceImpl extends ServiceImpl<WebNoteMapper, WebNote> impl
@Autowired @Autowired
WebNoteMapper noteMapper; WebNoteMapper noteMapper;
@Value("${oss.type}")
Integer type;
@NotNull @NotNull
private StringBuilder getTags(WebNote note, NoteDTO noteDTO) { private StringBuilder getTags(WebNote note, NoteDTO noteDTO) {
@ -164,7 +162,7 @@ public class WebNoteServiceImpl extends ServiceImpl<WebNoteMapper, WebNote> impl
// 批量上传图片 // 批量上传图片
List<String> dataList = null; List<String> dataList = null;
try { try {
dataList = ossService.saveBatch(files, type); dataList = ossService.saveBatch(files);
} catch (Exception e) { } catch (Exception e) {
log.error("图片上传失败"); log.error("图片上传失败");
e.printStackTrace(); e.printStackTrace();
@ -195,7 +193,7 @@ public class WebNoteServiceImpl extends ServiceImpl<WebNoteMapper, WebNote> impl
for (Object o : array) { for (Object o : array) {
pathArr.add((String) o); pathArr.add((String) o);
} }
ossService.batchDelete(pathArr, type); ossService.batchDelete(pathArr);
// TODO 可以使用多线程优化, // TODO 可以使用多线程优化,
// 删除点赞图片,评论,标签关系,收藏关系 // 删除点赞图片,评论,标签关系,收藏关系
likeOrCollectionService.remove(new QueryWrapper<WebLikeOrCollect>().eq("like_or_collection_id", noteId)); 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()); WebNavbar parentCategory = categoryService.getById(note.getCpid());
List<String> dataList = null; List<String> dataList = null;
try { try {
dataList = ossService.saveBatch(files, type); dataList = ossService.saveBatch(files);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -241,7 +239,7 @@ public class WebNoteServiceImpl extends ServiceImpl<WebNoteMapper, WebNote> impl
for (Object o : array) { for (Object o : array) {
pathArr.add((String) o); pathArr.add((String) o);
} }
ossService.batchDelete(pathArr, type); ossService.batchDelete(pathArr);
String[] urlArr = Objects.requireNonNull(dataList).toArray(new String[dataList.size()]); String[] urlArr = Objects.requireNonNull(dataList).toArray(new String[dataList.size()]);
String newUrls = JSONUtil.toJsonStr(urlArr); String newUrls = JSONUtil.toJsonStr(urlArr);
@ -285,7 +283,7 @@ public class WebNoteServiceImpl extends ServiceImpl<WebNoteMapper, WebNote> impl
note.setPinned("0"); note.setPinned("0");
} else { } else {
List<WebNote> noteList = this.list(new QueryWrapper<WebNote>().eq("uid", currentUid)); List<WebNote> noteList = this.list(new QueryWrapper<WebNote>().eq("uid", currentUid));
long count = noteList.stream().filter(item -> "1".equals(item.getPinned()) ).count(); long count = noteList.stream().filter(item -> "1".equals(item.getPinned())).count();
if (count >= 3) { if (count >= 3) {
throw new HongshuException("最多只能置顶3个笔记"); throw new HongshuException("最多只能置顶3个笔记");
} }

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

Loading…
Cancel
Save