Fix advertising admin flows, tracking endpoints and ffmpeg timeout

- createAdvertising saved an IMAGE ad without mediaKey and only then answered
  400; validation now happens first. Target URLs must be absolute http(s)
  (they end up in href on the client), DTOs get @NotNull/@NotBlank.
- confirm-upload accepted any sourceKey; the transcoder deletes the source
  afterwards, so another ad's finished video could be deleted. Keys must now
  belong to the ad's own sources/ prefix.
- Activating a fullscreen ad could hit uq_fullscreen_ad_single_active because
  Hibernate may flush the new ACTIVE row first; the old one is flushed first.
- Campaign status change answered with the previous status (bulk UPDATE vs
  stale entity).
- Impression/click used getReferenceById, so unknown ids failed with 500 on
  the FK at commit; session ids are trimmed to the 64-char column.
- /api/ads/next created a Tomcat HttpSession for every client without
  X-Session-Id and failed with 500 when Redis was down; it now falls back to
  the client IP and returns 204 on Redis errors.
- ffmpeg output was read before waitFor, so the 5-minute timeout never fired;
  output goes to a temp log and its tail is logged on failure.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
SlimusMinus
2026-10-20 01:04:10 +03:00
parent 6e001fbc41
commit a909b1f08b
19 changed files with 231 additions and 91 deletions

View File

@@ -9,6 +9,7 @@ import com.krylov.refound.entity.advertising.Campaign;
import com.krylov.refound.enums.advertising.AdvertisingType;
import com.krylov.refound.enums.advertising.TranscodingStatus;
import com.krylov.refound.repository.advertising.AdvertisingRepository;
import com.krylov.refound.util.AdTargetUrl;
import jakarta.persistence.EntityNotFoundException;
import java.util.List;
import java.util.UUID;
@@ -27,7 +28,7 @@ public class AdvertisingAdminService {
Advertising advertising = new Advertising();
advertising.setCampaign(campaign);
advertising.setType(request.type());
advertising.setTargetUrl(request.targetUrl());
advertising.setTargetUrl(AdTargetUrl.requireValid(request.targetUrl()));
advertising.setWeight(request.weight());
if (request.type() == AdvertisingType.IMAGE) {
advertising.setMediaKey(request.mediaKey());
@@ -47,7 +48,20 @@ public class AdvertisingAdminService {
public String getSourceKey(Long advertisingId,PresignUploadRequestDto request){
String extensionFor = extensionFor(request.contentType());
return "sources/%d/%s%s".formatted(advertisingId, UUID.randomUUID(), extensionFor);
return sourcePrefix(advertisingId) + UUID.randomUUID() + extensionFor;
}
/**
* sourceKey в confirm-upload приходит от клиента, а транскодер после работы
* удаляет исходник. Без проверки можно было подставить ключ готового ролика
* другого объявления — и он бы удалился.
*/
public boolean isOwnSourceKey(Long advertisingId, String sourceKey) {
return sourceKey != null && sourceKey.startsWith(sourcePrefix(advertisingId)) && !sourceKey.contains("..");
}
private static String sourcePrefix(Long advertisingId) {
return "sources/%d/".formatted(advertisingId);
}
public String extensionFor(String contentType) {
@@ -81,7 +95,7 @@ public class AdvertisingAdminService {
public AdvertisingAdminResponseDto updateAdvertising(Long advertisingId, AdvertisingUpdateRequestDto request) {
Advertising advertising = advertisingRepository.findById(advertisingId)
.orElseThrow(() -> new EntityNotFoundException("Advertising not found: " + advertisingId));
advertising.setTargetUrl(request.targetUrl());
advertising.setTargetUrl(AdTargetUrl.requireValid(request.targetUrl()));
advertising.setWeight(request.weight());
Advertising saved = advertisingRepository.save(advertising);
log.info("Advertising {} updated", saved.getId());