package com.jsbd.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.google.common.io.Files;
import com.jsbd.config.TokenUtil;
import com.jsbd.model.vo.DtreeVo;
import com.jsbd.entity.FilePojo;
import com.jsbd.entity.User;
import com.jsbd.mapper.FileMapper;
import com.jsbd.service.FileService;
import com.jsbd.service.UserService;
import com.jsbd.utils.OSSClientUtil;
import com.jsbd.utils.Result;
import lombok.SneakyThrows;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@Service
public class FileServiceImpl extends ServiceImpl<FileMapper, FilePojo> implements FileService {
@Autowired
private TokenUtil tokenUtil;
@Autowired
private UserService userService;
@Resource
private OSSClientUtil ossClientUtil;
@Override
public Page<FilePojo> getList(Page page, FilePojo pojo) {
LambdaQueryWrapper<FilePojo> wrapper = new LambdaQueryWrapper<>();
String dirIds = pojo.getDirIds();
if(StringUtils.isNotEmpty(dirIds)){
dirIds = dirIds.substring(dirIds.lastIndexOf("/") + 1);
}
wrapper.eq(FilePojo::getParentId, StringUtils.isEmpty(dirIds) ? -1L : Long.parseLong(dirIds));
if(StringUtils.isNotEmpty(pojo.getName())){
wrapper.eq(FilePojo::getName,pojo.getName());
}
User tokenUser = userService.getById(tokenUtil.getId());
wrapper.eq(FilePojo::getUserId,tokenUser.getUserId());
wrapper.orderByDesc(FilePojo::getIsDir, FilePojo::getPutTime);
return baseMapper.selectPage(page,wrapper);
}
@Override
public Page<FilePojo> listBySource(Page page,String path) {
User tokenUser = userService.getById(tokenUtil.getId());
// 将路径按照分隔符拆分成目录名称数组
String[] names = path.split("/");
// 从根节点开始逐级向下遍历
Page<FilePojo> result = new Page<>();
FilePojo node = baseMapper.selectOne(
new LambdaQueryWrapper<FilePojo>()
.eq(FilePojo::getParentId, -1)
.eq(FilePojo::getName, names[1])
.eq(FilePojo::getUserId, tokenUser.getUserId())
);
if (node != null) {
for (int i = 2; i < names.length; i++) {
List<FilePojo> children = baseMapper.selectList(
new LambdaQueryWrapper<FilePojo>()
.eq(FilePojo::getParentId, node.getId())
.eq(FilePojo::getName, names[i])
.eq(FilePojo::getUserId, tokenUser.getUserId())
);
if (children.size() > 0) {
node = children.get(0);
} else {
return result;
}
}
result = baseMapper.selectPage(page,
new LambdaQueryWrapper<FilePojo>()
.eq(FilePojo::getParentId, node.getId())
.eq(FilePojo::getUserId, tokenUser.getUserId())
);
}
return result;
}
@Override
public boolean addFolder(FilePojo pojo) {
String dirId = pojo.getDirIds().substring(pojo.getDirIds().lastIndexOf("/") + 1);
if ("/".equals(dirId) || StringUtils.isEmpty(dirId)) {
pojo.setParentId(-1);
} else {
FilePojo p = baseMapper.selectById(Long.parseLong(dirId));
pojo.setParentId(p.getId());
}
pojo.setType("dir");
pojo.setIsDir(Boolean.TRUE);
pojo.setIsImg(Boolean.FALSE);
pojo.setCreateTime(new DateTime());
User tokenUser = userService.getById(tokenUtil.getId());
pojo.setUserId(tokenUser.getUserId());
//判断文件夹名称在当前目录中是否存在
Integer count = baseMapper.selectCount(
new LambdaQueryWrapper<FilePojo>()
.eq(FilePojo::getName, pojo.getName())
.eq(FilePojo::getIsDir, Boolean.TRUE)
.eq(FilePojo::getParentId, pojo.getParentId())
);
if (count > 0) {
throw new RuntimeException("当前目录名称已存在,请修改后重试!");
}
return baseMapper.insert(pojo)>0;
}
@Override
public boolean updateByName(FilePojo pojo) {
FilePojo p = baseMapper.selectById(pojo.getId());
Integer count = baseMapper.selectCount(
new LambdaQueryWrapper<FilePojo>()
.eq(FilePojo::getName, pojo.getName())
.eq(FilePojo::getIsDir, p.getIsDir())
.eq(FilePojo::getParentId, p.getParentId())
.ne(FilePojo::getId, p.getId())
);
if (count > 0) {
throw new RuntimeException("当前目录已存在该名称,请修改后重试!");
}
FilePojo updPojo = new FilePojo();
updPojo.setId(pojo.getId());
updPojo.setName(pojo.getName());
updPojo.setUpdateTime(new DateTime());
return baseMapper.updateById(updPojo) > 0;
}
@Transactional(rollbackFor = Exception.class)
@Override
public boolean deleteByIds(String ids) {
String[] idArray = ids.split(",");
List<String> idsList=Arrays.asList(idArray);
//idlist 区分文件还是文件夹
QueryWrapper<FilePojo> wrapper = new QueryWrapper<>();
wrapper.in("id", idsList);
List<FilePojo> filesAll = baseMapper.selectList(wrapper);
List<FilePojo> folder=new ArrayList<>();
List<FilePojo> files=new ArrayList<>();
filesAll.forEach(filePojo -> {
if(filePojo.getIsDir()){
folder.add(filePojo);
}else{
files.add(filePojo);
}
});
//查询所有文件夹下的子文件信息(oss服务器删除)
List<FilePojo> childFiles=new ArrayList<>();
folder.forEach(fo->{
listChildFiles(fo.getId(), childFiles);
});
//需要删除的文件信息
files.addAll(childFiles);
//删除阿里云服务器文件信息
for (FilePojo child : files) {
ossClientUtil.delete(child.getFileName());
}
//文件夹下的文件夹信息
List<FilePojo> fchids = new ArrayList<>();
filesAll.forEach(all->{
delChildFiles(all.getId(), fchids);
});
filesAll.addAll(fchids);
//删除表记录
List<Integer> deleteIds = new ArrayList<>();
filesAll.forEach(del->{
deleteIds.add(del.ge
Java实现网盘文件管理功能,调用阿里oss存储实现
需积分: 5 30 浏览量
2023-06-08
11:29:46
上传
评论
收藏 15KB ZIP 举报
拉登的小行星
- 粉丝: 5
- 资源: 9
最新资源
- 筷手引流工具.apk
- 论文(最终)_20240430235101.pdf
- 基于python编写的Keras深度学习框架开发,利用卷积神经网络CNN,快速识别图片并进行分类
- 最全空间计量实证方法(空间杜宾模型和检验以及结果解释文档).txt
- 5uonly.apk
- 蓝桥杯Python组的历年真题
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 前端开发技术实验报告:内含4四实验&实验报告
- Highlight Plus v20.0.1
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈