2025-12-10 19:12:54 +07:00
|
|
|
package ru.soune.nocopy.controller;
|
2025-12-10 02:37:31 +07:00
|
|
|
|
2026-01-13 13:28:09 +07:00
|
|
|
import com.vrt.NoCopyFileService;
|
|
|
|
|
import com.vrt.fileprotection.FileProtector;
|
2026-01-23 13:22:02 +07:00
|
|
|
import com.vrt.fileprotection.NoCopyCheckResult;
|
2026-02-17 13:57:38 +07:00
|
|
|
import com.vrt.fileprotection.audio.AudioCheckResult;
|
|
|
|
|
import com.vrt.fileprotection.documents.DocumentCheckResult;
|
2026-05-29 18:17:04 +07:00
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
2025-12-10 02:37:31 +07:00
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2026-05-28 13:07:09 +07:00
|
|
|
import org.apache.commons.io.IOUtils;
|
2026-01-16 14:30:45 +07:00
|
|
|
import org.springframework.data.domain.Page;
|
|
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
|
|
import org.springframework.data.web.PageableDefault;
|
2026-05-28 12:18:46 +07:00
|
|
|
import org.springframework.http.*;
|
2025-12-10 02:37:31 +07:00
|
|
|
import org.springframework.validation.BindingResult;
|
|
|
|
|
import org.springframework.validation.FieldError;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
2025-12-17 22:31:30 +07:00
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
2025-12-10 19:12:54 +07:00
|
|
|
import ru.soune.nocopy.dto.BaseRequest;
|
|
|
|
|
import ru.soune.nocopy.dto.BaseResponse;
|
|
|
|
|
import ru.soune.nocopy.dto.MessageCode;
|
2026-04-30 18:24:19 +07:00
|
|
|
import ru.soune.nocopy.dto.docviewer.DocViewerRequest;
|
2026-01-24 11:32:02 +07:00
|
|
|
import ru.soune.nocopy.dto.file.*;
|
2026-01-20 03:47:51 +07:00
|
|
|
import ru.soune.nocopy.dto.register.RegAnswer;
|
2026-02-24 14:19:15 +07:00
|
|
|
import ru.soune.nocopy.entity.company.Company;
|
2026-01-24 11:32:02 +07:00
|
|
|
import ru.soune.nocopy.entity.file.FileEntity;
|
2025-12-30 02:19:07 +07:00
|
|
|
import ru.soune.nocopy.entity.file.FileStatus;
|
2025-12-17 22:31:30 +07:00
|
|
|
import ru.soune.nocopy.entity.file.UploadStatus;
|
2026-05-04 16:59:30 +07:00
|
|
|
import ru.soune.nocopy.entity.file.permission.PermissionType;
|
2026-02-05 19:02:17 +07:00
|
|
|
import ru.soune.nocopy.entity.user.AuthToken;
|
2026-02-24 14:19:15 +07:00
|
|
|
import ru.soune.nocopy.entity.user.User;
|
2025-12-30 02:19:07 +07:00
|
|
|
import ru.soune.nocopy.exception.*;
|
2025-12-18 01:13:19 +07:00
|
|
|
import ru.soune.nocopy.handler.*;
|
2026-02-05 19:02:17 +07:00
|
|
|
import ru.soune.nocopy.repository.AuthTokenRepository;
|
2026-01-15 20:41:35 +07:00
|
|
|
import ru.soune.nocopy.repository.FileEntityRepository;
|
2026-02-24 14:19:15 +07:00
|
|
|
import ru.soune.nocopy.repository.UserRepository;
|
2026-01-24 11:32:02 +07:00
|
|
|
import ru.soune.nocopy.service.FileSimilarityService;
|
2026-04-30 18:24:19 +07:00
|
|
|
import ru.soune.nocopy.service.dockview.DockViewService;
|
2026-04-04 15:31:51 +07:00
|
|
|
import ru.soune.nocopy.service.file.*;
|
2026-03-16 14:12:34 +07:00
|
|
|
import ru.soune.nocopy.service.file.CheckCounterService;
|
2026-02-05 18:55:54 +07:00
|
|
|
import ru.soune.nocopy.service.file.ProtectionsLimitService;
|
2026-02-13 21:11:30 +07:00
|
|
|
import ru.soune.nocopy.service.file.cloud.CloudStorageService;
|
2026-05-28 12:18:46 +07:00
|
|
|
import software.amazon.awssdk.services.s3.model.NoSuchKeyException;
|
2025-12-10 02:37:31 +07:00
|
|
|
|
2026-01-23 13:22:02 +07:00
|
|
|
import java.io.File;
|
2026-05-28 12:18:46 +07:00
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
|
import java.io.InputStream;
|
2026-05-22 15:20:34 +07:00
|
|
|
import java.net.URLEncoder;
|
2026-05-22 14:50:15 +07:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2026-04-04 11:26:35 +07:00
|
|
|
import java.util.*;
|
2025-12-10 02:37:31 +07:00
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/api")
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
public class ApiController {
|
2025-12-17 22:31:30 +07:00
|
|
|
private final FileUploadService fileUploadService;
|
2025-12-10 02:37:31 +07:00
|
|
|
|
2025-12-18 01:13:19 +07:00
|
|
|
private final Map<Integer, RequestHandler> handlers;
|
2025-12-10 02:37:31 +07:00
|
|
|
|
2026-05-25 19:08:41 +07:00
|
|
|
private final Map<Integer, RequestHandler> internalInfoHandler;
|
|
|
|
|
|
|
|
|
|
private final Map<Integer, RequestHandler> internalControlHandler;
|
2026-05-25 14:16:39 +07:00
|
|
|
|
2026-05-29 18:17:04 +07:00
|
|
|
private final Map<Integer, RequestHandler> authHandler;
|
2025-12-18 14:40:27 +07:00
|
|
|
|
2026-05-29 18:17:04 +07:00
|
|
|
private final FileEntityService fileEntityService;
|
2025-12-10 02:37:31 +07:00
|
|
|
|
2026-01-13 22:40:52 +07:00
|
|
|
private final FileSimilarityService fileSimilarityService;
|
|
|
|
|
|
2026-01-15 20:41:35 +07:00
|
|
|
private final FileEntityRepository fileEntityRepository;
|
|
|
|
|
|
2026-01-13 13:28:09 +07:00
|
|
|
private final NoCopyFileService noCopyFileService;
|
|
|
|
|
|
2026-05-29 18:17:04 +07:00
|
|
|
private final HttpServletRequest httpServletRequest;
|
2026-01-22 19:55:09 +07:00
|
|
|
|
2026-02-05 18:55:54 +07:00
|
|
|
private final ProtectionsLimitService protectionsLimitService;
|
|
|
|
|
|
2026-02-05 19:02:17 +07:00
|
|
|
private final AuthTokenRepository authTokenRepository;
|
|
|
|
|
|
2026-02-24 14:19:15 +07:00
|
|
|
private final UserRepository userRepository;
|
|
|
|
|
|
2026-03-16 14:12:34 +07:00
|
|
|
private final CheckCounterService checkCounterService;
|
|
|
|
|
|
2026-02-16 15:04:58 +07:00
|
|
|
private final CloudStorageService cloudStorageService;
|
|
|
|
|
|
2026-04-04 15:31:51 +07:00
|
|
|
private final ZipService zipService;
|
|
|
|
|
|
2026-04-30 18:24:19 +07:00
|
|
|
private final DockViewService dockViewService;
|
2026-04-04 17:38:20 +07:00
|
|
|
|
2025-12-10 02:37:31 +07:00
|
|
|
@PostMapping("/v{version}/data")
|
2025-12-10 12:17:08 +07:00
|
|
|
public ResponseEntity<?> handlePostRequest(@RequestBody BaseRequest request,
|
|
|
|
|
@PathVariable("version") int version) {
|
2025-12-10 02:37:31 +07:00
|
|
|
Integer msgId = request.getMsgId();
|
2025-12-10 17:17:48 +07:00
|
|
|
BaseResponse response;
|
2025-12-10 02:37:31 +07:00
|
|
|
|
2025-12-10 12:17:08 +07:00
|
|
|
try {
|
|
|
|
|
RequestHandler handler = handlers.get(msgId);
|
2025-12-10 02:37:31 +07:00
|
|
|
|
2025-12-10 12:17:08 +07:00
|
|
|
if (handler == null) {
|
2025-12-10 17:17:48 +07:00
|
|
|
response = new BaseResponse(msgId,
|
2025-12-10 12:17:08 +07:00
|
|
|
MessageCode.MSG_ID_NOT_FOUND.getCode(),
|
2025-12-10 15:27:14 +07:00
|
|
|
MessageCode.MSG_ID_NOT_FOUND.getDescription(),
|
|
|
|
|
new HashMap<>());
|
2025-12-10 17:17:48 +07:00
|
|
|
} else {
|
|
|
|
|
response = handler.handle(request);
|
2025-12-10 12:17:08 +07:00
|
|
|
}
|
2025-12-10 02:37:31 +07:00
|
|
|
|
2025-12-10 12:17:08 +07:00
|
|
|
return ResponseEntity.ok().body(response);
|
|
|
|
|
} catch (ValidationException e) {
|
|
|
|
|
return createValidationErrorResponse(e.getBindingResult(), e.getMsgId());
|
2026-01-21 12:34:40 +07:00
|
|
|
} catch (UserNotFoundException e) {
|
|
|
|
|
return ResponseEntity.ok().body(new BaseResponse(msgId, MessageCode.USER_NOT_FOUND.getCode(),
|
|
|
|
|
MessageCode.USER_NOT_FOUND.getDescription(), new HashMap<>()));
|
2026-01-05 16:26:46 +07:00
|
|
|
} catch (NotFoundAuthToken e) {
|
|
|
|
|
return ResponseEntity.ok().body(new BaseResponse(msgId, MessageCode.AUTH_TOKEN_NOT_FOUND.getCode(),
|
|
|
|
|
MessageCode.AUTH_TOKEN_NOT_FOUND.getDescription(), new HashMap<>()));
|
2025-12-10 12:17:08 +07:00
|
|
|
} catch (NotValidFieldException e) {
|
|
|
|
|
throw e;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("Handler execution failed for msgId: {}", msgId, e);
|
2025-12-10 19:25:45 +07:00
|
|
|
|
2026-05-25 14:16:39 +07:00
|
|
|
BaseResponse errorResponse = new BaseResponse(msgId,
|
|
|
|
|
MessageCode.INVALID_JSON_BODY.getCode(),
|
|
|
|
|
MessageCode.INVALID_JSON_BODY.getDescription(),
|
|
|
|
|
new HashMap<>());
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok().body(errorResponse);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-29 18:17:04 +07:00
|
|
|
@PostMapping("/auth")
|
|
|
|
|
public ResponseEntity<?> handlePostRequest(@RequestBody BaseRequest request) {
|
|
|
|
|
Integer msgId = request.getMsgId();
|
|
|
|
|
BaseResponse response;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
RequestHandler handler = authHandler.get(msgId);
|
|
|
|
|
|
|
|
|
|
if (handler == null) {
|
|
|
|
|
response = new BaseResponse(msgId,
|
|
|
|
|
MessageCode.MSG_ID_NOT_FOUND.getCode(),
|
|
|
|
|
MessageCode.MSG_ID_NOT_FOUND.getDescription(),
|
|
|
|
|
new HashMap<>());
|
|
|
|
|
} else {
|
|
|
|
|
response = handler.handle(request);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok().body(response);
|
|
|
|
|
} catch (ValidationException e) {
|
|
|
|
|
return createValidationErrorResponse(e.getBindingResult(), e.getMsgId());
|
|
|
|
|
} catch (UserNotFoundException e) {
|
|
|
|
|
return ResponseEntity.ok().body(new BaseResponse(msgId, MessageCode.USER_NOT_FOUND.getCode(),
|
|
|
|
|
MessageCode.USER_NOT_FOUND.getDescription(), new HashMap<>()));
|
|
|
|
|
} catch (NotFoundAuthToken e) {
|
|
|
|
|
return ResponseEntity.ok().body(new BaseResponse(msgId, MessageCode.AUTH_TOKEN_NOT_FOUND.getCode(),
|
|
|
|
|
MessageCode.AUTH_TOKEN_NOT_FOUND.getDescription(), new HashMap<>()));
|
|
|
|
|
} catch (NotValidFieldException e) {
|
|
|
|
|
throw e;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("Handler execution failed for msgId: {}", msgId, e);
|
|
|
|
|
|
|
|
|
|
BaseResponse errorResponse = new BaseResponse(msgId,
|
|
|
|
|
MessageCode.INVALID_JSON_BODY.getCode(),
|
|
|
|
|
MessageCode.INVALID_JSON_BODY.getDescription(),
|
|
|
|
|
new HashMap<>());
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok().body(errorResponse);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 19:08:41 +07:00
|
|
|
@PostMapping("/internal/data-info")
|
|
|
|
|
public ResponseEntity<?> handleInternalInfoPostRequest(@RequestBody BaseRequest request) {
|
2026-05-25 14:16:39 +07:00
|
|
|
Integer msgId = request.getMsgId();
|
|
|
|
|
BaseResponse response;
|
2026-05-25 19:08:41 +07:00
|
|
|
RequestHandler internalHandler = internalInfoHandler.get(msgId);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (internalHandler == null) {
|
|
|
|
|
response = new BaseResponse(msgId,
|
|
|
|
|
MessageCode.MSG_ID_NOT_FOUND.getCode(),
|
|
|
|
|
MessageCode.MSG_ID_NOT_FOUND.getDescription(),
|
|
|
|
|
new HashMap<>());
|
|
|
|
|
} else {
|
|
|
|
|
response = internalHandler.handle(request);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok().body(response);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("Handler execution failed for msgId: {}", msgId, e);
|
|
|
|
|
|
|
|
|
|
BaseResponse errorResponse = new BaseResponse(msgId,
|
|
|
|
|
MessageCode.INVALID_JSON_BODY.getCode(),
|
|
|
|
|
MessageCode.INVALID_JSON_BODY.getDescription(),
|
|
|
|
|
new HashMap<>());
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok().body(errorResponse);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-28 12:18:46 +07:00
|
|
|
@GetMapping("/internal/data-download/{fileId}")
|
2026-05-28 13:07:09 +07:00
|
|
|
public ResponseEntity<byte[]> getPublicFile(@PathVariable String fileId) {
|
2026-05-28 12:18:46 +07:00
|
|
|
try {
|
|
|
|
|
FileEntity fileEntity = fileEntityRepository.findById(fileId)
|
|
|
|
|
.orElseThrow(() -> new FileNotFoundException("Not found in DB: " + fileId));
|
|
|
|
|
|
|
|
|
|
MediaType mediaType = getMediaType(fileEntity);
|
|
|
|
|
|
|
|
|
|
ContentDisposition contentDisposition = ContentDisposition.inline()
|
|
|
|
|
.filename(fileEntity.getOriginalFileName(), StandardCharsets.UTF_8)
|
|
|
|
|
.build();
|
|
|
|
|
|
2026-05-28 13:07:09 +07:00
|
|
|
byte[] fileBytes;
|
|
|
|
|
try (InputStream inputStream = cloudStorageService.readFileFromStorage(fileEntity.getFilePath())) {
|
|
|
|
|
fileBytes = IOUtils.toByteArray(inputStream);
|
|
|
|
|
}
|
2026-05-28 12:18:46 +07:00
|
|
|
|
|
|
|
|
return ResponseEntity.ok()
|
|
|
|
|
.contentType(mediaType)
|
|
|
|
|
.header(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString())
|
|
|
|
|
.header(HttpHeaders.CACHE_CONTROL, "public, max-age=3600")
|
2026-05-28 13:07:09 +07:00
|
|
|
.body(fileBytes);
|
|
|
|
|
|
2026-05-28 12:18:46 +07:00
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
|
return ResponseEntity.notFound().build();
|
|
|
|
|
} catch (NoSuchKeyException e) {
|
|
|
|
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 19:08:41 +07:00
|
|
|
@PostMapping("/internal/data-control")
|
|
|
|
|
public ResponseEntity<?> handleInternalControlPostRequest(@RequestBody BaseRequest request) {
|
|
|
|
|
Integer msgId = request.getMsgId();
|
|
|
|
|
BaseResponse response;
|
|
|
|
|
RequestHandler internalHandler = internalControlHandler.get(msgId);
|
2026-05-25 14:16:39 +07:00
|
|
|
|
|
|
|
|
try {
|
2026-05-25 18:31:41 +07:00
|
|
|
if (internalHandler == null) {
|
2026-05-25 14:16:39 +07:00
|
|
|
response = new BaseResponse(msgId,
|
|
|
|
|
MessageCode.MSG_ID_NOT_FOUND.getCode(),
|
|
|
|
|
MessageCode.MSG_ID_NOT_FOUND.getDescription(),
|
|
|
|
|
new HashMap<>());
|
|
|
|
|
} else {
|
2026-05-25 18:31:41 +07:00
|
|
|
response = internalHandler.handle(request);
|
2026-05-25 14:16:39 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok().body(response);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("Handler execution failed for msgId: {}", msgId, e);
|
|
|
|
|
|
2025-12-10 19:25:45 +07:00
|
|
|
BaseResponse errorResponse = new BaseResponse(msgId,
|
|
|
|
|
MessageCode.INVALID_JSON_BODY.getCode(),
|
|
|
|
|
MessageCode.INVALID_JSON_BODY.getDescription(),
|
|
|
|
|
new HashMap<>());
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok().body(errorResponse);
|
2025-12-10 12:17:08 +07:00
|
|
|
}
|
2025-12-10 02:37:31 +07:00
|
|
|
}
|
|
|
|
|
|
2025-12-17 22:31:30 +07:00
|
|
|
@PostMapping("/v{version}/files/chunk")
|
|
|
|
|
public ResponseEntity<BaseResponse> uploadChunk(
|
|
|
|
|
@PathVariable("version") int version,
|
2025-12-25 13:04:49 +07:00
|
|
|
@RequestParam(value = "upload_id", required = false) String uploadId,
|
|
|
|
|
@RequestParam(value = "chunk_number", required = false) Integer chunkNumber,
|
2026-01-26 21:03:45 +07:00
|
|
|
@RequestParam(value = "chunk", required = false) MultipartFile chunk,
|
2026-01-27 08:09:54 +07:00
|
|
|
@RequestParam(value = "findSimilar", required = false, defaultValue = "0") Integer findSimilar) {
|
2025-12-17 22:31:30 +07:00
|
|
|
try {
|
2025-12-25 13:04:49 +07:00
|
|
|
if (chunk == null || chunk.isEmpty()) {
|
2026-01-15 20:41:35 +07:00
|
|
|
return buildErrorResponse(uploadId, chunkNumber, "Chunk file null or empty");
|
2025-12-25 13:04:49 +07:00
|
|
|
}
|
|
|
|
|
if (uploadId == null || uploadId.isBlank()) {
|
2026-01-15 20:41:35 +07:00
|
|
|
return buildErrorResponse(uploadId, chunkNumber, "Upload ID is required");
|
2025-12-25 13:04:49 +07:00
|
|
|
}
|
|
|
|
|
if (chunkNumber == null || chunkNumber < 0) {
|
2026-01-15 20:41:35 +07:00
|
|
|
return buildErrorResponse(uploadId, chunkNumber, "Valid chunk number is required");
|
2025-12-25 13:04:49 +07:00
|
|
|
}
|
|
|
|
|
|
2026-01-26 21:03:45 +07:00
|
|
|
UploadProgressResponse uploadProgressResponse = fileUploadService.uploadChunk(uploadId, chunkNumber,
|
|
|
|
|
chunk, findSimilar);
|
2025-12-17 22:31:30 +07:00
|
|
|
|
2026-01-26 21:03:45 +07:00
|
|
|
return buildSuccessResponse(uploadId, chunkNumber, chunk,
|
|
|
|
|
fileEntityService.findFileIdByPath(uploadProgressResponse.getFilePath()));
|
2026-01-19 15:49:27 +07:00
|
|
|
} catch (DuplicateImageException e) {
|
2026-01-25 19:59:16 +07:00
|
|
|
Map<String, Object> duplicateData = new HashMap<>();
|
|
|
|
|
duplicateData.put("duplicateFileId", e.duplicateFileId());
|
|
|
|
|
duplicateData.put("userId", e.userId());
|
|
|
|
|
duplicateData.put("message", e.getMessage());
|
|
|
|
|
|
|
|
|
|
if (uploadId != null) {
|
|
|
|
|
duplicateData.put("uploadId", uploadId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok().body(new BaseResponse(
|
|
|
|
|
20004,
|
|
|
|
|
MessageCode.DUPLICATE_FILE_UPLOAD.getCode(),
|
2026-01-19 15:49:27 +07:00
|
|
|
MessageCode.DUPLICATE_FILE_UPLOAD.getDescription(),
|
2026-01-25 19:59:16 +07:00
|
|
|
duplicateData
|
|
|
|
|
));
|
2026-03-31 14:24:03 +07:00
|
|
|
} catch (FileFormatException e){
|
2026-02-21 21:41:05 +07:00
|
|
|
return buildErrorResponse(uploadId, chunkNumber, e.getMessage());
|
2025-12-17 22:31:30 +07:00
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("Error uploading chunk", e);
|
2026-01-15 20:41:35 +07:00
|
|
|
return buildErrorResponse(uploadId, chunkNumber, "Failed to upload chunk: " + e.getMessage());
|
2025-12-17 22:31:30 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 15:31:51 +07:00
|
|
|
@PostMapping("/v{version}/private/files/chunk")
|
|
|
|
|
public ResponseEntity<BaseResponse> uploadChunk(
|
|
|
|
|
@RequestParam(value = "upload_id", required = false) String uploadId,
|
|
|
|
|
@RequestParam(value = "chunk_number", required = false) Integer chunkNumber,
|
|
|
|
|
@RequestParam(value = "chunk", required = false) MultipartFile chunk,
|
2026-05-29 18:17:04 +07:00
|
|
|
@RequestParam(value = "last_file", required = false) Boolean last_file) {
|
2026-04-04 15:31:51 +07:00
|
|
|
try {
|
|
|
|
|
if (chunk == null || chunk.isEmpty()) {
|
|
|
|
|
return buildErrorResponse(uploadId, chunkNumber, "Chunk file null or empty");
|
|
|
|
|
}
|
|
|
|
|
if (uploadId == null || uploadId.isBlank()) {
|
|
|
|
|
return buildErrorResponse(uploadId, chunkNumber, "Upload Id is required");
|
|
|
|
|
}
|
|
|
|
|
if (chunkNumber == null || chunkNumber < 0) {
|
|
|
|
|
return buildErrorResponse(uploadId, chunkNumber, "Valid chunk number is required");
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-29 18:17:04 +07:00
|
|
|
Long userId = (Long) httpServletRequest.getAttribute("userId");
|
2026-04-04 15:31:51 +07:00
|
|
|
|
|
|
|
|
UploadProgressResponse uploadProgressResponse = fileUploadService.uploadPassportChunk(uploadId, chunkNumber,
|
|
|
|
|
chunk, userId);
|
|
|
|
|
|
|
|
|
|
if (last_file) {
|
|
|
|
|
zipService.createAndSplitZip(userId,
|
|
|
|
|
fileEntityService.getAllUserFiles(userId, List.of(FileStatus.PRIVATE)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return buildSuccessResponse(uploadId, chunkNumber, chunk,
|
|
|
|
|
fileEntityService.findFileIdByPath(uploadProgressResponse.getFilePath()));
|
|
|
|
|
}catch (FileFormatException e){
|
|
|
|
|
return buildErrorResponse(uploadId, chunkNumber, e.getMessage());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("Error uploading chunk", e);
|
|
|
|
|
return buildErrorResponse(uploadId, chunkNumber, "Failed to upload chunk: " + e.getMessage());
|
2025-12-17 22:31:30 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 14:42:02 +07:00
|
|
|
@GetMapping("/check/{fileId}")
|
|
|
|
|
public ResponseEntity<BaseResponse> check(@PathVariable String fileId) {
|
|
|
|
|
FileEntity fileEntity = fileEntityRepository.findByFileId(fileId);
|
|
|
|
|
|
|
|
|
|
File file = new File(fileEntity.getFilePath());
|
|
|
|
|
NoCopyCheckResult checkResult = noCopyFileService.checkFile(file, FileProtector.Type.IMAGE);
|
|
|
|
|
log.info("checkResult: {}", checkResult);
|
|
|
|
|
|
2026-04-07 16:43:08 +07:00
|
|
|
|
2026-04-07 14:42:02 +07:00
|
|
|
return ResponseEntity.ok(new BaseResponse(1, 1, "1", checkResult));
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-27 08:09:54 +07:00
|
|
|
@GetMapping("/v{version}/files/{fileId}/similar")
|
2026-01-16 14:30:45 +07:00
|
|
|
public ResponseEntity<BaseResponse> findSimilarFiles(
|
|
|
|
|
@PathVariable("version") int version,
|
|
|
|
|
@PathVariable String fileId,
|
2026-02-17 13:57:38 +07:00
|
|
|
@RequestParam(value = "auth_token", required = false) String authToken,
|
2026-01-16 14:30:45 +07:00
|
|
|
@RequestParam(required = false) List<String> similarityLevels,
|
|
|
|
|
@PageableDefault(size = 20, sort = "hammingDistance") Pageable pageable) {
|
2026-01-15 20:41:35 +07:00
|
|
|
|
2026-02-17 13:57:38 +07:00
|
|
|
try {
|
|
|
|
|
FileEntity fileEntity = fileEntityRepository.findByFileId(fileId);
|
2026-01-16 14:30:45 +07:00
|
|
|
|
2026-02-17 13:57:38 +07:00
|
|
|
if (fileEntity == null) {
|
|
|
|
|
return ResponseEntity.ok().body(new BaseResponse(0, MessageCode.INVALID_FIELD.getCode(),
|
|
|
|
|
MessageCode.INVALID_FIELD.getDescription(), null));
|
|
|
|
|
}
|
2026-01-16 14:30:45 +07:00
|
|
|
|
2026-02-17 13:57:38 +07:00
|
|
|
BaseResponse response;
|
|
|
|
|
if ("image".equals(fileEntity.getMimeType())) {
|
|
|
|
|
Page<SimilarFileDTO> similarFilesPage = fileSimilarityService.findSimilarFiles(
|
|
|
|
|
fileId, similarityLevels, pageable, authToken);
|
|
|
|
|
response = buildSuccessResponse(similarFilesPage, MessageCode.SIMILAR_FILES_FOUND);
|
|
|
|
|
} else {
|
2026-04-23 12:37:14 +07:00
|
|
|
List<SimilarFileDTO> results = processNonImageFile(fileEntity, true);
|
2026-03-02 14:00:10 +07:00
|
|
|
log.info("results: {}", results);
|
2026-02-17 13:57:38 +07:00
|
|
|
response = buildSuccessResponse(results, MessageCode.SIMILAR_FILES_FOUND);
|
2026-03-16 14:12:34 +07:00
|
|
|
checkCounterService.incrementCheckCount(fileEntity.getUserId(), fileEntity.getMimeType());
|
2026-02-17 13:57:38 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok(response);
|
2026-03-03 11:38:22 +07:00
|
|
|
} catch(IllegalArgumentException e) {
|
|
|
|
|
log.error("Error with file extensions : {}", e.getMessage(), e);
|
|
|
|
|
return ResponseEntity.ok().body(new BaseResponse(MessageCode.FILE_FOR_SEARCH_NOT_VALID.getCode(),
|
|
|
|
|
MessageCode.FILE_FOR_SEARCH_NOT_VALID.getCode(),
|
|
|
|
|
MessageCode.FILE_FOR_SEARCH_NOT_VALID.getDescription(), null));
|
2026-02-17 13:57:38 +07:00
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("Error finding similar files: {}", e.getMessage(), e);
|
2026-03-03 11:38:22 +07:00
|
|
|
return ResponseEntity.ok().body(new BaseResponse(MessageCode.INVALID_FIELD.getCode(),
|
|
|
|
|
MessageCode.INVALID_FIELD.getCode(),
|
2026-02-17 13:57:38 +07:00
|
|
|
MessageCode.INVALID_FIELD.getDescription(), null));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-23 12:37:14 +07:00
|
|
|
private List<SimilarFileDTO> processNonImageFile(FileEntity fileEntity, boolean cloud) throws Exception {
|
2026-02-17 13:57:38 +07:00
|
|
|
List<SimilarFileDTO> results = new ArrayList<>();
|
2026-04-23 14:28:06 +07:00
|
|
|
File file = null;
|
2026-02-27 01:52:56 +07:00
|
|
|
FileEntity duplicateByHash = fileSimilarityService.findDuplicateByHash(fileEntity.getFilePath(),
|
2026-04-23 12:37:14 +07:00
|
|
|
fileEntity.getMimeType(), fileEntity.getUserId(), cloud);
|
2026-04-23 14:28:06 +07:00
|
|
|
|
2026-03-02 14:00:10 +07:00
|
|
|
log.info("duplicateByHash: {}", duplicateByHash);
|
2026-04-23 14:28:06 +07:00
|
|
|
|
2026-02-27 01:47:50 +07:00
|
|
|
if (duplicateByHash != null) {
|
|
|
|
|
results.add(fileSimilarityService.buildDTO(duplicateByHash));
|
|
|
|
|
|
|
|
|
|
return results;
|
2026-01-15 20:41:35 +07:00
|
|
|
}
|
|
|
|
|
|
2026-04-23 14:28:06 +07:00
|
|
|
try {
|
|
|
|
|
file = cloudStorageService.readFileFromStorageByPath(fileEntity.getFilePath());
|
|
|
|
|
FileProtector.Type type = "document".equals(fileEntity.getMimeType()) ?
|
|
|
|
|
FileProtector.Type.DOC : FileProtector.Type.AUDIO;
|
2026-01-15 20:41:35 +07:00
|
|
|
|
2026-04-23 14:28:06 +07:00
|
|
|
NoCopyCheckResult checkResult = noCopyFileService.checkFile(file, type);
|
|
|
|
|
log.info("checkResult: {}", checkResult);
|
2026-02-17 13:57:38 +07:00
|
|
|
|
2026-04-23 14:28:06 +07:00
|
|
|
switch (checkResult) {
|
|
|
|
|
case DocumentCheckResult.Success success -> {
|
|
|
|
|
FileProtector.FileInfo info = success.getInfo();
|
|
|
|
|
FileEntity entity = fileEntityRepository.findByFileId(info.getId());
|
|
|
|
|
if (entity != null) {
|
|
|
|
|
results.add(fileSimilarityService.buildDTO(entity));
|
|
|
|
|
}
|
2026-02-17 13:57:38 +07:00
|
|
|
}
|
2026-04-23 14:28:06 +07:00
|
|
|
case AudioCheckResult.Success success -> {
|
|
|
|
|
FileProtector.FileInfo info = success.getInfo();
|
|
|
|
|
FileEntity entity = fileEntityRepository.findByFileId(info.getId());
|
|
|
|
|
if (entity != null) {
|
|
|
|
|
results.add(fileSimilarityService.buildDTO(entity));
|
|
|
|
|
}
|
2026-02-17 13:57:38 +07:00
|
|
|
}
|
2026-04-23 14:28:06 +07:00
|
|
|
case DocumentCheckResult.Failed failed ->
|
|
|
|
|
log.warn("Document check failed: {}", failed.getMessage());
|
|
|
|
|
case AudioCheckResult.Failed failed ->
|
|
|
|
|
log.warn("Audio check failed: {}", failed.getMessage());
|
|
|
|
|
default -> log.warn("Unexpected result type: {}", checkResult.getClass().getSimpleName());
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new Exception("Files for search not valid");
|
|
|
|
|
} finally {
|
|
|
|
|
if (file != null && file.exists()) {
|
|
|
|
|
file.delete();
|
2026-02-17 13:57:38 +07:00
|
|
|
}
|
2026-01-15 20:41:35 +07:00
|
|
|
}
|
|
|
|
|
|
2026-02-17 13:57:38 +07:00
|
|
|
return results;
|
|
|
|
|
}
|
2026-01-15 20:41:35 +07:00
|
|
|
|
2026-02-17 13:57:38 +07:00
|
|
|
private BaseResponse buildSuccessResponse(Page<SimilarFileDTO> page, MessageCode messageCode) {
|
|
|
|
|
Map<String, Object> responseData = new HashMap<>();
|
|
|
|
|
responseData.put("content", page.getContent());
|
|
|
|
|
responseData.put("page", page.getNumber());
|
|
|
|
|
responseData.put("size", page.getSize());
|
|
|
|
|
responseData.put("totalElements", page.getTotalElements());
|
|
|
|
|
responseData.put("totalPages", page.getTotalPages());
|
|
|
|
|
responseData.put("hasNext", page.hasNext());
|
|
|
|
|
responseData.put("hasPrevious", page.hasPrevious());
|
|
|
|
|
|
|
|
|
|
return new BaseResponse(20004, messageCode.getCode(),
|
|
|
|
|
messageCode.getDescription(), responseData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private BaseResponse buildSuccessResponse(List<SimilarFileDTO> list, MessageCode messageCode) {
|
|
|
|
|
Map<String, Object> responseData = new HashMap<>();
|
|
|
|
|
responseData.put("content", list);
|
|
|
|
|
responseData.put("totalElements", list.size());
|
|
|
|
|
|
|
|
|
|
return new BaseResponse(20004, messageCode.getCode(),
|
|
|
|
|
messageCode.getDescription(), responseData);
|
2026-01-13 22:40:52 +07:00
|
|
|
}
|
|
|
|
|
|
2025-12-17 22:31:30 +07:00
|
|
|
@GetMapping("/v{version}/files/progress/{uploadId}")
|
|
|
|
|
public ResponseEntity<BaseResponse> getUploadProgress(
|
|
|
|
|
@PathVariable("version") int version,
|
|
|
|
|
@PathVariable String uploadId) {
|
|
|
|
|
|
|
|
|
|
log.info("Getting progress for upload session: {}, version: {}", uploadId, version);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
var progress = fileUploadService.getUploadProgress(uploadId);
|
|
|
|
|
|
|
|
|
|
UploadProgress responseBody = UploadProgress.builder()
|
|
|
|
|
.uploadId(progress.getUploadId())
|
|
|
|
|
.fileName(progress.getFileName())
|
|
|
|
|
.totalChunks(progress.getTotalChunks())
|
|
|
|
|
.uploadedChunks(progress.getUploadedChunks())
|
|
|
|
|
.status(progress.getStatus().toString())
|
|
|
|
|
.progressPercentage(progress.getProgressPercentage())
|
|
|
|
|
.filePath(progress.getFilePath())
|
|
|
|
|
.remainingChunks(progress.getTotalChunks() - progress.getUploadedChunks())
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok().body(new BaseResponse(
|
|
|
|
|
20004, MessageCode.SUCCESS.getCode(), MessageCode.SUCCESS.getDescription(), responseBody));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("Error getting progress for upload: {}", uploadId, e);
|
|
|
|
|
|
|
|
|
|
UploadProgress responseBody = UploadProgress.builder()
|
|
|
|
|
.uploadId(uploadId)
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok().body(new BaseResponse(
|
|
|
|
|
20004, MessageCode.FILE_UPLOAD_ERROR.getCode(),
|
|
|
|
|
"Failed to get upload progress: " + e.getMessage(), responseBody));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PostMapping("/v{version}/files/{uploadId}/complete")
|
|
|
|
|
public ResponseEntity<BaseResponse> completeUpload(
|
|
|
|
|
@PathVariable("version") int version,
|
|
|
|
|
@PathVariable String uploadId) {
|
|
|
|
|
|
|
|
|
|
log.info("Completing upload session: {}, version: {}", uploadId, version);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
var progress = fileUploadService.getUploadProgress(uploadId);
|
|
|
|
|
|
|
|
|
|
if (progress.getStatus() == UploadStatus.COMPLETED) {
|
|
|
|
|
CompleteUploadResponse responseBody = CompleteUploadResponse.builder()
|
|
|
|
|
.uploadId(progress.getUploadId())
|
|
|
|
|
.status(progress.getStatus().toString())
|
|
|
|
|
.uploadedChunks(progress.getUploadedChunks())
|
|
|
|
|
.totalChunks(progress.getTotalChunks())
|
|
|
|
|
.message("Upload already completed")
|
|
|
|
|
.filePath(progress.getFilePath())
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok().body(new BaseResponse(20004, MessageCode.SUCCESS.getCode(),
|
|
|
|
|
"Upload already completed", responseBody));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (progress.getUploadedChunks() < progress.getTotalChunks()) {
|
|
|
|
|
CompleteUploadResponse responseBody = CompleteUploadResponse.builder()
|
|
|
|
|
.uploadId(progress.getUploadId())
|
|
|
|
|
.status(progress.getStatus().toString())
|
|
|
|
|
.uploadedChunks(progress.getUploadedChunks())
|
|
|
|
|
.totalChunks(progress.getTotalChunks())
|
|
|
|
|
.message("Not all chunks uploaded")
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok().body(new BaseResponse(20004, MessageCode.INCOMPLETE_UPLOAD.getCode(),
|
|
|
|
|
"Not all chunks uploaded", responseBody));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CompleteUploadResponse responseBody = CompleteUploadResponse.builder()
|
|
|
|
|
.uploadId(progress.getUploadId())
|
|
|
|
|
.status(progress.getStatus().toString())
|
|
|
|
|
.uploadedChunks(progress.getUploadedChunks())
|
|
|
|
|
.totalChunks(progress.getTotalChunks())
|
|
|
|
|
.message("File assembly in progress")
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok().body(new BaseResponse(20004, MessageCode.SUCCESS.getCode(),
|
|
|
|
|
"File assembly in progress", responseBody));
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("Error completing upload: {}", uploadId, e);
|
|
|
|
|
|
|
|
|
|
CompleteUploadResponse responseBody = CompleteUploadResponse.builder()
|
|
|
|
|
.uploadId(uploadId)
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok().body(new BaseResponse(20004, MessageCode.FILE_UPLOAD_ERROR.getCode(),
|
|
|
|
|
"Failed to complete upload: " + e.getMessage(), responseBody));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-07 12:49:01 +07:00
|
|
|
@GetMapping("/v{version}/files/public-download/{fileId}")
|
|
|
|
|
public ResponseEntity<?> downloadFile(
|
|
|
|
|
@PathVariable(required = false) String fileId, @PathVariable(required = false) Integer version) {
|
|
|
|
|
FileEntityResponse entityResponse = fileEntityService.getById(fileId, version);
|
|
|
|
|
FileStatus status = entityResponse.getStatus();
|
|
|
|
|
|
|
|
|
|
if (status.equals(FileStatus.BLOCKED) || status.equals(FileStatus.DELETED) ||
|
|
|
|
|
status.equals(FileStatus.REMOVED)) {
|
|
|
|
|
Map<String, Object> errorData = new HashMap<>();
|
|
|
|
|
errorData.put("fileId", entityResponse.getId());
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok().body(new BaseResponse(20004,
|
|
|
|
|
MessageCode.FILE_IS_BLOCKED.getCode(),
|
|
|
|
|
MessageCode.FILE_IS_BLOCKED.getDescription(),
|
|
|
|
|
errorData));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!entityResponse.getPermissions().get(PermissionType.DOWNLOAD)) {
|
|
|
|
|
Map<String, Object> errorData = new HashMap<>();
|
|
|
|
|
errorData.put("fileId", entityResponse.getId());
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok().body(new BaseResponse(20004,
|
|
|
|
|
MessageCode.NOT_HAVE_DOWNLOAD_PERMISSION.getCode(),
|
|
|
|
|
MessageCode.NOT_HAVE_DOWNLOAD_PERMISSION.getDescription(),
|
|
|
|
|
errorData));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] bytes = cloudStorageService.readFileFromStorageBytes(entityResponse.getProtectedFilePath());
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok()
|
|
|
|
|
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
|
|
|
|
.header(HttpHeaders.CONTENT_DISPOSITION,
|
|
|
|
|
"attachment; filename=\"" + entityResponse.getFileName() + "\"")
|
|
|
|
|
.body(bytes);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-18 14:40:27 +07:00
|
|
|
@GetMapping("/v{version}/files/download/{fileId}")
|
2025-12-26 12:22:40 +07:00
|
|
|
public ResponseEntity<?> downloadFile(
|
|
|
|
|
@PathVariable(required = false) String fileId,
|
|
|
|
|
@PathVariable(required = false) Integer version,
|
|
|
|
|
@RequestHeader(value = "Authorization", required = false) String tokenHeader) {
|
2025-12-18 14:40:27 +07:00
|
|
|
try {
|
2026-02-18 11:30:15 +07:00
|
|
|
if (tokenHeader == null || tokenHeader.isBlank()) {
|
2025-12-30 02:19:07 +07:00
|
|
|
Map<String, Object> errorData = new HashMap<>();
|
|
|
|
|
errorData.put("token", tokenHeader);
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok().body(new BaseResponse(20004,
|
2026-02-16 10:43:20 +07:00
|
|
|
MessageCode.TOKEN_IS_NULL.getCode(),
|
|
|
|
|
MessageCode.TOKEN_IS_NULL.getDescription(),
|
2025-12-30 02:19:07 +07:00
|
|
|
errorData));
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-29 18:17:04 +07:00
|
|
|
Long userId = (Long) httpServletRequest.getAttribute("userId");
|
2025-12-26 12:22:40 +07:00
|
|
|
FileEntityResponse entityResponse = fileEntityService.getById(fileId, version);
|
2026-03-25 15:55:16 +07:00
|
|
|
FileStatus status = entityResponse.getStatus();
|
2025-12-18 14:40:27 +07:00
|
|
|
|
2026-03-25 15:55:16 +07:00
|
|
|
if (status.equals(FileStatus.BLOCKED) || status.equals(FileStatus.DELETED) ||
|
|
|
|
|
status.equals(FileStatus.REMOVED)) {
|
|
|
|
|
Map<String, Object> errorData = new HashMap<>();
|
|
|
|
|
errorData.put("fileId", entityResponse.getId());
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok().body(new BaseResponse(20004,
|
|
|
|
|
MessageCode.FILE_IS_BLOCKED.getCode(),
|
|
|
|
|
MessageCode.FILE_IS_BLOCKED.getDescription(),
|
|
|
|
|
errorData));
|
|
|
|
|
}
|
2025-12-18 14:40:27 +07:00
|
|
|
|
2026-05-04 16:59:30 +07:00
|
|
|
if (!entityResponse.getPermissions().get(PermissionType.DOWNLOAD)) {
|
|
|
|
|
Map<String, Object> errorData = new HashMap<>();
|
|
|
|
|
errorData.put("fileId", entityResponse.getId());
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok().body(new BaseResponse(20004,
|
|
|
|
|
MessageCode.NOT_HAVE_DOWNLOAD_PERMISSION.getCode(),
|
|
|
|
|
MessageCode.NOT_HAVE_DOWNLOAD_PERMISSION.getDescription(),
|
|
|
|
|
errorData));
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-24 14:19:15 +07:00
|
|
|
User fileUser = userRepository.findById(entityResponse.getUserId()).orElseThrow();
|
|
|
|
|
User user = userRepository.findById(userId).orElseThrow();
|
|
|
|
|
Company companyUser = user.getCompany();
|
|
|
|
|
|
|
|
|
|
if (companyUser != null && !fileUser.getCompany().getId().equals(companyUser.getId())) {
|
2025-12-30 02:19:07 +07:00
|
|
|
Map<String, Object> errorData = new HashMap<>();
|
|
|
|
|
errorData.put("token", tokenHeader);
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok().body(new BaseResponse(20004,
|
2026-02-24 14:19:15 +07:00
|
|
|
MessageCode.USER_NOT_HAD_PERMISSION.getCode(),
|
|
|
|
|
MessageCode.USER_NOT_HAD_PERMISSION.getDescription(),
|
|
|
|
|
errorData));
|
2026-02-24 14:24:42 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (companyUser == null && !entityResponse.getUserId().equals(userId)) {
|
2025-12-30 02:19:07 +07:00
|
|
|
Map<String, Object> errorData = new HashMap<>();
|
|
|
|
|
errorData.put("token", tokenHeader);
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok().body(new BaseResponse(20004,
|
2026-02-16 10:43:20 +07:00
|
|
|
MessageCode.USER_NOT_HAD_PERMISSION.getCode(),
|
|
|
|
|
MessageCode.USER_NOT_HAD_PERMISSION.getDescription(),
|
2025-12-30 02:19:07 +07:00
|
|
|
errorData));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (entityResponse.getStatus().equals(FileStatus.DELETED)) {
|
|
|
|
|
Map<String, Object> errorData = new HashMap<>();
|
|
|
|
|
errorData.put("file_status", entityResponse.getStatus());
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok().body(new BaseResponse(20004,
|
2026-02-18 11:30:15 +07:00
|
|
|
MessageCode.FILE_DELETE.getCode(),
|
|
|
|
|
MessageCode.FILE_DELETE.getDescription(),
|
2025-12-30 02:19:07 +07:00
|
|
|
errorData));
|
2025-12-18 14:40:27 +07:00
|
|
|
}
|
2026-02-13 21:11:30 +07:00
|
|
|
|
2026-04-21 19:34:43 +07:00
|
|
|
byte[] bytes = cloudStorageService.readFileFromStorageBytes(entityResponse.getProtectedFilePath());
|
2026-04-21 11:50:25 +07:00
|
|
|
|
2025-12-18 14:40:27 +07:00
|
|
|
return ResponseEntity.ok()
|
2026-04-21 02:10:17 +07:00
|
|
|
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
2025-12-18 14:40:27 +07:00
|
|
|
.header(HttpHeaders.CONTENT_DISPOSITION,
|
2026-02-12 16:03:39 +07:00
|
|
|
"attachment; filename=\"" + entityResponse.getFileName() + "\"")
|
2026-04-21 19:34:43 +07:00
|
|
|
.body(bytes);
|
2025-12-26 12:22:40 +07:00
|
|
|
} catch (FileEntityNotFoundException e) {
|
2025-12-30 02:19:07 +07:00
|
|
|
Map<String, Object> errorData = new HashMap<>();
|
|
|
|
|
errorData.put("fileId", fileId);
|
|
|
|
|
|
2025-12-26 12:22:40 +07:00
|
|
|
return ResponseEntity.ok().body(new BaseResponse(20004,
|
|
|
|
|
MessageCode.FILE_NOT_FOUND.getCode(),
|
|
|
|
|
MessageCode.FILE_NOT_FOUND.getDescription(),
|
2025-12-30 02:19:07 +07:00
|
|
|
errorData));
|
2026-02-18 11:30:15 +07:00
|
|
|
} catch (NotFoundAuthToken e) {
|
|
|
|
|
Map<String, Object> errorData = new HashMap<>();
|
|
|
|
|
errorData.put("token", tokenHeader);
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok().body(new BaseResponse(20004,
|
|
|
|
|
MessageCode.AUTH_TOKEN_NOT_FOUND.getCode(),
|
|
|
|
|
MessageCode.AUTH_TOKEN_NOT_FOUND.getDescription(),
|
|
|
|
|
errorData));
|
2026-04-30 18:24:19 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-30 18:35:35 +07:00
|
|
|
@PostMapping("/file/link")
|
2026-04-30 18:24:19 +07:00
|
|
|
public ResponseEntity<?> downloadFile(@RequestBody DocViewerRequest viewerRequest) {
|
|
|
|
|
try {
|
2026-05-29 18:17:04 +07:00
|
|
|
Long userId = httpServletRequest.getAttribute("userId") == null ? 0L:
|
|
|
|
|
(Long) httpServletRequest.getAttribute("userId");
|
2026-04-30 18:24:19 +07:00
|
|
|
|
2026-04-30 18:49:48 +07:00
|
|
|
if (viewerRequest.getFileId() == null) {
|
|
|
|
|
return ResponseEntity.ok().body(new BaseResponse(0,
|
|
|
|
|
MessageCode.NOT_VALID_FIELD.getCode(),
|
|
|
|
|
MessageCode.NOT_VALID_FIELD.getDescription(),
|
|
|
|
|
"file_id is required"));
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-30 18:24:19 +07:00
|
|
|
FileEntityResponse entityResponse = fileEntityService.getById(viewerRequest.getFileId(), 0);
|
|
|
|
|
FileStatus status = entityResponse.getStatus();
|
|
|
|
|
|
|
|
|
|
if (status.equals(FileStatus.BLOCKED) || status.equals(FileStatus.DELETED) ||
|
|
|
|
|
status.equals(FileStatus.REMOVED)) {
|
|
|
|
|
Map<String, Object> errorData = new HashMap<>();
|
|
|
|
|
errorData.put("fileId", entityResponse.getId());
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok().body(new BaseResponse(0,
|
|
|
|
|
MessageCode.FILE_IS_BLOCKED.getCode(),
|
|
|
|
|
MessageCode.FILE_IS_BLOCKED.getDescription(),
|
|
|
|
|
errorData));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] bytes = cloudStorageService.readFileFromStorageBytes(entityResponse.getProtectedFilePath());
|
|
|
|
|
|
|
|
|
|
dockViewService.createDockView(viewerRequest, userId);
|
|
|
|
|
|
2026-05-22 16:47:32 +07:00
|
|
|
String filename = entityResponse.getFileName();
|
|
|
|
|
String encodedFilename = URLEncoder.encode(filename, StandardCharsets.UTF_8)
|
|
|
|
|
.replace("+", "%20");
|
|
|
|
|
|
2026-05-22 16:57:03 +07:00
|
|
|
String safeFilename = filename.replaceAll("[^\\x00-\\x7F]", "_");
|
|
|
|
|
|
2026-04-30 18:24:19 +07:00
|
|
|
return ResponseEntity.ok()
|
|
|
|
|
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
2026-05-22 15:20:34 +07:00
|
|
|
.header(HttpHeaders.CONTENT_DISPOSITION,
|
2026-05-22 16:57:03 +07:00
|
|
|
"attachment; filename=\"" + safeFilename + "\"; filename*=UTF-8''" + encodedFilename)
|
2026-04-30 18:24:19 +07:00
|
|
|
.body(bytes);
|
|
|
|
|
} catch (FileEntityNotFoundException e) {
|
|
|
|
|
Map<String, Object> errorData = new HashMap<>();
|
|
|
|
|
errorData.put("fileId", viewerRequest.getFileId());
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok().body(new BaseResponse(0,
|
|
|
|
|
MessageCode.FILE_NOT_FOUND.getCode(),
|
|
|
|
|
MessageCode.FILE_NOT_FOUND.getDescription(),
|
|
|
|
|
errorData));
|
|
|
|
|
} catch (NotFoundAuthToken e) {
|
|
|
|
|
Map<String, Object> errorData = new HashMap<>();
|
|
|
|
|
errorData.put("token", viewerRequest.getFileId());
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok().body(new BaseResponse(0,
|
|
|
|
|
MessageCode.AUTH_TOKEN_NOT_FOUND.getCode(),
|
|
|
|
|
MessageCode.AUTH_TOKEN_NOT_FOUND.getDescription(),
|
|
|
|
|
errorData));
|
2025-12-18 14:40:27 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 19:02:17 +07:00
|
|
|
@GetMapping("/check/file_stats")
|
|
|
|
|
public ResponseEntity<?> checkFileProtectStats(@RequestHeader("Authorization") String tokenHeader) {
|
|
|
|
|
String token = tokenHeader.replace("Bearer ", "");
|
|
|
|
|
|
|
|
|
|
Optional<AuthToken> tokenOptional = authTokenRepository.findByToken(token);
|
|
|
|
|
|
|
|
|
|
AuthToken authToken = tokenOptional.orElseThrow(() -> new NotFoundAuthToken("Token: " + token + "not found"));
|
|
|
|
|
|
|
|
|
|
Map<String, Object> fileTypeStats = protectionsLimitService.getFileTypeStats(authToken.getUser().getId());
|
2026-02-05 18:55:54 +07:00
|
|
|
|
|
|
|
|
return ResponseEntity.ok().body(fileTypeStats);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-26 21:03:45 +07:00
|
|
|
private ResponseEntity<BaseResponse> buildSuccessResponse(String uploadId, Integer chunkNumber, MultipartFile chunk,
|
|
|
|
|
String fileId) {
|
2026-01-15 20:41:35 +07:00
|
|
|
ChunkUploadResponse responseBody = ChunkUploadResponse.builder()
|
|
|
|
|
.uploadId(uploadId)
|
|
|
|
|
.chunkNumber(chunkNumber)
|
|
|
|
|
.chunkSize(chunk.getSize())
|
2026-01-26 21:03:45 +07:00
|
|
|
.fileId(fileId)
|
2026-01-15 20:41:35 +07:00
|
|
|
.message("Chunk uploaded successfully")
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok().body(new BaseResponse(
|
|
|
|
|
20000,
|
|
|
|
|
MessageCode.SUCCESS.getCode(),
|
|
|
|
|
"Chunk uploaded successfully",
|
|
|
|
|
responseBody));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ResponseEntity<BaseResponse> buildErrorResponse(String uploadId, Integer chunkNumber, String errorMessage) {
|
|
|
|
|
ChunkUploadResponse responseBody = ChunkUploadResponse.builder()
|
|
|
|
|
.uploadId(uploadId)
|
|
|
|
|
.chunkNumber(chunkNumber)
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok().body(new BaseResponse(
|
|
|
|
|
20004,
|
|
|
|
|
MessageCode.FILE_UPLOAD_ERROR.getCode(),
|
|
|
|
|
errorMessage,
|
|
|
|
|
responseBody));
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-10 15:27:14 +07:00
|
|
|
private ResponseEntity<BaseResponse> createValidationErrorResponse(BindingResult bindingResult, Integer msgId) {
|
2025-12-10 02:37:31 +07:00
|
|
|
List<Map<String, String>> fieldErrors = bindingResult.getFieldErrors()
|
|
|
|
|
.stream()
|
|
|
|
|
.map(this::createErrorDetail)
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
2025-12-10 15:27:14 +07:00
|
|
|
RegAnswer regAnswer = new RegAnswer();
|
|
|
|
|
regAnswer.setFieldErrors(fieldErrors);
|
2025-12-10 02:37:31 +07:00
|
|
|
|
2025-12-10 15:27:14 +07:00
|
|
|
return ResponseEntity.ok().body(new BaseResponse(msgId, MessageCode.INVALID_FIELD.getCode(),
|
|
|
|
|
MessageCode.INVALID_FIELD.getDescription(), regAnswer));
|
2025-12-10 02:37:31 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Map<String, String> createErrorDetail(FieldError fieldError) {
|
|
|
|
|
Map<String, String> errorDetail = new HashMap<>();
|
|
|
|
|
errorDetail.put("field", fieldError.getField());
|
|
|
|
|
errorDetail.put("code", fieldError.getCode() != null ? fieldError.getCode() : "VALIDATION_ERROR");
|
|
|
|
|
errorDetail.put("message", fieldError.getDefaultMessage());
|
|
|
|
|
|
|
|
|
|
if (fieldError.getRejectedValue() != null &&
|
|
|
|
|
!fieldError.getField().toLowerCase().contains("password")) {
|
|
|
|
|
errorDetail.put("rejected_value", fieldError.getRejectedValue().toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return errorDetail;
|
|
|
|
|
}
|
2026-05-28 12:18:46 +07:00
|
|
|
|
|
|
|
|
private MediaType getMediaType(FileEntity fileEntity) {
|
|
|
|
|
try {
|
|
|
|
|
return MediaType.parseMediaType(fileEntity.getMimeType());
|
|
|
|
|
} catch (InvalidMediaTypeException e) {
|
|
|
|
|
String extension = fileEntity.getFileExtension().toLowerCase();
|
|
|
|
|
switch (extension) {
|
|
|
|
|
case "jpg":
|
|
|
|
|
case "jpeg":
|
|
|
|
|
return MediaType.IMAGE_JPEG;
|
|
|
|
|
case "png":
|
|
|
|
|
return MediaType.IMAGE_PNG;
|
|
|
|
|
case "pdf":
|
|
|
|
|
return MediaType.APPLICATION_PDF;
|
|
|
|
|
default:
|
|
|
|
|
return MediaType.APPLICATION_OCTET_STREAM;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-12-10 02:37:31 +07:00
|
|
|
}
|