fix(back-end) url construction for media

This commit is contained in:
2026-03-10 12:42:26 +01:00
parent 42e0e75d70
commit 5f815d8a54
7 changed files with 26 additions and 14 deletions

View File

@@ -18,15 +18,15 @@ public class MediaStorageService {
private final Path originalRootLocation;
private final Path publicRootLocation;
private final Path privateRootLocation;
private final String publicBaseUrl;
private final String frontendBaseUrl;
public MediaStorageService(@Value("${media.storage.root:storage_media}") String storageRoot,
@Value("${media.public.base-url:http://localhost:8080/media}") String publicBaseUrl) {
@Value("${app.frontend.base-url:${APP_FRONTEND_BASE_URL:http://localhost:8080}}") String frontendBaseUrl) {
this.normalizedRootLocation = Paths.get(storageRoot).toAbsolutePath().normalize();
this.originalRootLocation = normalizedRootLocation.resolve("original").normalize();
this.publicRootLocation = normalizedRootLocation.resolve("public").normalize();
this.privateRootLocation = normalizedRootLocation.resolve("private").normalize();
this.publicBaseUrl = publicBaseUrl;
this.frontendBaseUrl = frontendBaseUrl;
init();
}
@@ -73,11 +73,12 @@ public class MediaStorageService {
if (storageKey == null || storageKey.isBlank()) {
return null;
}
String mediaBaseUrl = buildMediaBaseUrl();
String normalizedKey = storageKey.startsWith("/") ? storageKey.substring(1) : storageKey;
if (publicBaseUrl.endsWith("/")) {
return publicBaseUrl + normalizedKey;
if (mediaBaseUrl.endsWith("/")) {
return mediaBaseUrl + normalizedKey;
}
return publicBaseUrl + "/" + normalizedKey;
return mediaBaseUrl + "/" + normalizedKey;
}
private void copy(Path source, Path destination) throws IOException {
@@ -127,4 +128,15 @@ public class MediaStorageService {
}
return visibility.trim().toUpperCase(Locale.ROOT);
}
private String buildMediaBaseUrl() {
String normalized = frontendBaseUrl != null ? frontendBaseUrl.trim() : "";
if (normalized.isBlank()) {
normalized = "http://localhost:4200";
}
if (normalized.endsWith("/")) {
normalized = normalized.substring(0, normalized.length() - 1);
}
return normalized + "/media";
}
}

View File

@@ -9,5 +9,4 @@ admin.session.ttl-minutes=480
# Local media storage served by a local static server on port 8081.
media.storage.root=/Users/joe/IdeaProjects/print-calculator/storage_media
media.public.base-url=http://localhost:8081
media.ffmpeg.path=ffmpeg

View File

@@ -28,7 +28,6 @@ clamav.enabled=${CLAMAV_ENABLED:false}
# Media configuration
media.storage.root=${MEDIA_STORAGE_ROOT:storage_media}
media.public.base-url=${MEDIA_PUBLIC_BASE_URL:http://localhost:8080/media}
media.ffmpeg.path=${MEDIA_FFMPEG_PATH:ffmpeg}
media.upload.max-file-size-bytes=${MEDIA_UPLOAD_MAX_FILE_SIZE_BYTES:26214400}
shop.model.max-file-size-bytes=${SHOP_MODEL_MAX_FILE_SIZE_BYTES:104857600}