This commit is contained in:
@@ -8,6 +8,7 @@ import com.vrt.fileprotection.documents.DocumentCheckResult;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.core.io.FileSystemResource;
|
import org.springframework.core.io.FileSystemResource;
|
||||||
|
import org.springframework.core.io.InputStreamResource;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.data.web.PageableDefault;
|
import org.springframework.data.web.PageableDefault;
|
||||||
@@ -45,6 +46,7 @@ import ru.soune.nocopy.util.FileUtil;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
@@ -494,6 +496,7 @@ public class ApiController {
|
|||||||
MessageCode.FILE_DELETE.getDescription(),
|
MessageCode.FILE_DELETE.getDescription(),
|
||||||
errorData));
|
errorData));
|
||||||
}
|
}
|
||||||
|
//TODO
|
||||||
|
|
||||||
// if (!entityResponse.isExistsOnDisk()) {
|
// if (!entityResponse.isExistsOnDisk()) {
|
||||||
// Map<String, Object> errorData = new HashMap<>();
|
// Map<String, Object> errorData = new HashMap<>();
|
||||||
@@ -505,23 +508,23 @@ public class ApiController {
|
|||||||
// errorData));
|
// errorData));
|
||||||
// }
|
// }
|
||||||
|
|
||||||
File file = cloudStorageService.readFileFromStorageByPath(entityResponse.getProtectedFilePath());
|
InputStream stream = cloudStorageService.readFileFromStorage(entityResponse.getProtectedFilePath());
|
||||||
|
|
||||||
if (!file.exists()) {
|
// if (!file.exists()) {
|
||||||
Map<String, Object> errorData = new HashMap<>();
|
// Map<String, Object> errorData = new HashMap<>();
|
||||||
errorData.put("file", file.exists());
|
// errorData.put("file", file.exists());
|
||||||
|
//
|
||||||
return ResponseEntity.ok().body(new BaseResponse(20004,
|
// return ResponseEntity.ok().body(new BaseResponse(20004,
|
||||||
MessageCode.FILE_DOWNLOAD_ERROR.getCode(),
|
// MessageCode.FILE_DOWNLOAD_ERROR.getCode(),
|
||||||
MessageCode.FILE_DOWNLOAD_ERROR.getDescription(),
|
// MessageCode.FILE_DOWNLOAD_ERROR.getDescription(),
|
||||||
errorData));
|
// errorData));
|
||||||
}
|
// }
|
||||||
|
|
||||||
return ResponseEntity.ok()
|
return ResponseEntity.ok()
|
||||||
.contentLength(file.length())
|
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
||||||
.header(HttpHeaders.CONTENT_DISPOSITION,
|
.header(HttpHeaders.CONTENT_DISPOSITION,
|
||||||
"attachment; filename=\"" + entityResponse.getFileName() + "\"")
|
"attachment; filename=\"" + entityResponse.getFileName() + "\"")
|
||||||
.body(new FileSystemResource(file));
|
.body(new InputStreamResource(stream));
|
||||||
} catch (FileEntityNotFoundException e) {
|
} catch (FileEntityNotFoundException e) {
|
||||||
Map<String, Object> errorData = new HashMap<>();
|
Map<String, Object> errorData = new HashMap<>();
|
||||||
errorData.put("fileId", fileId);
|
errorData.put("fileId", fileId);
|
||||||
@@ -538,16 +541,16 @@ public class ApiController {
|
|||||||
MessageCode.AUTH_TOKEN_NOT_FOUND.getCode(),
|
MessageCode.AUTH_TOKEN_NOT_FOUND.getCode(),
|
||||||
MessageCode.AUTH_TOKEN_NOT_FOUND.getDescription(),
|
MessageCode.AUTH_TOKEN_NOT_FOUND.getDescription(),
|
||||||
errorData));
|
errorData));
|
||||||
} catch (IOException e) {
|
// } catch (IOException e) {
|
||||||
Map<String, Object> errorData = new HashMap<>();
|
// Map<String, Object> errorData = new HashMap<>();
|
||||||
errorData.put("token", tokenHeader);
|
// errorData.put("token", tokenHeader);
|
||||||
errorData.put("fileId", fileId);
|
// errorData.put("fileId", fileId);
|
||||||
errorData.put("version", version);
|
// errorData.put("version", version);
|
||||||
|
//
|
||||||
return ResponseEntity.ok().body(new BaseResponse(20004,
|
// return ResponseEntity.ok().body(new BaseResponse(20004,
|
||||||
MessageCode.FILE_DOWNLOAD_ERROR_NOT_CORRECT_FIELD.getCode(),
|
// MessageCode.FILE_DOWNLOAD_ERROR_NOT_CORRECT_FIELD.getCode(),
|
||||||
MessageCode.FILE_DOWNLOAD_ERROR_NOT_CORRECT_FIELD.getDescription(),
|
// MessageCode.FILE_DOWNLOAD_ERROR_NOT_CORRECT_FIELD.getDescription(),
|
||||||
errorData));
|
// errorData));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
|||||||
import software.amazon.awssdk.auth.credentials.AwsCredentials;
|
import software.amazon.awssdk.auth.credentials.AwsCredentials;
|
||||||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||||
import software.amazon.awssdk.core.sync.RequestBody;
|
import software.amazon.awssdk.core.sync.RequestBody;
|
||||||
|
import software.amazon.awssdk.core.sync.ResponseTransformer;
|
||||||
import software.amazon.awssdk.http.apache.ApacheHttpClient;
|
import software.amazon.awssdk.http.apache.ApacheHttpClient;
|
||||||
import software.amazon.awssdk.regions.Region;
|
import software.amazon.awssdk.regions.Region;
|
||||||
import software.amazon.awssdk.services.s3.S3Client;
|
import software.amazon.awssdk.services.s3.S3Client;
|
||||||
@@ -18,6 +19,7 @@ import software.amazon.awssdk.services.s3.model.PutObjectRequest;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
@@ -96,6 +98,24 @@ public class CloudStorageService {
|
|||||||
return tempFile;
|
return tempFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public InputStream readFileFromStorage(String filePath) {
|
||||||
|
GetObjectRequest objectRequest = GetObjectRequest.builder()
|
||||||
|
.bucket(bucket)
|
||||||
|
.key("files" + filePath)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
AwsCredentials credentials = AwsBasicCredentials.create(key, secretKey);
|
||||||
|
|
||||||
|
S3Client s3Client = S3Client.builder()
|
||||||
|
.httpClient(ApacheHttpClient.create())
|
||||||
|
.region(Region.of(region))
|
||||||
|
.endpointOverride(URI.create(s3endpoint))
|
||||||
|
.credentialsProvider(StaticCredentialsProvider.create(credentials))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return s3Client.getObject(objectRequest, ResponseTransformer.toInputStream());
|
||||||
|
}
|
||||||
|
|
||||||
public void deleteFileFromStorage(String filePath) {
|
public void deleteFileFromStorage(String filePath) {
|
||||||
if (filePath == null || filePath.isEmpty()) {
|
if (filePath == null || filePath.isEmpty()) {
|
||||||
log.warn("Attempted to delete file with empty path");
|
log.warn("Attempted to delete file with empty path");
|
||||||
|
|||||||
Reference in New Issue
Block a user