@@ -21,6 +21,7 @@ import ru.soune.nocopy.dto.file.FileEntityResponse;
|
|||||||
import ru.soune.nocopy.dto.file.UploadProgress;
|
import ru.soune.nocopy.dto.file.UploadProgress;
|
||||||
import ru.soune.nocopy.entity.AuthToken;
|
import ru.soune.nocopy.entity.AuthToken;
|
||||||
import ru.soune.nocopy.entity.file.UploadStatus;
|
import ru.soune.nocopy.entity.file.UploadStatus;
|
||||||
|
import ru.soune.nocopy.exception.FileEntityNotFoundException;
|
||||||
import ru.soune.nocopy.exception.NotFoundAuthToken;
|
import ru.soune.nocopy.exception.NotFoundAuthToken;
|
||||||
import ru.soune.nocopy.exception.NotValidFieldException;
|
import ru.soune.nocopy.exception.NotValidFieldException;
|
||||||
import ru.soune.nocopy.exception.ValidationException;
|
import ru.soune.nocopy.exception.ValidationException;
|
||||||
@@ -241,29 +242,32 @@ public class ApiController {
|
|||||||
|
|
||||||
|
|
||||||
@GetMapping("/v{version}/files/download/{fileId}")
|
@GetMapping("/v{version}/files/download/{fileId}")
|
||||||
public ResponseEntity<Resource> downloadFile(
|
public ResponseEntity<?> downloadFile(
|
||||||
@PathVariable String fileId,
|
@PathVariable(required = false) String fileId,
|
||||||
@PathVariable Integer version,
|
@PathVariable(required = false) Integer version,
|
||||||
@RequestHeader("Authorization") String tokenHeader) {
|
@RequestHeader(value = "Authorization", required = false) String tokenHeader) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Long userId = getUserIdFromToken(tokenHeader);
|
Long userId = getUserIdFromToken(tokenHeader);
|
||||||
FileEntityResponse fileInfo = fileEntityService.getById(fileId, version);
|
FileEntityResponse entityResponse = fileEntityService.getById(fileId, version);
|
||||||
|
|
||||||
if (!fileInfo.getUserId().equals(userId)) {
|
if (!entityResponse.getUserId().equals(userId)) {
|
||||||
return ResponseEntity.status(403).build();
|
return ResponseEntity.ok().body(new BaseResponse(20004, MessageCode.FILE_DOWNLOAD_ERROR.getCode(),
|
||||||
|
MessageCode.FILE_DOWNLOAD_ERROR.getDescription(), Map.of("token", tokenHeader)));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fileInfo.isExistsOnDisk()) {
|
if (!entityResponse.isExistsOnDisk()) {
|
||||||
return ResponseEntity.status(404)
|
return ResponseEntity.ok().body(new BaseResponse(20004, MessageCode.FILE_DOWNLOAD_ERROR.getCode(),
|
||||||
.body(null);
|
MessageCode.FILE_DOWNLOAD_ERROR.getDescription(),
|
||||||
|
Map.of("onDisk", entityResponse.isExistsOnDisk())));
|
||||||
}
|
}
|
||||||
|
|
||||||
Path filePath = Paths.get(fileInfo.getFilePath());
|
Path filePath = Paths.get(entityResponse.getFilePath());
|
||||||
Resource resource = new UrlResource(filePath.toUri());
|
Resource resource = new UrlResource(filePath.toUri());
|
||||||
|
|
||||||
if (!resource.exists()) {
|
if (!resource.exists()) {
|
||||||
return ResponseEntity.status(404).build();
|
return ResponseEntity.ok().body(new BaseResponse(20004, MessageCode.FILE_DOWNLOAD_ERROR.getCode(),
|
||||||
|
MessageCode.FILE_DOWNLOAD_ERROR.getDescription(),
|
||||||
|
Map.of("resource", resource.exists())));
|
||||||
}
|
}
|
||||||
|
|
||||||
String contentType = determineContentType(filePath);
|
String contentType = determineContentType(filePath);
|
||||||
@@ -271,15 +275,21 @@ public class ApiController {
|
|||||||
return ResponseEntity.ok()
|
return ResponseEntity.ok()
|
||||||
.contentType(MediaType.parseMediaType(contentType))
|
.contentType(MediaType.parseMediaType(contentType))
|
||||||
.header(HttpHeaders.CONTENT_DISPOSITION,
|
.header(HttpHeaders.CONTENT_DISPOSITION,
|
||||||
"attachment; filename=\"" + fileInfo.getOriginalFileName() + "\"")
|
"attachment; filename=\"" + entityResponse.getOriginalFileName() + "\"")
|
||||||
.header(HttpHeaders.CONTENT_LENGTH, String.valueOf(fileInfo.getFileSize()))
|
.header(HttpHeaders.CONTENT_LENGTH, String.valueOf(entityResponse.getFileSize()))
|
||||||
.body(resource);
|
.body(resource);
|
||||||
|
} catch (FileEntityNotFoundException e) {
|
||||||
} catch (NotFoundAuthToken e) {
|
return ResponseEntity.ok().body(new BaseResponse(20004,
|
||||||
return ResponseEntity.status(401).build();
|
MessageCode.FILE_NOT_FOUND.getCode(),
|
||||||
|
MessageCode.FILE_NOT_FOUND.getDescription(),
|
||||||
|
Map.of("fileId", fileId)));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Error downloading file", e);
|
return ResponseEntity.ok().body(new BaseResponse(20004,
|
||||||
return ResponseEntity.status(500).build();
|
MessageCode.FILE_DOWNLOAD_ERROR_NOT_CORRECT_FIELD.getCode(),
|
||||||
|
MessageCode.FILE_DOWNLOAD_ERROR_NOT_CORRECT_FIELD.getDescription(),
|
||||||
|
Map.of("token", tokenHeader,
|
||||||
|
"fileId", fileId,
|
||||||
|
"version", version)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ public enum MessageCode {
|
|||||||
INVALID_TOKEN(2, "Invalid token"),
|
INVALID_TOKEN(2, "Invalid token"),
|
||||||
INVALID_ACTION(2, "Invalid action"),
|
INVALID_ACTION(2, "Invalid action"),
|
||||||
FILE_UPLOAD_ERROR(2, "File upload error"),
|
FILE_UPLOAD_ERROR(2, "File upload error"),
|
||||||
|
FILE_DOWNLOAD_ERROR(2, "File download error"),
|
||||||
|
FILE_DOWNLOAD_ERROR_NOT_CORRECT_FIELD(2, "File download error with correct field"),
|
||||||
INVALID_JSON_BODY(2, "Invalid fields in JSON object"),
|
INVALID_JSON_BODY(2, "Invalid fields in JSON object"),
|
||||||
INCOMPLETE_UPLOAD(2, "Not load all chunks"),
|
INCOMPLETE_UPLOAD(2, "Not load all chunks"),
|
||||||
MSG_ID_NOT_FOUND(4, "Message id not found"),
|
MSG_ID_NOT_FOUND(4, "Message id not found"),
|
||||||
|
|||||||
Reference in New Issue
Block a user