- 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>
48 lines
1.8 KiB
Java
48 lines
1.8 KiB
Java
package com.krylov.refound.util;
|
|
|
|
import com.krylov.refound.enums.ErrorCode;
|
|
import com.krylov.refound.exception.ApiException;
|
|
import java.net.URI;
|
|
import java.net.URISyntaxException;
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
/**
|
|
* Ссылка, куда ведёт клик по рекламе. Фронт вставляет её в href, поэтому
|
|
* разрешены только абсолютные http(s)-адреса: "javascript:..." или "data:..."
|
|
* выполнились бы в браузере пользователя.
|
|
*/
|
|
public final class AdTargetUrl {
|
|
|
|
/** Колонка advertising.target_url — VARCHAR(1024). */
|
|
public static final int MAX_LENGTH = 1024;
|
|
|
|
private AdTargetUrl() {
|
|
}
|
|
|
|
/** Проверяет ссылку и возвращает её без пробелов по краям; 400 при ошибке. */
|
|
public static String requireValid(String url) {
|
|
if (url == null || url.isBlank()) {
|
|
throw invalid("Ссылка рекламы обязательна");
|
|
}
|
|
String trimmed = url.trim();
|
|
if (trimmed.length() > MAX_LENGTH) {
|
|
throw invalid("Ссылка рекламы длиннее " + MAX_LENGTH + " символов");
|
|
}
|
|
try {
|
|
URI uri = new URI(trimmed);
|
|
String scheme = uri.getScheme();
|
|
if (scheme == null || !(scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https"))
|
|
|| uri.getHost() == null) {
|
|
throw invalid("Ссылка рекламы должна быть абсолютным http(s)-адресом");
|
|
}
|
|
} catch (URISyntaxException e) {
|
|
throw invalid("Некорректная ссылка рекламы");
|
|
}
|
|
return trimmed;
|
|
}
|
|
|
|
private static ApiException invalid(String message) {
|
|
return new ApiException(ErrorCode.VALIDATION_ERROR, message, HttpStatus.BAD_REQUEST);
|
|
}
|
|
}
|