This commit is contained in:
@@ -22,9 +22,8 @@ public class ImageFoundRequestHandler implements RequestHandler {
|
||||
|
||||
@Override
|
||||
public BaseResponse handle(BaseRequest request) throws Exception {
|
||||
|
||||
ImageSearchRequest imageSearchRequest = objectMapper.convertValue(
|
||||
request.getMessageBody(), ImageSearchRequest.class);
|
||||
ImageSearchRequest imageSearchRequest = objectMapper.convertValue(request.getMessageBody(),
|
||||
ImageSearchRequest.class);
|
||||
|
||||
String fileId = imageSearchRequest.getFileId();
|
||||
|
||||
|
||||
@@ -8,8 +8,12 @@ import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.soune.nocopy.dto.BaseResponse;
|
||||
import ru.soune.nocopy.dto.MessageCode;
|
||||
import ru.soune.nocopy.dto.file.YandexSearchResponse;
|
||||
import ru.soune.nocopy.entity.file.FileEntity;
|
||||
import ru.soune.nocopy.exception.FileEntityNotFoundException;
|
||||
import ru.soune.nocopy.exception.NotValidFieldException;
|
||||
import ru.soune.nocopy.repository.FileEntityRepository;
|
||||
|
||||
import java.io.*;
|
||||
@@ -18,6 +22,7 @@ import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Base64;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@@ -45,18 +50,19 @@ public class YandexSearchService {
|
||||
public YandexSearchResponse searchByFileEntity(String fileId) throws IOException {
|
||||
FileEntity fileEntity = fileEntityRepository.findById(fileId)
|
||||
.orElseThrow(() -> {
|
||||
log.error("File not found: {}", fileId);
|
||||
return new RuntimeException("ФFile not found: " + fileId);
|
||||
throw new NotValidFieldException("File not found", new BaseResponse(20007,
|
||||
MessageCode.FILE_NOT_FOUND.getCode(), MessageCode.FILE_NOT_FOUND.getDescription(),
|
||||
Map.of("fileId",fileId)));
|
||||
});
|
||||
|
||||
|
||||
if (!isImageFile(fileEntity)) {
|
||||
log.error("File not image: {}", fileEntity.getMimeType());
|
||||
throw new RuntimeException("File not image: " + fileId);
|
||||
throw new NotValidFieldException("File not image", new BaseResponse(20007,
|
||||
MessageCode.INVALID_FIELD.getCode(), MessageCode.INVALID_FIELD.getDescription(),
|
||||
Map.of("file_type", fileEntity.getMimeType())));
|
||||
}
|
||||
|
||||
byte[] fileBytes = readFileFromDisk(fileEntity);
|
||||
log.info("Прочитано {} байт с диска", fileBytes.length);
|
||||
|
||||
return callYandexApi(fileBytes);
|
||||
}
|
||||
@@ -93,7 +99,7 @@ public class YandexSearchService {
|
||||
connection.setRequestProperty("Authorization", "Api-Key " + apiKey);
|
||||
connection.setRequestProperty("Content-Type", "application/json");
|
||||
connection.setRequestProperty("Accept", "application/json");
|
||||
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
|
||||
// connection.setRequestProperty("User-Agent", "Mozilla/5.0");
|
||||
connection.setConnectTimeout(30000);
|
||||
connection.setReadTimeout(30000);
|
||||
connection.setDoOutput(true);
|
||||
@@ -117,7 +123,7 @@ public class YandexSearchService {
|
||||
new InputStreamReader(connection.getErrorStream(), "utf-8"))) {
|
||||
responseBody = readAll(br);
|
||||
}
|
||||
throw new IOException("Ошибка Yandex API: " + responseCode + " - " + responseBody);
|
||||
throw new IOException("Error Yandex API: " + responseCode + " - " + responseBody);
|
||||
}
|
||||
|
||||
return parseJsonResponse(responseBody);
|
||||
|
||||
Reference in New Issue
Block a user