This commit is contained in:
@@ -90,10 +90,10 @@ public class ImageFoundRequestHandler implements RequestHandler {
|
||||
allUniqueImages = searchImageService.removeDuplicateUrls(allYandexImages, allGoogleImages);
|
||||
log.info("allUniqueImages OK: {} images", allUniqueImages.size());
|
||||
|
||||
if (!allUniqueImages.isEmpty()) {
|
||||
tariffInfoService.writeOffTokens(fileEntity.getUserId(), TariffConstants.TOKEN_VALUE_FOR_SEARCH);
|
||||
checkCounterService.incrementCheckCount(fileEntity.getUserId(), fileEntity.getMimeType());
|
||||
}
|
||||
// if (!allUniqueImages.isEmpty()) {
|
||||
tariffInfoService.writeOffTokens(fileEntity.getUserId(), TariffConstants.TOKEN_VALUE_FOR_SEARCH);
|
||||
checkCounterService.incrementCheckCount(fileEntity.getUserId(), fileEntity.getMimeType());
|
||||
// }
|
||||
|
||||
int page = imageSearchRequest.getPage() != null ? imageSearchRequest.getPage() : 1;
|
||||
int pageSize = 5;
|
||||
|
||||
@@ -9,6 +9,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.soune.nocopy.entity.monitoring.FileMonitoringEntity;
|
||||
import ru.soune.nocopy.repository.FileMonitoringRepository;
|
||||
import ru.soune.nocopy.service.monitoring.MonitoringSearchService;
|
||||
import ru.soune.nocopy.service.tariff.TariffInfoService;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@@ -19,6 +19,7 @@ import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.SecureRandom;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@@ -47,20 +48,34 @@ public class EmailService {
|
||||
@Value("${server.front.port}")
|
||||
private String frontPort;
|
||||
|
||||
public void sendVerificationEmail(User user, String code) throws MessagingException, IOException {
|
||||
public void sendEmail(String from, String sendTo, String subject, String body) throws MessagingException {
|
||||
MimeMessage message = mailSender.createMimeMessage();
|
||||
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
|
||||
|
||||
helper.setFrom("noreply@no-copy.ru");
|
||||
helper.setTo(user.getEmail());
|
||||
helper.setSubject(standartSubject);
|
||||
String htmlContent = loadAndProcessTemplate(user.getFullName(), code,
|
||||
"templates/email-verification.html");
|
||||
helper.setText(htmlContent, true);
|
||||
helper.setFrom(from);
|
||||
helper.setTo(sendTo);
|
||||
helper.setSubject(subject);
|
||||
helper.setText(body, true);
|
||||
|
||||
mailSender.send(message);
|
||||
}
|
||||
|
||||
public void sendVerificationEmail(User user, String code) throws MessagingException, IOException {
|
||||
String htmlContent = loadAndProcessTemplate(Map.of("username", user.getFullName(), "code", code),
|
||||
"templates/email-verification.html");
|
||||
sendEmail("noreply@no-copy.ru", user.getEmail(), standartAdressFrom, htmlContent);
|
||||
}
|
||||
|
||||
public void sendTokensNotFoundEmail(User user, Integer currentTokens, Integer missingTokens)
|
||||
throws MessagingException, IOException {
|
||||
String htmlContent = loadAndProcessTemplate(Map.of("username", user.getFullName(),
|
||||
"current_tokens", String.valueOf(currentTokens), "missing_tokens",
|
||||
String.valueOf(missingTokens), "link_to_token_board", baseUrl + "/pages/payment"),
|
||||
"templates/token-not-found.html");
|
||||
|
||||
sendEmail("noreply@no-copy.ru", user.getEmail(), standartAdressFrom, htmlContent);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public EmailVerificationToken createEmailVerificationToken(AuthToken authToken) {
|
||||
EmailVerificationToken existingToken = emailVerificationTokenRepository
|
||||
@@ -96,6 +111,18 @@ public class EmailService {
|
||||
.replace("{{code}}", safeCode);
|
||||
}
|
||||
|
||||
private String loadAndProcessTemplate(Map<String, String> fieldsForTemplate, String templatePath) throws IOException {
|
||||
ClassPathResource resource = new ClassPathResource(templatePath);
|
||||
String template = new String(resource.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
|
||||
|
||||
for (Map.Entry<String, String> entry : fieldsForTemplate.entrySet()) {
|
||||
template = template.replace("{{" + entry.getKey() + "}}", escapeHtml(entry.getValue()));
|
||||
}
|
||||
|
||||
return template;
|
||||
}
|
||||
|
||||
|
||||
private String escapeHtml(String text) {
|
||||
if (text == null) return "";
|
||||
return text
|
||||
|
||||
@@ -1,19 +1,30 @@
|
||||
package ru.soune.nocopy.service.monitoring;
|
||||
|
||||
import jakarta.mail.MessagingException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.soune.nocopy.dto.file.YandexSearchResponse;
|
||||
import ru.soune.nocopy.dto.tarriff.TariffDTO;
|
||||
import ru.soune.nocopy.entity.file.*;
|
||||
import ru.soune.nocopy.entity.monitoring.FileMonitoringEntity;
|
||||
import ru.soune.nocopy.entity.monitoring.FoundViolationEntity;
|
||||
import ru.soune.nocopy.entity.monitoring.MonitoringType;
|
||||
import ru.soune.nocopy.entity.monitoring.SearchEngine;
|
||||
import ru.soune.nocopy.entity.tarif.TariffInfo;
|
||||
import ru.soune.nocopy.entity.tarif.TariffType;
|
||||
import ru.soune.nocopy.entity.user.User;
|
||||
import ru.soune.nocopy.exception.TariffNotFoundException;
|
||||
import ru.soune.nocopy.repository.FileMonitoringRepository;
|
||||
import ru.soune.nocopy.repository.FoundViolationRepository;
|
||||
import ru.soune.nocopy.repository.UserRepository;
|
||||
import ru.soune.nocopy.service.mail.EmailService;
|
||||
import ru.soune.nocopy.service.search.SearchImageService;
|
||||
import ru.soune.nocopy.service.tariff.TariffInfoService;
|
||||
import ru.soune.nocopy.service.tariff.TariffService;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
@@ -30,8 +41,16 @@ public class MonitoringSearchService {
|
||||
|
||||
private final FileMonitoringRepository monitoringRepository;
|
||||
|
||||
private final TariffInfoService tariffInfoService;
|
||||
|
||||
private final TariffService tariffService;
|
||||
|
||||
private final EmailService emailService;
|
||||
|
||||
private final UserRepository userRepository;
|
||||
|
||||
@Transactional
|
||||
public void processFileSearch(FileMonitoringEntity monitoring) {
|
||||
public void processFileSearch(FileMonitoringEntity monitoring) throws MessagingException, IOException {
|
||||
String searchSessionId = UUID.randomUUID().toString();
|
||||
log.info("Starting search session: {} for file: {}", searchSessionId, monitoring.getFile().getId());
|
||||
|
||||
@@ -39,7 +58,12 @@ public class MonitoringSearchService {
|
||||
monitoring.setLastRunStatus("IN_PROGRESS");
|
||||
monitoringRepository.save(monitoring);
|
||||
|
||||
MonitoringType monitoringType = monitoring.getMonitoringType();
|
||||
TariffDTO tariffMonitoring = tariffService.getTariffByType(TariffType.valueOf(monitoringType.name()));
|
||||
|
||||
try {
|
||||
tariffInfoService.writeOffTokens(monitoring.getUserId(), tariffMonitoring.getTokens());
|
||||
|
||||
String yandexResponse = searchImageService.searchReverseByPublicUrl(
|
||||
monitoring.getFile(), "yandex_reverse_image", "visual_matches");
|
||||
|
||||
@@ -62,7 +86,16 @@ public class MonitoringSearchService {
|
||||
}
|
||||
|
||||
monitoring.setLastRunStatus("SUCCESS - Found " + savedCount + " violations");
|
||||
} catch (TariffNotFoundException e) {
|
||||
User user = userRepository.findById(monitoring.getUserId()).orElseThrow();
|
||||
TariffInfo activeTariffInfo = user.getActiveTariffInfo();
|
||||
int currentTokens = activeTariffInfo.getTokens() + activeTariffInfo.getBoughtTokens();
|
||||
|
||||
emailService.sendTokensNotFoundEmail(user, currentTokens, tariffMonitoring.getTokens());
|
||||
monitoring.setLastRunStatus("ERROR: " + e.getMessage());
|
||||
updateNextRun(monitoring);
|
||||
|
||||
monitoringRepository.save(monitoring);
|
||||
} catch (Exception e) {
|
||||
log.error("Search failed for session: {}", searchSessionId, e);
|
||||
monitoring.setLastRunStatus("ERROR: " + e.getMessage());
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package ru.soune.nocopy.service.notification;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class NotificationService {
|
||||
|
||||
}
|
||||
@@ -39,7 +39,7 @@ public class TariffInfoService {
|
||||
|
||||
return tariffInfoRepository.save(tariffInfo);
|
||||
}
|
||||
|
||||
|
||||
@Transactional
|
||||
public void writeOffTokens(long userId, Integer value) {
|
||||
User user = userRepository.findById(userId).orElseThrow();
|
||||
@@ -52,6 +52,10 @@ public class TariffInfoService {
|
||||
activeTariffInfo = user.getCompany().getTariffInfo();
|
||||
}
|
||||
|
||||
writeOffTokensForTariff(activeTariffInfo, value);
|
||||
}
|
||||
|
||||
public void writeOffTokensForTariff(TariffInfo activeTariffInfo, Integer value) {
|
||||
Integer tokens = activeTariffInfo.getTokens();
|
||||
Integer boughtTokens = activeTariffInfo.getBoughtTokens();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user