file
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package com.senmesh.sfile.service.impl;
|
||||
|
||||
import com.senmesh.common.util.ListUtil;
|
||||
import com.senmesh.common.util.UserContextHolder;
|
||||
import com.senmesh.sfile.convert.FileCategoryConvert;
|
||||
import com.senmesh.sfile.dao.FileCategoryMapper;
|
||||
import com.senmesh.sfile.dto.FileCategoryCreateDTO;
|
||||
import com.senmesh.sfile.dto.FileCategoryDTO;
|
||||
import com.senmesh.sfile.dto.FileCategoryModifyDTO;
|
||||
import com.senmesh.sfile.dto.FileCategoryQueryDTO;
|
||||
import com.senmesh.sfile.entity.FileCategory;
|
||||
import com.senmesh.sfile.service.FileCategoryService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 文件种类ServiceImpl
|
||||
*/
|
||||
@Service
|
||||
public class FileCategoryServiceImpl implements FileCategoryService {
|
||||
|
||||
@Autowired
|
||||
private FileCategoryMapper fileCategoryMapper;
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Long create(FileCategoryCreateDTO createDTO) {
|
||||
FileCategory fileCategory = FileCategoryConvert.INSTANCE.toEntity(createDTO);
|
||||
fileCategoryMapper.saveEntity(fileCategory);
|
||||
return fileCategory.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void modify(FileCategoryModifyDTO modifyDTO) {
|
||||
FileCategory fileCategory = FileCategoryConvert.INSTANCE.toEntity(modifyDTO);
|
||||
fileCategoryMapper.updateEntitySelective(fileCategory);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void remove(Long id) {
|
||||
fileCategoryMapper.deleteEntity(FileCategory.builder().id(id).build(),
|
||||
UserContextHolder.getLoginUserId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FileCategoryDTO> list(FileCategoryQueryDTO queryDTO) {
|
||||
queryDTO.disablePage();
|
||||
return fileCategoryMapper.list(queryDTO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void page(FileCategoryQueryDTO queryDTO) {
|
||||
fileCategoryMapper.list(queryDTO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileCategoryDTO find(FileCategoryQueryDTO queryDTO) {
|
||||
List<FileCategoryDTO> fileCategoryDTOs = this.list(queryDTO);
|
||||
return ListUtil.first(fileCategoryDTOs);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.senmesh.sfile.service.impl;
|
||||
|
||||
import com.senmesh.common.util.UserContextHolder;
|
||||
import com.senmesh.sfile.constant.FileStatusEnum;
|
||||
import com.senmesh.sfile.constant.RootTypeEnum;
|
||||
import com.senmesh.sfile.dto.UserFileCreateDTO;
|
||||
import com.senmesh.sfile.service.NormalUserFileService;
|
||||
import com.senmesh.sfile.service.UserFileService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* 普通用户文件ServiceImpl
|
||||
*/
|
||||
@Service
|
||||
public class NormalUserFileServiceImpl implements NormalUserFileService {
|
||||
|
||||
@Autowired
|
||||
private UserFileService userFileService;
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Long create(UserFileCreateDTO createDTO) {
|
||||
createDTO.setUserId(UserContextHolder.getExistLoginUserId());
|
||||
// 设置根类型为普通
|
||||
createDTO.setRootType(RootTypeEnum.NORMAL.getType());
|
||||
// TODO 扫描文件审核
|
||||
createDTO.setFileStatus(FileStatusEnum.NORMAL.getStatus());
|
||||
// 创建用户文件
|
||||
return userFileService.create(createDTO);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.senmesh.sfile.service.impl;
|
||||
|
||||
import com.senmesh.common.util.UserContextHolder;
|
||||
import com.senmesh.sfile.constant.FileStatusEnum;
|
||||
import com.senmesh.sfile.constant.RootTypeEnum;
|
||||
import com.senmesh.sfile.dto.ExpandUserFileCreateDTO;
|
||||
import com.senmesh.sfile.dto.ShareUserFileCreateDTO;
|
||||
import com.senmesh.sfile.service.ShareUserFileService;
|
||||
import com.senmesh.sfile.service.UserFileService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 分享用户文件ServiceImpl
|
||||
*/
|
||||
@Service
|
||||
public class ShareUserFileServiceImpl implements ShareUserFileService {
|
||||
|
||||
@Autowired
|
||||
private UserFileService userFileService;
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Long create(ShareUserFileCreateDTO createDTO) {
|
||||
Long userFileId = createDTO.getUserFileId();
|
||||
Long userId = UserContextHolder.getExistLoginUserId();
|
||||
Integer validDays = createDTO.getValidDays();
|
||||
|
||||
// 根据有效天数计算过期时间
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(new Date());
|
||||
calendar.add(Calendar.DAY_OF_MONTH, validDays);
|
||||
Date endTime = calendar.getTime();
|
||||
|
||||
// 调用UserFileService的重载create方法,根据普通文件创建分享文件
|
||||
ExpandUserFileCreateDTO expandCreateDTO = ExpandUserFileCreateDTO.builder()
|
||||
.userFileId(userFileId)
|
||||
.userId(userId)
|
||||
.rootType(RootTypeEnum.SHARE.getType())
|
||||
.fileStatus(FileStatusEnum.NORMAL.getStatus())
|
||||
.endTime(endTime)
|
||||
.build();
|
||||
return userFileService.create(expandCreateDTO);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,261 @@
|
||||
package com.senmesh.sfile.service.impl;
|
||||
|
||||
import com.senmesh.common.constant.CommonConstants;
|
||||
import com.senmesh.common.exception.util.Assert;
|
||||
import com.senmesh.common.util.ListUtil;
|
||||
import com.senmesh.common.util.UserContextHolder;
|
||||
import com.senmesh.sfile.constant.FileTypeEnum;
|
||||
import com.senmesh.sfile.constant.RootTypeEnum;
|
||||
import com.senmesh.sfile.convert.UserFileConvert;
|
||||
import com.senmesh.sfile.dao.UserFileMapper;
|
||||
import com.senmesh.sfile.dto.ExpandUserFileCreateDTO;
|
||||
import com.senmesh.sfile.dto.UserFileCreateDTO;
|
||||
import com.senmesh.sfile.dto.UserFileDTO;
|
||||
import com.senmesh.sfile.dto.UserFileModifyDTO;
|
||||
import com.senmesh.sfile.dto.UserFileModifyStatusDTO;
|
||||
import com.senmesh.sfile.dto.UserFileQueryDTO;
|
||||
import com.senmesh.sfile.entity.UserFile;
|
||||
import com.senmesh.sfile.service.UserFileService;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 用户文件ServiceImpl
|
||||
*/
|
||||
@Service
|
||||
public class UserFileServiceImpl implements UserFileService {
|
||||
|
||||
@Autowired
|
||||
private UserFileMapper userFileMapper;
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Long create(UserFileCreateDTO createDTO) {
|
||||
Long parentId = createDTO.getParentId();
|
||||
String parentPath = createDTO.getParentPath();
|
||||
Long userId = createDTO.getUserId();
|
||||
String rootType = createDTO.getRootType();
|
||||
|
||||
// 1. 校验文件类型时fileCode不能为空
|
||||
if (FileTypeEnum.isFile(createDTO.getFileType())) {
|
||||
Assert.notBlank(createDTO.getFileCode(), "文件编码不能为空");
|
||||
}
|
||||
|
||||
// 2. 校验父节点(如果不是根目录)
|
||||
if (parentId != null && parentId != 0L) {
|
||||
// 如果没有提供parentPath,需要查询父节点
|
||||
if (StringUtils.isBlank(parentPath)) {
|
||||
UserFileDTO parentFile = this.findOne(UserFileQueryDTO.builder()
|
||||
.id(parentId)
|
||||
.userId(userId)
|
||||
.rootType(rootType)
|
||||
.build());
|
||||
Assert.isTrue(FileTypeEnum.isFolder(parentFile.getFileType()), "父节点必须是文件夹");
|
||||
// 获取父路径用于后续拼接
|
||||
parentPath = parentFile.getPath();
|
||||
}
|
||||
// 如果提供了parentPath,信任调用方,不做校验
|
||||
}
|
||||
|
||||
// 3. 插入数据获取ID
|
||||
UserFile userFile = UserFileConvert.INSTANCE.toEntity(createDTO);
|
||||
userFileMapper.saveEntity(userFile);
|
||||
Long newId = userFile.getId();
|
||||
|
||||
// 4. 构建path
|
||||
String path = StringUtils.isNotBlank(parentPath)
|
||||
? parentPath + CommonConstants.COMMA + newId
|
||||
: String.valueOf(newId);
|
||||
|
||||
// 5. 更新path
|
||||
userFile.setPath(path);
|
||||
userFileMapper.updateEntitySelective(userFile);
|
||||
|
||||
return newId;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Long create(ExpandUserFileCreateDTO expandCreateDTO) {
|
||||
Long userFileId = expandCreateDTO.getUserFileId();
|
||||
Long userId = expandCreateDTO.getUserId();
|
||||
String rootType = expandCreateDTO.getRootType();
|
||||
|
||||
// 1. 查询普通文件(限制rootType为普通)
|
||||
UserFileDTO normalFile = this.findOne(UserFileQueryDTO.builder()
|
||||
.id(userFileId)
|
||||
.userId(userId)
|
||||
.rootType(RootTypeEnum.NORMAL.getType())
|
||||
.build());
|
||||
|
||||
// 2. 创建文件夹,文件名 = 原文件名 + 当前时间
|
||||
String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
|
||||
String folderName = normalFile.getFileName() + "_" + timestamp;
|
||||
|
||||
UserFileCreateDTO folderCreateDTO = UserFileCreateDTO.builder()
|
||||
.userId(userId)
|
||||
.rootType(rootType)
|
||||
.fileType(FileTypeEnum.FOLDER.getType())
|
||||
.fileName(folderName)
|
||||
.parentId(0L)
|
||||
.endTime(expandCreateDTO.getEndTime())
|
||||
.accessCode(expandCreateDTO.getAccessCode())
|
||||
.build();
|
||||
Long folderId = this.create(folderCreateDTO);
|
||||
String folderPath = String.valueOf(folderId);
|
||||
|
||||
// 3. 查询需要复制的文件列表
|
||||
// 查询条件:path LIKE 'xxx%' AND fileType = '1'
|
||||
// 如果是文件:会查到自己
|
||||
// 如果是文件夹:会查到其下所有文件类型的子文件
|
||||
UserFileQueryDTO queryDTO = UserFileQueryDTO.builder()
|
||||
.userId(userId)
|
||||
.rootType(RootTypeEnum.NORMAL.getType())
|
||||
.fileType(FileTypeEnum.FILE.getType())
|
||||
.pathLike(normalFile.getPath())
|
||||
.build();
|
||||
queryDTO.disablePage();
|
||||
List<UserFileDTO> filesToCopy = this.list(queryDTO);
|
||||
|
||||
// 4. 在文件夹下创建文件对象
|
||||
for (UserFileDTO fileDTO : filesToCopy) {
|
||||
UserFileCreateDTO fileCreateDTO = UserFileCreateDTO.builder()
|
||||
.userId(userId)
|
||||
.rootType(rootType)
|
||||
.fileType(FileTypeEnum.FILE.getType())
|
||||
.fileName(fileDTO.getFileName())
|
||||
.fileCode(fileDTO.getFileCode())
|
||||
.parentPath(folderPath)
|
||||
.build();
|
||||
this.create(fileCreateDTO);
|
||||
}
|
||||
|
||||
return folderId;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void modify(UserFileModifyDTO modifyDTO) {
|
||||
Long id = modifyDTO.getId();
|
||||
Long userId = modifyDTO.getUserId();
|
||||
Long newParentId = modifyDTO.getParentId();
|
||||
String rootType = modifyDTO.getRootType();
|
||||
|
||||
// 1. 根据id+userId+rootType查询文件是否存在
|
||||
UserFileDTO existFile = this.findOne(UserFileQueryDTO.builder()
|
||||
.id(id)
|
||||
.userId(userId)
|
||||
.rootType(rootType)
|
||||
.build());
|
||||
|
||||
// 2. 判断是否需要更新path(parentId有值且与现存的parentId不一致)
|
||||
boolean needUpdatePath = newParentId != null
|
||||
&& !newParentId.equals(existFile.getParentId());
|
||||
|
||||
if (needUpdatePath) {
|
||||
// 2.1 校验新父节点(如果不是根目录)
|
||||
if (newParentId != 0L) {
|
||||
UserFileDTO newParentFile = this.findOne(UserFileQueryDTO.builder()
|
||||
.id(newParentId)
|
||||
.userId(userId)
|
||||
.rootType(rootType)
|
||||
.build());
|
||||
Assert.isTrue(FileTypeEnum.isFolder(newParentFile.getFileType()), "父节点必须是文件夹");
|
||||
// 校验新父节点的rootType必须与当前文件相同
|
||||
Assert.isTrue(existFile.getRootType().equals(newParentFile.getRootType()),
|
||||
"不能将文件移动到不同类型的文件夹");
|
||||
}
|
||||
|
||||
// 2.2 在SQL中一次性更新当前节点和所有子孙节点的path(SQL内部会限制相同userId和rootType)
|
||||
userFileMapper.updatePathByParentIdChange(id, newParentId);
|
||||
}
|
||||
|
||||
// 3. 更新文件信息
|
||||
UserFile userFile = UserFileConvert.INSTANCE.toEntity(modifyDTO);
|
||||
userFileMapper.updateEntitySelective(userFile);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void modifyStatus(UserFileModifyStatusDTO modifyStatusDTO) {
|
||||
Long id = modifyStatusDTO.getId();
|
||||
String oldStatus = modifyStatusDTO.getOldStatus();
|
||||
String newStatus = modifyStatusDTO.getNewStatus();
|
||||
Long operateUserId = UserContextHolder.getLoginUserId();
|
||||
|
||||
// 更新文件状态及其所有子文件状态(SQL内部会校验oldStatus并限制相同userId和rootType)
|
||||
userFileMapper.updateStatusWithChildren(id, oldStatus, newStatus, operateUserId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void modifyStatus(UserFileModifyStatusDTO modifyStatusDTO, Long userId) {
|
||||
Assert.notNull(userId, "用户ID不能为空");
|
||||
Long id = modifyStatusDTO.getId();
|
||||
|
||||
// 校验文件是否存在且属于该用户
|
||||
this.findOne(UserFileQueryDTO.builder()
|
||||
.id(id)
|
||||
.userId(userId)
|
||||
.build());
|
||||
|
||||
// 调用原方法修改状态
|
||||
modifyStatus(modifyStatusDTO);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void remove(Long id) {
|
||||
// 删除文件及其所有子文件(SQL内部会限制相同userId和rootType)
|
||||
userFileMapper.deleteFileWithChildren(id, UserContextHolder.getLoginUserId());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void remove(Long id, Long userId) {
|
||||
Assert.notNull(id, "文件ID不能为空");
|
||||
Assert.notNull(userId, "用户ID不能为空");
|
||||
|
||||
// 校验文件是否存在且属于该用户
|
||||
this.findOne(UserFileQueryDTO.builder()
|
||||
.id(id)
|
||||
.userId(userId)
|
||||
.build());
|
||||
|
||||
// 删除文件及其所有子文件(SQL内部会限制相同userId和rootType)
|
||||
remove(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UserFileDTO> list(UserFileQueryDTO queryDTO) {
|
||||
queryDTO.disablePage();
|
||||
return userFileMapper.list(queryDTO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void page(UserFileQueryDTO queryDTO) {
|
||||
userFileMapper.list(queryDTO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserFileDTO find(UserFileQueryDTO queryDTO) {
|
||||
List<UserFileDTO> userFileDTOs = this.list(queryDTO);
|
||||
return ListUtil.first(userFileDTOs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserFileDTO findOne(UserFileQueryDTO queryDTO) {
|
||||
UserFileDTO userFileDTO = this.find(queryDTO);
|
||||
Assert.notNull(userFileDTO, "文件不存在");
|
||||
return userFileDTO;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
package com.senmesh.sfile.service.impl;
|
||||
|
||||
import com.senmesh.common.exception.util.Assert;
|
||||
import com.senmesh.common.util.ListUtil;
|
||||
import com.senmesh.common.util.UserContextHolder;
|
||||
import com.senmesh.frame.redis.dto.LockDTO;
|
||||
import com.senmesh.frame.redis.redisson.RedissonUtils;
|
||||
import com.senmesh.sfile.convert.UserSpaceConvert;
|
||||
import com.senmesh.sfile.dao.UserSpaceMapper;
|
||||
import com.senmesh.sfile.dto.UserSpaceCreateDTO;
|
||||
import com.senmesh.sfile.dto.UserSpaceDTO;
|
||||
import com.senmesh.sfile.dto.UserSpaceModifyDTO;
|
||||
import com.senmesh.sfile.dto.UserSpaceQueryDTO;
|
||||
import com.senmesh.sfile.entity.UserSpace;
|
||||
import com.senmesh.sfile.service.UserSpaceService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 用户空间ServiceImpl
|
||||
*/
|
||||
@Service
|
||||
public class UserSpaceServiceImpl implements UserSpaceService {
|
||||
|
||||
@Autowired
|
||||
private UserSpaceMapper userSpaceMapper;
|
||||
|
||||
/**
|
||||
* 默认总容量:5GB
|
||||
*/
|
||||
private static final Long DEFAULT_TOTAL_SIZE = 5L * 1024 * 1024 * 1024;
|
||||
|
||||
|
||||
public static LockDTO getLockDTO(Long userId) {
|
||||
Assert.notNull(userId, "用户id不能为空");
|
||||
return LockDTO.builder().methodName("userspace").tryLockWaitTime(0L)
|
||||
.arg(userId.toString())
|
||||
.failMsg("该用户空间正在被操作")
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Long create(UserSpaceCreateDTO createDTO) {
|
||||
Long userId = createDTO.getUserId();
|
||||
UserSpaceQueryDTO queryDTO = UserSpaceQueryDTO.builder().userId(userId).build();
|
||||
|
||||
return RedissonUtils.lockAndSave(getLockDTO(userId),
|
||||
() -> this.find(queryDTO), () -> {
|
||||
UserSpace userSpace = UserSpaceConvert.INSTANCE.toEntity(createDTO);
|
||||
userSpace.setTotalSize(DEFAULT_TOTAL_SIZE);
|
||||
userSpace.setUsedSize(0L);
|
||||
userSpaceMapper.saveEntity(userSpace);
|
||||
return userSpace.getId();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void modify(UserSpaceModifyDTO modifyDTO) {
|
||||
Long userId = modifyDTO.getUserId();
|
||||
UserSpaceQueryDTO queryDTO = UserSpaceQueryDTO.builder().userId(userId).build();
|
||||
UserSpaceDTO userSpaceDTO = this.find(queryDTO);
|
||||
Assert.notNull(userSpaceDTO, "用户空间不存在");
|
||||
|
||||
UserSpace userSpace = UserSpace.builder()
|
||||
.id(userSpaceDTO.getId())
|
||||
.usedSize(modifyDTO.getUsedSize())
|
||||
.build();
|
||||
userSpaceMapper.updateEntitySelective(userSpace);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void remove(Long id) {
|
||||
userSpaceMapper.deleteEntity(UserSpace.builder().id(id).build(),
|
||||
UserContextHolder.getLoginUserId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UserSpaceDTO> list(UserSpaceQueryDTO queryDTO) {
|
||||
queryDTO.disablePage();
|
||||
return userSpaceMapper.list(queryDTO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void page(UserSpaceQueryDTO queryDTO) {
|
||||
userSpaceMapper.list(queryDTO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserSpaceDTO find(UserSpaceQueryDTO queryDTO) {
|
||||
List<UserSpaceDTO> userSpaceDTOs = this.list(queryDTO);
|
||||
return ListUtil.first(userSpaceDTOs);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user