feat: add some condition for illegal name
docs: add api docs for chunk upload system
This commit is contained in:
parent
105e1443d9
commit
041bf72e4b
@ -159,16 +159,32 @@ public class FileController {
|
||||
return type.equals(FileType.FILE) ? fileService.findFilePathById(id) : fileService.findFolderPathById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取需要上传的区块列表
|
||||
* @param hash 文件分块后的所有hash值
|
||||
* @return 需要上传的区块哈希和区块ID
|
||||
*/
|
||||
@PostMapping("/chunk/check")
|
||||
public Result<List<CheckChunkResult>> checkChunkUploaded(@RequestBody List<String> hash) {
|
||||
return fileService.checkChunkUploaded(hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传一个区块
|
||||
* @param block 区块文件
|
||||
* @return 上传的区块ID
|
||||
*/
|
||||
@PostMapping("/chunk/upload")
|
||||
public Result<Long> uploadChunk(@RequestBody MultipartFile block) {
|
||||
return fileService.uploadChunk(block);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将区块保存为文件
|
||||
* @param request 保存文件请求
|
||||
* @return 保存后的文件
|
||||
*/
|
||||
@PutMapping("/chunk/save")
|
||||
public Result<FileVo> saveFile(@RequestBody SaveChunksRequest request) {
|
||||
return fileService.saveFile(request);
|
||||
|
@ -6,6 +6,12 @@ import lombok.Data;
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class CheckChunkResult {
|
||||
/**
|
||||
* 区块哈希值
|
||||
*/
|
||||
private String hash;
|
||||
/**
|
||||
* 区块ID(当区块存在时,存在的区块ID)
|
||||
*/
|
||||
private Long chunkId;
|
||||
}
|
||||
|
@ -6,7 +6,16 @@ import java.util.List;
|
||||
|
||||
@Data
|
||||
public class SaveChunksRequest {
|
||||
/**
|
||||
* 文件名
|
||||
*/
|
||||
private String filename;
|
||||
/**
|
||||
* 区块列表
|
||||
*/
|
||||
private List<Long> chunks;
|
||||
/**
|
||||
* 文件夹ID(根目录为-1)
|
||||
*/
|
||||
private Long folderId;
|
||||
}
|
||||
|
@ -9,9 +9,18 @@ import lombok.Data;
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class ChunkFileVo {
|
||||
/**
|
||||
* 区块ID
|
||||
*/
|
||||
@Column("chunk_id")
|
||||
private long chunkId;
|
||||
/**
|
||||
* 文件ID
|
||||
*/
|
||||
@Column("file_id")
|
||||
private long fileId;
|
||||
/**
|
||||
* 区块顺序下标(从0开始)
|
||||
*/
|
||||
private long index;
|
||||
}
|
||||
|
@ -8,8 +8,17 @@ import lombok.Data;
|
||||
@Table("chunks")
|
||||
@Data
|
||||
public class ChunksVo {
|
||||
/**
|
||||
* 区块ID
|
||||
*/
|
||||
@Id(keyType = KeyType.Auto)
|
||||
private long id;
|
||||
/**
|
||||
* 区块哈希值
|
||||
*/
|
||||
private String hash;
|
||||
/**
|
||||
* 区块大小
|
||||
*/
|
||||
private long size;
|
||||
}
|
||||
|
@ -49,10 +49,7 @@ import org.springframework.web.multipart.MultipartHttpServletRequest;
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
@ -148,6 +145,10 @@ public class FileService {
|
||||
if (fieldName.equals("file")) {
|
||||
String name = next.getName();
|
||||
FilenameDescription filename = getFilename(name);
|
||||
Optional<Result<FileVo>> illegalResult = filename.checkIllegal();
|
||||
if (illegalResult.isPresent()) {
|
||||
return illegalResult.get();
|
||||
}
|
||||
String start = filename.name();
|
||||
String extName = filename.ext();
|
||||
if (fileMapper.selectCountByCondition(FILE_VO.NAME.eq(start).and(FILE_VO.EXT.eq(extName)).and(FILE_VO.FOLDER.eq(folderParams))) > 0) {
|
||||
@ -229,6 +230,12 @@ public class FileService {
|
||||
public Result<FolderVo> mkdir(FolderCreateRequest request, UserVo user, String address) {
|
||||
String name = request.getName();
|
||||
long parent = request.getParent();
|
||||
if (name.isEmpty()) {
|
||||
return Result.failed(HttpStatus.BAD_REQUEST, "文件名不可为空!");
|
||||
}
|
||||
if (name.length() > 160) {
|
||||
return Result.failed(HttpStatus.PAYLOAD_TOO_LARGE, "文件夹名称过长,无法创建!");
|
||||
}
|
||||
if (fileMapper.selectCountByCondition(FILE_VO.EXT.eq(null_()).and(FILE_VO.NAME.eq(name)).and(FILE_VO.FOLDER.eq(parent))) > 0) {
|
||||
return Result.failed(HttpStatus.CONFLICT, "创建文件夹失败,同名文件已存在!");
|
||||
}
|
||||
@ -479,8 +486,13 @@ public class FileService {
|
||||
@Transactional
|
||||
public Result<FileVo> saveFile(SaveChunksRequest chunks) {
|
||||
FilenameDescription filename = getFilename(chunks.getFilename());
|
||||
Optional<Result<FileVo>> illegalResult = filename.checkIllegal();
|
||||
if (illegalResult.isPresent()) {
|
||||
return illegalResult.get();
|
||||
}
|
||||
String name = filename.name();
|
||||
String ext = filename.ext();
|
||||
|
||||
Long folderId = chunks.getFolderId();
|
||||
if (fileMapper.selectCountByCondition(FILE_VO.FOLDER.eq(folderId).and(FILE_VO.NAME.eq(name)).and(FILE_VO.EXT.eq(ext))) > 0) {
|
||||
return Result.failed(HttpStatus.CONFLICT, "文件已存在!");
|
||||
@ -488,8 +500,7 @@ public class FileService {
|
||||
FileVo fileVo = new FileVo();
|
||||
List<Long> chunkIds = chunks.getChunks();
|
||||
List<ChunksVo> chunksVos = chunksMapper.selectListByIds(chunkIds);
|
||||
//noinspection OptionalGetWithoutIsPresent
|
||||
List<ChunksVo> sortedChunks = chunkIds.stream().map(e -> chunksVos.stream().filter(a -> a.getId() == e).findFirst().get()).toList();
|
||||
List<ChunksVo> sortedChunks = chunkIds.stream().map(e -> chunksVos.stream().filter(a -> a.getId() == e).findFirst().orElseThrow()).toList();
|
||||
String mime;
|
||||
String sha512;
|
||||
long size;
|
||||
@ -528,5 +539,14 @@ public class FileService {
|
||||
}
|
||||
|
||||
private record FilenameDescription(String name, String ext) {
|
||||
public <T> Optional<Result<T>> checkIllegal() {
|
||||
if (name.length() > 120 || ext.length() > 40) {
|
||||
return Optional.of(Result.failed(HttpStatus.PAYLOAD_TOO_LARGE, "文件名过长,无法上传!"));
|
||||
}
|
||||
if (name.isEmpty()) {
|
||||
return Optional.of(Result.failed(HttpStatus.BAD_REQUEST, "文件名为空,无法上传!"));
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
}
|
@ -21,8 +21,6 @@ import org.springframework.data.redis.core.ValueOperations;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static cn.wzpmc.filemanager.entities.vo.table.UserVoTableDef.USER_VO;
|
||||
@ -70,6 +68,10 @@ public class UserService {
|
||||
public void register(UserRegisterRequest request, HttpServletResponse response, String address) {
|
||||
String username = request.getUsername();
|
||||
String password = request.getPassword();
|
||||
if (username == null || password == null || username.isEmpty() || password.isEmpty()) {
|
||||
Result.failed(HttpStatus.BAD_REQUEST, "用户名/密码不可为空!").writeToResponse(response);
|
||||
return;
|
||||
}
|
||||
String sha1edPassword = DigestUtils.sha1Hex(password);
|
||||
Auth auth = request.getAuth();
|
||||
if (this.userMapper.selectCountByCondition(USER_VO.NAME.eq(username)) > 0) {
|
||||
|
@ -7,7 +7,7 @@
|
||||
CREATE TABLE IF NOT EXISTS `file`
|
||||
(
|
||||
`id` int NOT NULL AUTO_INCREMENT COMMENT '文件ID',
|
||||
`name` varchar(40) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '文件名',
|
||||
`name` varchar(120) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '文件名',
|
||||
`ext` varchar(40) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '文件扩展名',
|
||||
`mime` varchar(100) COLLATE utf8mb4_general_ci NOT NULL COMMENT '文件的MIME类型',
|
||||
`hash` char(128) COLLATE utf8mb4_general_ci NOT NULL COMMENT '文件sha512',
|
||||
@ -31,7 +31,7 @@
|
||||
CREATE TABLE IF NOT EXISTS `folder`
|
||||
(
|
||||
`id` int NOT NULL AUTO_INCREMENT COMMENT '文件夹ID',
|
||||
`name` varchar(80) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '文件夹名称',
|
||||
`name` varchar(160) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '文件夹名称',
|
||||
`parent` int DEFAULT NULL COMMENT '父文件夹ID',
|
||||
`creator` int DEFAULT NULL COMMENT '创建者ID',
|
||||
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
|
||||
|
Loading…
x
Reference in New Issue
Block a user