@@ -16,6 +16,8 @@ import ru.soune.nocopy.repository.FileEntityRepository;
|
|||||||
import ru.soune.nocopy.repository.UserRepository;
|
import ru.soune.nocopy.repository.UserRepository;
|
||||||
import ru.soune.nocopy.service.file.CheckCounterService;
|
import ru.soune.nocopy.service.file.CheckCounterService;
|
||||||
import ru.soune.nocopy.service.file.FileStorageService;
|
import ru.soune.nocopy.service.file.FileStorageService;
|
||||||
|
import org.springframework.http.ContentDisposition;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
@@ -45,10 +47,13 @@ public class FileController {
|
|||||||
|
|
||||||
Resource resource = fileStorageService.loadFileAsResource(fileEntity.getFilePath());
|
Resource resource = fileStorageService.loadFileAsResource(fileEntity.getFilePath());
|
||||||
|
|
||||||
|
ContentDisposition contentDisposition = ContentDisposition.inline()
|
||||||
|
.filename(fileEntity.getOriginalFileName(), StandardCharsets.UTF_8)
|
||||||
|
.build();
|
||||||
|
|
||||||
return ResponseEntity.ok()
|
return ResponseEntity.ok()
|
||||||
.contentType(new MediaType(fileEntity.getMimeType(), fileEntity.getFileExtension()))
|
.contentType(new MediaType(fileEntity.getMimeType(), fileEntity.getFileExtension()))
|
||||||
.header(HttpHeaders.CONTENT_DISPOSITION,
|
.header(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString())
|
||||||
"inline; filename=\"" + fileEntity.getOriginalFileName() + "\"")
|
|
||||||
.body(resource);
|
.body(resource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -102,12 +102,15 @@ public class YandexSearchService {
|
|||||||
return callReverseImageApiByUrl(publicUrl);
|
return callReverseImageApiByUrl(publicUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private String callReverseImageApiByUrl(String imageUrl) throws IOException {
|
private String callReverseImageApiByUrl(String imageUrl) throws IOException {
|
||||||
if (searchApiKey == null || searchApiKey.isEmpty()) {
|
if (searchApiKey == null || searchApiKey.isEmpty()) {
|
||||||
throw new IllegalStateException("SearchAPI key not configured. Set searchapi.api-key property");
|
throw new IllegalStateException("SearchAPI key not configured. Set searchapi.api-key property");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (imageUrl == null || imageUrl.trim().isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("Image URL cannot be null or empty");
|
||||||
|
}
|
||||||
|
|
||||||
HttpUrl url = HttpUrl.parse("https://searchapi.io/api/v1/search")
|
HttpUrl url = HttpUrl.parse("https://searchapi.io/api/v1/search")
|
||||||
.newBuilder()
|
.newBuilder()
|
||||||
.addQueryParameter("engine", "yandex_reverse_image")
|
.addQueryParameter("engine", "yandex_reverse_image")
|
||||||
@@ -117,31 +120,87 @@ public class YandexSearchService {
|
|||||||
.addQueryParameter("timeout", "30000")
|
.addQueryParameter("timeout", "30000")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
log.info("Calling Yandex Reverse Image API: {}", url);
|
log.info("Calling Yandex Reverse Image API for URL: {}", imageUrl);
|
||||||
|
log.debug("Full API URL: {}", url);
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
.url(url)
|
.url(url)
|
||||||
.header("Accept", "application/json")
|
.header("Accept", "application/json")
|
||||||
.header("User-Agent", "Mozilla/5.0")
|
.header("User-Agent", "Mozilla/5.0 (compatible; YourApp/1.0)")
|
||||||
|
.header("Accept-Encoding", "gzip")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
try (Response response = httpClient.newCall(request).execute()) {
|
try (Response response = httpClient.newCall(request).execute()) {
|
||||||
|
ResponseBody responseBody = response.body();
|
||||||
|
|
||||||
log.info("Response code: {}", response.code());
|
log.info("Response code: {}", response.code());
|
||||||
log.info("Response headers: {}", response.headers());
|
log.debug("Response headers: {}", response.headers());
|
||||||
|
|
||||||
if (!response.isSuccessful()) {
|
if (!response.isSuccessful()) {
|
||||||
String errorBody = response.body() != null ? response.body().string() : "No error body";
|
String errorBody = responseBody != null ? responseBody.string() : "No error body";
|
||||||
log.error("API request failed: {} - {}", response.code(), errorBody);
|
log.error("API request failed: {} - {}", response.code(), errorBody);
|
||||||
throw new IOException("API request failed: " + response.code() + " - " + response.message());
|
throw new IOException("API request failed: " + response.code() + " - " + response.message());
|
||||||
}
|
}
|
||||||
|
|
||||||
String responseBody = response.body().string();
|
if (responseBody == null) {
|
||||||
log.info("Response body length: {} characters", responseBody.length());
|
log.error("Response body is null for successful request");
|
||||||
|
throw new IOException("Response body is null");
|
||||||
|
}
|
||||||
|
|
||||||
return responseBody;
|
String responseContent = responseBody.string();
|
||||||
|
log.info("Response body length: {} characters", responseContent.length());
|
||||||
|
|
||||||
|
if (log.isDebugEnabled() && responseContent.length() > 0) {
|
||||||
|
log.debug("Response body (first 500 chars): {}",
|
||||||
|
responseContent.substring(0, Math.min(responseContent.length(), 500)));
|
||||||
|
}
|
||||||
|
|
||||||
|
return responseContent;
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("Failed to call Yandex Reverse Image API for URL: {}", imageUrl, e);
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// private String callReverseImageApiByUrl(String imageUrl) throws IOException {
|
||||||
|
// if (searchApiKey == null || searchApiKey.isEmpty()) {
|
||||||
|
// throw new IllegalStateException("SearchAPI key not configured. Set searchapi.api-key property");
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// HttpUrl url = HttpUrl.parse("https://searchapi.io/api/v1/search")
|
||||||
|
// .newBuilder()
|
||||||
|
// .addQueryParameter("engine", "yandex_reverse_image")
|
||||||
|
// .addQueryParameter("api_key", searchApiKey)
|
||||||
|
// .addQueryParameter("url", imageUrl)
|
||||||
|
// .addQueryParameter("wait", "true")
|
||||||
|
// .addQueryParameter("timeout", "30000")
|
||||||
|
// .build();
|
||||||
|
//
|
||||||
|
// log.info("Calling Yandex Reverse Image API: {}", url);
|
||||||
|
//
|
||||||
|
// Request request = new Request.Builder()
|
||||||
|
// .url(url)
|
||||||
|
// .header("Accept", "application/json")
|
||||||
|
// .header("User-Agent", "Mozilla/5.0")
|
||||||
|
// .build();
|
||||||
|
//
|
||||||
|
// try (Response response = httpClient.newCall(request).execute()) {
|
||||||
|
// log.info("Response code: {}", response.code());
|
||||||
|
// log.info("Response headers: {}", response.headers());
|
||||||
|
//
|
||||||
|
// if (!response.isSuccessful()) {
|
||||||
|
// String errorBody = response.body() != null ? response.body().string() : "No error body";
|
||||||
|
// log.error("API request failed: {} - {}", response.code(), errorBody);
|
||||||
|
// throw new IOException("API request failed: " + response.code() + " - " + response.message());
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// String responseBody = response.body().string();
|
||||||
|
// log.info("Response body length: {} characters", responseBody.length());
|
||||||
|
//
|
||||||
|
// return responseBody;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
private boolean isImageFile(FileEntity fileEntity) {
|
private boolean isImageFile(FileEntity fileEntity) {
|
||||||
String mimeType = fileEntity.getMimeType();
|
String mimeType = fileEntity.getMimeType();
|
||||||
|
|||||||
Reference in New Issue
Block a user