# Conflicts: # src/main/java/ru/soune/nocopy/controller/ApiController.java # src/main/java/ru/soune/nocopy/handler/FileEntityHandler.java
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
package ru.soune.nocopy.dto.file;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class FileInfoUserResponse {
|
||||
@JsonProperty("all_files_size")
|
||||
private Long allFileSize;
|
||||
|
||||
@JsonProperty("all_files_quantity")
|
||||
private Integer fileCount;
|
||||
|
||||
@JsonProperty("all_files_check")
|
||||
private Integer filesCheck;
|
||||
|
||||
@JsonProperty("all_files_violation")
|
||||
private Integer filesViolation;
|
||||
|
||||
@JsonProperty("images_size")
|
||||
private Long imagesSize;
|
||||
|
||||
@JsonProperty("images_quantity")
|
||||
private Integer imagesCount;
|
||||
|
||||
@JsonProperty("images_check")
|
||||
private Integer imagesCheck;
|
||||
|
||||
@JsonProperty("images_violations")
|
||||
private Integer imagesViolations;
|
||||
|
||||
@JsonProperty("videos_size")
|
||||
private Long videosSize;
|
||||
|
||||
@JsonProperty("videos_quantity")
|
||||
private Integer videosCount;
|
||||
|
||||
@JsonProperty("videos_check")
|
||||
private Integer videosCheck;
|
||||
|
||||
@JsonProperty("videos_violations")
|
||||
private Integer videosViolations;
|
||||
|
||||
@JsonProperty("audios_size")
|
||||
private Long audiosSize;
|
||||
|
||||
@JsonProperty("audios_quantity")
|
||||
private Integer audiosCount;
|
||||
|
||||
@JsonProperty("audios_check")
|
||||
private Integer audiosCheck;
|
||||
|
||||
@JsonProperty("audios_violations")
|
||||
private Integer audiosViolations;
|
||||
}
|
||||
@@ -4,5 +4,7 @@ public enum FileStatus {
|
||||
ACTIVE,
|
||||
DELETED,
|
||||
PROCESSING,
|
||||
VIOLATION,
|
||||
CHECKED,
|
||||
ERROR
|
||||
}
|
||||
|
||||
@@ -7,21 +7,10 @@ import java.util.List;
|
||||
|
||||
@Getter
|
||||
public enum FileType {
|
||||
IMAGE("image", Arrays.asList("jpg", "jpeg", "png", "gif", "bmp", "webp")),
|
||||
IMAGE("image", Arrays.asList("jpg", "jpeg", "png", "gif", "bmp", "webp", "jfif")),
|
||||
VIDEO("video", Arrays.asList("mp4", "avi", "mov", "wmv", "flv", "mkv", "webm", "m4v", "mpg", "mpeg",
|
||||
"3gp", "3g2", "f4v", "m2ts", "mts", "vob", "ogv", "divx")),
|
||||
AUDIO("audio", Arrays.asList("mp3", "wav", "flac")),
|
||||
DOCUMENT("document", Arrays.asList(
|
||||
"pdf", "txt", "rtf",
|
||||
"doc", "docx", "xls", "xlsx", "ppt", "pptx", "pps", "ppsx", "dot", "dotx", "xlt", "xltx", "pot", "potx",
|
||||
"odt", "ods", "odp", "odg", "odf", "odb", "odc", "odi", "odm", "ott", "ots", "otp", "otg", "oth",
|
||||
"sxw", "sxc", "sxi", "sxd", "sxg", "stc", "sti", "stw", "sxm",
|
||||
"pages", "numbers", "key",
|
||||
"csv", "tsv", "xml", "html", "htm", "tex", "md", "markdown",
|
||||
"epub", "mobi", "azw", "azw3", "fb2",
|
||||
"wps", "wpt", "et", "dps", "vsd", "vsdx",
|
||||
"java", "py", "cpp", "c", "h", "js", "css", "php", "sql", "json", "yaml", "yml", "sh", "bat",
|
||||
"one", "note"));
|
||||
AUDIO("audio", Arrays.asList("mp3", "wav", "flac"));
|
||||
|
||||
private final String displayName;
|
||||
private final List<String> allowedExtensions;
|
||||
|
||||
@@ -14,6 +14,7 @@ import ru.soune.nocopy.repository.AuthTokenRepository;
|
||||
import ru.soune.nocopy.repository.FileEntityRepository;
|
||||
import ru.soune.nocopy.service.auth.AuthService;
|
||||
import ru.soune.nocopy.service.file.FileEntityService;
|
||||
import ru.soune.nocopy.service.file.FileStatsService;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@@ -22,10 +23,13 @@ import java.util.List;
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class FileEntityHandler implements RequestHandler {
|
||||
|
||||
private final FileEntityService fileEntityService;
|
||||
|
||||
private final AuthService authService;
|
||||
|
||||
private final FileStatsService fileStatsService;
|
||||
|
||||
private final AuthTokenRepository authTokenRepository;
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
@@ -43,6 +47,8 @@ public class FileEntityHandler implements RequestHandler {
|
||||
switch (action) {
|
||||
case "file_info":
|
||||
return handleGetFileInfo(request, fileRequest);
|
||||
case "user_files_info":
|
||||
return handleGetFilesUserInfo(request, fileRequest);
|
||||
case "file_by_session":
|
||||
return handleGetFileBySession(request, fileRequest);
|
||||
case "user_files":
|
||||
@@ -75,6 +81,16 @@ public class FileEntityHandler implements RequestHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private BaseResponse handleGetFilesUserInfo(BaseRequest request, FileEntityRequest fileRequest) {
|
||||
Long userId = getUserIdFromToken(fileRequest.getToken());
|
||||
FileInfoUserResponse userFileStats = fileStatsService.getUserFileStats(userId);
|
||||
|
||||
return new BaseResponse(request.getMsgId(),
|
||||
MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(),
|
||||
userFileStats);
|
||||
}
|
||||
|
||||
private BaseResponse handleGetFileInfo(BaseRequest request, FileEntityRequest fileRequest) {
|
||||
try {
|
||||
Long userId = authService.useUserAuthToken(fileRequest.getToken());
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
package ru.soune.nocopy.service.file;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.soune.nocopy.dto.file.FileInfoUserResponse;
|
||||
import ru.soune.nocopy.entity.file.FileEntity;
|
||||
import ru.soune.nocopy.entity.file.FileStatus;
|
||||
import ru.soune.nocopy.entity.file.FileType;
|
||||
import ru.soune.nocopy.repository.FileEntityRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class FileStatsService {
|
||||
private final FileEntityRepository fileEntityRepository;
|
||||
|
||||
public FileInfoUserResponse getUserFileStats(Long userId) {
|
||||
List<FileEntity> userFiles = fileEntityRepository.findByUserId(userId);
|
||||
return calculateStats(userFiles);
|
||||
}
|
||||
|
||||
public FileInfoUserResponse calculateStats(List<FileEntity> files) {
|
||||
return FileInfoUserResponse.builder()
|
||||
.allFileSize(calculateTotalSize(files))
|
||||
.fileCount(calculateTotalCount(files))
|
||||
.filesCheck(calculateByStatus(files, FileStatus.CHECKED))
|
||||
.filesViolation(calculateByStatus(files, FileStatus.VIOLATION))
|
||||
|
||||
.imagesSize(calculateMediaSize(files, FileType.IMAGE))
|
||||
.imagesCount(calculateMediaCount(files, FileType.IMAGE))
|
||||
.imagesCheck(calculateMediaByStatus(files, FileType.IMAGE, FileStatus.CHECKED))
|
||||
.imagesViolations(calculateMediaByStatus(files, FileType.IMAGE, FileStatus.VIOLATION))
|
||||
|
||||
.videosSize(calculateMediaSize(files, FileType.VIDEO))
|
||||
.videosCount(calculateMediaCount(files, FileType.VIDEO))
|
||||
.videosCheck(calculateMediaByStatus(files, FileType.VIDEO, FileStatus.CHECKED))
|
||||
.videosViolations(calculateMediaByStatus(files, FileType.VIDEO, FileStatus.VIOLATION))
|
||||
|
||||
.audiosSize(calculateMediaSize(files, FileType.AUDIO))
|
||||
.audiosCount(calculateMediaCount(files, FileType.AUDIO))
|
||||
.audiosCheck(calculateMediaByStatus(files, FileType.AUDIO, FileStatus.CHECKED))
|
||||
.audiosViolations(calculateMediaByStatus(files, FileType.AUDIO, FileStatus.VIOLATION))
|
||||
.build();
|
||||
}
|
||||
private Long calculateTotalSize(List<FileEntity> files) {
|
||||
return files.stream()
|
||||
.filter(file -> file.getStatus() != FileStatus.DELETED)
|
||||
.mapToLong(FileEntity::getFileSize)
|
||||
.sum();
|
||||
}
|
||||
|
||||
private Integer calculateTotalCount(List<FileEntity> files) {
|
||||
return (int) files.stream()
|
||||
.filter(file -> file.getStatus() != FileStatus.DELETED)
|
||||
.count();
|
||||
}
|
||||
|
||||
private Integer calculateByStatus(List<FileEntity> files, FileStatus status) {
|
||||
return (int) files.stream()
|
||||
.filter(file -> file.getStatus() == status)
|
||||
.count();
|
||||
}
|
||||
|
||||
private Long calculateMediaSize(List<FileEntity> files, FileType fileType) {
|
||||
return files.stream()
|
||||
.filter(file -> file.getStatus() != FileStatus.DELETED)
|
||||
.filter(file -> isFileType(file, fileType))
|
||||
.mapToLong(FileEntity::getFileSize)
|
||||
.sum();
|
||||
}
|
||||
|
||||
private Integer calculateMediaCount(List<FileEntity> files, FileType fileType) {
|
||||
return (int) files.stream()
|
||||
.filter(file -> file.getStatus() != FileStatus.DELETED)
|
||||
.filter(file -> isFileType(file, fileType))
|
||||
.count();
|
||||
}
|
||||
|
||||
private Integer calculateMediaByStatus(List<FileEntity> files, FileType fileType, FileStatus status) {
|
||||
return (int) files.stream()
|
||||
.filter(file -> isFileType(file, fileType))
|
||||
.filter(file -> file.getStatus() == status)
|
||||
.count();
|
||||
}
|
||||
|
||||
private boolean isFileType(FileEntity file, FileType fileType) {
|
||||
if (file.getFileExtension() == null) return false;
|
||||
|
||||
String extension = file.getFileExtension().toLowerCase().replace(".", "");
|
||||
return fileType.supportsExtension(extension);
|
||||
}
|
||||
}
|
||||
@@ -102,7 +102,7 @@ public class FileUploadServiceImpl implements FileUploadService {
|
||||
int totalChunks = (int) Math.ceil((double) fileSize / chunkSize);
|
||||
log.debug("File will be split into {} chunks (chunk size: {} bytes)",
|
||||
totalChunks, chunkSize);
|
||||
Long chunkSize = totalChunks == 1 ? fileSize : 1048576L;
|
||||
Long chunkSize = totalChunks == 1 ? fileSize : 1000000L;
|
||||
|
||||
FileUploadSession session = FileUploadSession.builder()
|
||||
.userId(userId)
|
||||
|
||||
Reference in New Issue
Block a user