This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package ru.soune.nocopy.controller;
|
||||
|
||||
import com.google.api.client.util.IOUtils;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import com.vrt.NoCopyFileService;
|
||||
import com.vrt.fileprotection.FileProtector;
|
||||
import com.vrt.fileprotection.NoCopyCheckResult;
|
||||
@@ -13,12 +15,15 @@ import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.web.PageableDefault;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
|
||||
import ru.soune.nocopy.dto.BaseRequest;
|
||||
import ru.soune.nocopy.dto.BaseResponse;
|
||||
import ru.soune.nocopy.dto.MessageCode;
|
||||
@@ -43,6 +48,7 @@ import ru.soune.nocopy.service.file.cloud.CloudStorageService;
|
||||
import ru.soune.nocopy.service.register.AuthService;
|
||||
import ru.soune.nocopy.service.user.moderation.UserVerificationService;
|
||||
import ru.soune.nocopy.util.FileUtil;
|
||||
import software.amazon.awssdk.services.s3.model.NoSuchKeyException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -508,8 +514,6 @@ public class ApiController {
|
||||
// errorData));
|
||||
// }
|
||||
|
||||
InputStream stream = cloudStorageService.readFileFromStorage(entityResponse.getProtectedFilePath());
|
||||
|
||||
// if (!file.exists()) {
|
||||
// Map<String, Object> errorData = new HashMap<>();
|
||||
// errorData.put("file", file.exists());
|
||||
@@ -520,11 +524,19 @@ public class ApiController {
|
||||
// errorData));
|
||||
// }
|
||||
|
||||
StreamingResponseBody responseBody = outputStream -> {
|
||||
try (InputStream stream = cloudStorageService.readFileFromStorage(entityResponse.getProtectedFilePath())) {
|
||||
ByteStreams.copy(stream, outputStream);
|
||||
} catch (NoSuchKeyException e) {
|
||||
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "File not found");
|
||||
}
|
||||
};
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION,
|
||||
"attachment; filename=\"" + entityResponse.getFileName() + "\"")
|
||||
.body(new InputStreamResource(stream));
|
||||
.body(responseBody);
|
||||
} catch (FileEntityNotFoundException e) {
|
||||
Map<String, Object> errorData = new HashMap<>();
|
||||
errorData.put("fileId", fileId);
|
||||
|
||||
Reference in New Issue
Block a user