This commit is contained in:
@@ -74,6 +74,8 @@ public class ApiController {
|
||||
|
||||
private final AuthTokenRepository authTokenRepository;
|
||||
|
||||
private final CloudStorageService cloudStorageService;
|
||||
|
||||
@PostMapping("/v{version}/data")
|
||||
public ResponseEntity<?> handlePostRequest(@RequestBody BaseRequest request,
|
||||
@PathVariable("version") int version) {
|
||||
@@ -292,8 +294,6 @@ public class ApiController {
|
||||
}
|
||||
}
|
||||
|
||||
private final CloudStorageService cloudStorageService;
|
||||
|
||||
@GetMapping("/v{version}/files/download/{fileId}")
|
||||
public ResponseEntity<?> downloadFile(
|
||||
@PathVariable(required = false) String fileId,
|
||||
@@ -441,33 +441,6 @@ public class ApiController {
|
||||
return ResponseEntity.ok().body(fileTypeStats);
|
||||
}
|
||||
|
||||
private boolean hasDuplicate(List<SimilarFileDTO> similarFiles) {
|
||||
return similarFiles.stream().anyMatch(f -> f.getHammingDistance() <= 5);
|
||||
}
|
||||
|
||||
private ResponseEntity<BaseResponse> handleDuplicate(FileEntity fileEntity, List<SimilarFileDTO> similarFiles)
|
||||
throws IOException {
|
||||
fileEntityService.deleteFromDisk(fileEntity);
|
||||
|
||||
fileEntityService.softDeleteFileWithHash(fileEntity);
|
||||
|
||||
Optional<FileEntity> originalFile = fileEntityRepository.findById(similarFiles.get(0).getFileId());
|
||||
|
||||
if (originalFile.isPresent()) {
|
||||
Map<String, String> duplicateInfo = Map.of(
|
||||
"duplicate_file_id", originalFile.get().getId(),
|
||||
"owner_user_id", String.valueOf(originalFile.get().getUserId()));
|
||||
|
||||
return ResponseEntity.ok().body(new BaseResponse(
|
||||
20004,
|
||||
MessageCode.DUPLICATE_FILE_UPLOAD.getCode(),
|
||||
"Failed to upload chunk, duplicate",
|
||||
duplicateInfo));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private ResponseEntity<BaseResponse> buildSuccessResponse(String uploadId, Integer chunkNumber, MultipartFile chunk,
|
||||
String fileId) {
|
||||
ChunkUploadResponse responseBody = ChunkUploadResponse.builder()
|
||||
|
||||
@@ -1,86 +0,0 @@
|
||||
package ru.soune.nocopy.controller;
|
||||
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponses;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.AwsCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||
import software.amazon.awssdk.core.sync.RequestBody;
|
||||
import software.amazon.awssdk.http.apache.ApacheHttpClient;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
|
||||
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
|
||||
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/photos")
|
||||
@Tag(name = "Photos")
|
||||
@ApiResponses(@ApiResponse(responseCode = "200", useReturnTypeSchema = true))
|
||||
public class PhotoController {
|
||||
|
||||
private static final String KEY_ID = "YCAJEmHZcWxzf4oGWvETG3mms";
|
||||
private static final String SECRET_KEY = "YCPAPZ32XC-LWb8xE0b_xTcBD8NZUCHpSOfu8LUG";
|
||||
private static final String REGION = "ru-central1";
|
||||
private static final String S3_ENDPOINT = "https://storage.yandexcloud.net";
|
||||
|
||||
private static final String BUCKET = "ncp-test-storage";
|
||||
|
||||
private final S3Client s3Client;
|
||||
|
||||
public PhotoController() {
|
||||
AwsCredentials credentials = AwsBasicCredentials.create(KEY_ID, SECRET_KEY);
|
||||
|
||||
s3Client = S3Client.builder()
|
||||
.httpClient(ApacheHttpClient.create())
|
||||
.region(Region.of(REGION))
|
||||
.endpointOverride(URI.create(S3_ENDPOINT))
|
||||
.credentialsProvider(StaticCredentialsProvider.create(credentials))
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
@PutMapping(consumes = MULTIPART_FORM_DATA_VALUE)
|
||||
public String uploadFile(@RequestParam MultipartFile photo) throws IOException {
|
||||
|
||||
String key = "photos/" + photo.getOriginalFilename();
|
||||
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
|
||||
.bucket(BUCKET)
|
||||
.key(key)
|
||||
.contentType(photo.getContentType())
|
||||
.build();
|
||||
|
||||
s3Client.putObject(putObjectRequest, RequestBody.fromBytes(photo.getBytes()));
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public ResponseEntity<byte[]> downloadFile(@RequestParam String key) throws IOException {
|
||||
GetObjectRequest objectRequest = GetObjectRequest.builder()
|
||||
.bucket(BUCKET)
|
||||
.key(key)
|
||||
.build();
|
||||
|
||||
var inputStream = s3Client.getObject(objectRequest);
|
||||
byte[] data = inputStream.readAllBytes();
|
||||
|
||||
var headers = new HttpHeaders();
|
||||
headers.add(HttpHeaders.CONTENT_DISPOSITION, "inline");
|
||||
headers.add(HttpHeaders.CONTENT_TYPE, inputStream.response().contentType());
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.headers(headers)
|
||||
.body(data);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user