云文件系统初始化

This commit is contained in:
2026-04-01 23:03:55 +08:00
parent 3a20f6e7ed
commit 32fc36cae1
4 changed files with 142 additions and 43 deletions

View File

@@ -5,6 +5,7 @@ import com.filesystem.entity.FileShare;
import com.filesystem.security.UserPrincipal;
import com.filesystem.service.FileService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
@@ -250,5 +251,37 @@ public class FileController {
return ResponseEntity.badRequest().body(Map.of("message", e.getMessage()));
}
}
}
@PostMapping("/batchDownload")
public ResponseEntity<?> batchDownload(
@AuthenticationPrincipal UserPrincipal principal,
@RequestBody Map<String, List<Long>> request,
HttpServletResponse response) throws IOException {
List<Long> ids = request.get("ids");
if (ids == null || ids.isEmpty()) {
return ResponseEntity.badRequest().body(Map.of("message", "请选择要下载的文件"));
}
try {
byte[] zipBytes = fileService.createZipArchive(ids, principal.getUserId());
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=\"download.zip\"");
response.setContentLength(zipBytes.length);
response.getOutputStream().write(zipBytes);
response.getOutputStream().flush();
return ResponseEntity.ok().build();
} catch (RuntimeException e) {
return ResponseEntity.badRequest().body(Map.of("message", e.getMessage()));
}
}
@GetMapping("/movableFolders")
public ResponseEntity<?> getMovableFolders(
@AuthenticationPrincipal UserPrincipal principal,
@RequestParam(required = false) List<Long> excludeIds) {
List<FileEntity> folders = fileService.getMovableFolders(principal.getUserId(), excludeIds);
return ResponseEntity.ok(Map.of("data", folders));
}
}

View File

@@ -499,4 +499,75 @@ public class FileService {
file.setFolderId(targetFolderId);
fileMapper.updateById(file);
}
public byte[] createZipArchive(List<Long> fileIds, Long userId) throws IOException {
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
java.util.zip.ZipOutputStream zos = new java.util.zip.ZipOutputStream(baos);
for (Long fileId : fileIds) {
FileEntity file = fileMapper.selectById(fileId);
if (file == null || !file.getUserId().equals(userId) || file.getIsDeleted() == 1) {
continue;
}
if (file.getIsFolder() == 1) {
// 递归添加文件夹内容
addFolderToZip(zos, file, "", userId);
} else {
// 添加单个文件
addFileToZip(zos, file, "");
}
}
zos.close();
return baos.toByteArray();
}
private void addFolderToZip(java.util.zip.ZipOutputStream zos, FileEntity folder, String parentPath, Long userId) throws IOException {
String folderPath = parentPath + folder.getName() + "/";
// 获取文件夹下的所有内容
List<FileEntity> children = fileMapper.selectList(
new LambdaQueryWrapper<FileEntity>()
.eq(FileEntity::getUserId, userId)
.eq(FileEntity::getFolderId, folder.getId())
.eq(FileEntity::getIsDeleted, 0)
);
for (FileEntity child : children) {
if (child.getIsFolder() == 1) {
addFolderToZip(zos, child, folderPath, userId);
} else {
addFileToZip(zos, child, folderPath);
}
}
}
private void addFileToZip(java.util.zip.ZipOutputStream zos, FileEntity file, String parentPath) throws IOException {
if (file.getPath() == null) return;
Path filePath = Paths.get(storagePath).toAbsolutePath().resolve("files").resolve(file.getPath());
if (!Files.exists(filePath)) return;
String zipEntryName = parentPath + file.getName();
java.util.zip.ZipEntry zipEntry = new java.util.zip.ZipEntry(zipEntryName);
zos.putNextEntry(zipEntry);
byte[] fileBytes = Files.readAllBytes(filePath);
zos.write(fileBytes);
zos.closeEntry();
}
public List<FileEntity> getMovableFolders(Long userId, List<Long> excludeIds) {
LambdaQueryWrapper<FileEntity> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(FileEntity::getUserId, userId)
.eq(FileEntity::getIsDeleted, 0)
.eq(FileEntity::getIsFolder, 1);
if (excludeIds != null && !excludeIds.isEmpty()) {
wrapper.notIn(FileEntity::getId, excludeIds);
}
return fileMapper.selectList(wrapper);
}
}