云文件管理系统上传组件优化
This commit is contained in:
@@ -122,4 +122,26 @@ public class UserController {
|
||||
userService.updateProfile(principal.getUserId(), nickname, signature, phone, email);
|
||||
return ResponseEntity.ok(Map.of("message", "更新成功"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改密码
|
||||
*/
|
||||
@PutMapping("/password")
|
||||
public ResponseEntity<?> changePassword(
|
||||
@AuthenticationPrincipal UserPrincipal principal,
|
||||
@RequestBody Map<String, String> request) {
|
||||
String oldPassword = request.get("oldPassword");
|
||||
String newPassword = request.get("newPassword");
|
||||
|
||||
if (oldPassword == null || oldPassword.isEmpty() || newPassword == null || newPassword.isEmpty()) {
|
||||
return ResponseEntity.badRequest().body(Map.of("message", "请填写完整信息"));
|
||||
}
|
||||
|
||||
try {
|
||||
userService.changePassword(principal.getUserId(), oldPassword, newPassword);
|
||||
return ResponseEntity.ok(Map.of("message", "密码修改成功"));
|
||||
} catch (Exception e) {
|
||||
return ResponseEntity.badRequest().body(Map.of("message", e.getMessage()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,6 +69,13 @@ public class FileService {
|
||||
wrapper.eq(FileEntity::getUserId, userId)
|
||||
.eq(FileEntity::getIsDeleted, 1)
|
||||
.orderByDesc(FileEntity::getDeletedAt);
|
||||
|
||||
if (folderId != null) {
|
||||
wrapper.eq(FileEntity::getFolderId, folderId);
|
||||
} else {
|
||||
wrapper.isNull(FileEntity::getFolderId);
|
||||
}
|
||||
|
||||
return fileMapper.selectList(wrapper);
|
||||
}
|
||||
|
||||
@@ -107,10 +114,21 @@ public class FileService {
|
||||
FileEntity file = fileMapper.selectById(id);
|
||||
if (file == null || !file.getUserId().equals(userId)) return;
|
||||
|
||||
// 检查父文件夹是否在回收站,如果是,将文件移到根目录
|
||||
Long folderId = file.getFolderId();
|
||||
if (folderId != null) {
|
||||
FileEntity parentFolder = fileMapper.selectById(folderId);
|
||||
if (parentFolder != null && parentFolder.getIsDeleted() == 1) {
|
||||
// 父文件夹在回收站,将文件移到根目录
|
||||
folderId = null;
|
||||
}
|
||||
}
|
||||
|
||||
LambdaUpdateWrapper<FileEntity> wrapper = new LambdaUpdateWrapper<>();
|
||||
wrapper.eq(FileEntity::getId, id)
|
||||
.set(FileEntity::getIsDeleted, 0)
|
||||
.set(FileEntity::getDeletedAt, null);
|
||||
.set(FileEntity::getDeletedAt, null)
|
||||
.set(FileEntity::getFolderId, folderId);
|
||||
fileMapper.update(null, wrapper);
|
||||
|
||||
// 如果是文件夹,递归还原所有子文件
|
||||
@@ -144,9 +162,28 @@ public class FileService {
|
||||
FileEntity file = fileMapper.selectById(id);
|
||||
if (file == null || !file.getUserId().equals(userId)) return;
|
||||
|
||||
// 如果是文件夹,先递归删除所有子文件
|
||||
// 如果是文件夹,检查是否有未删除的子文件
|
||||
if (file.getIsFolder() != null && file.getIsFolder() == 1) {
|
||||
deleteChildrenPermanently(id, userId);
|
||||
Long undeletedCount = fileMapper.selectCount(
|
||||
new LambdaQueryWrapper<FileEntity>()
|
||||
.eq(FileEntity::getFolderId, id)
|
||||
.eq(FileEntity::getIsDeleted, 0)
|
||||
.eq(FileEntity::getUserId, userId)
|
||||
);
|
||||
if (undeletedCount != null && undeletedCount > 0) {
|
||||
throw new RuntimeException("请先处理该目录下的子文件后重试");
|
||||
}
|
||||
|
||||
// 检查是否有已删除的子文件(在回收站里的)
|
||||
Long deletedChildrenCount = fileMapper.selectCount(
|
||||
new LambdaQueryWrapper<FileEntity>()
|
||||
.eq(FileEntity::getFolderId, id)
|
||||
.eq(FileEntity::getIsDeleted, 1)
|
||||
.eq(FileEntity::getUserId, userId)
|
||||
);
|
||||
if (deletedChildrenCount != null && deletedChildrenCount > 0) {
|
||||
throw new RuntimeException("请先处理该目录下的子文件后重试");
|
||||
}
|
||||
}
|
||||
|
||||
// 删除当前文件的物理文件并扣减存储
|
||||
@@ -164,32 +201,6 @@ public class FileService {
|
||||
fileMapper.deleteById(id);
|
||||
}
|
||||
|
||||
private void deleteChildrenPermanently(Long parentFolderId, Long userId) {
|
||||
List<FileEntity> children = fileMapper.selectList(
|
||||
new LambdaQueryWrapper<FileEntity>()
|
||||
.eq(FileEntity::getFolderId, parentFolderId)
|
||||
.eq(FileEntity::getIsDeleted, 1)
|
||||
.eq(FileEntity::getUserId, userId)
|
||||
);
|
||||
for (FileEntity child : children) {
|
||||
if (child.getIsFolder() != null && child.getIsFolder() == 1) {
|
||||
deleteChildrenPermanently(child.getId(), userId);
|
||||
}
|
||||
if (child.getPath() != null && !child.getPath().isEmpty()) {
|
||||
try {
|
||||
Path filePath = Paths.get(storagePath).toAbsolutePath().resolve("files").resolve(child.getPath());
|
||||
Files.deleteIfExists(filePath);
|
||||
} catch (IOException e) {
|
||||
// ignore
|
||||
}
|
||||
if (child.getSize() != null && child.getSize() > 0) {
|
||||
userService.decreaseStorage(userId, child.getSize());
|
||||
}
|
||||
}
|
||||
fileMapper.deleteById(child.getId());
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void emptyTrash(Long userId) {
|
||||
List<FileEntity> trashFiles = getTrashFiles(userId, null);
|
||||
|
||||
@@ -147,4 +147,25 @@ public class UserService {
|
||||
userMapper.updateById(user);
|
||||
}
|
||||
}
|
||||
|
||||
public void changePassword(Long userId, String oldPassword, String newPassword) {
|
||||
User user = userMapper.selectById(userId);
|
||||
if (user == null) {
|
||||
throw new RuntimeException("用户不存在");
|
||||
}
|
||||
|
||||
// 验证旧密码
|
||||
if (!passwordEncoder.matches(oldPassword, user.getPassword())) {
|
||||
throw new RuntimeException("原密码错误");
|
||||
}
|
||||
|
||||
// 检查新密码长度
|
||||
if (newPassword == null || newPassword.length() < 6) {
|
||||
throw new RuntimeException("新密码长度不能少于6位");
|
||||
}
|
||||
|
||||
// 更新密码
|
||||
user.setPassword(passwordEncoder.encode(newPassword));
|
||||
userMapper.updateById(user);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user