44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
/**
|
|
* Как на основном сайте: в API может прийти уже полный URL
|
|
* (`https://storage.yandexcloud.net/bucket/projects/uuid.jpg`) или ключ
|
|
* (`projects/uuid.jpg`) — тогда к нему дописывается VITE_S3_BUCKET.
|
|
*/
|
|
export function resolveProjectImageSrc(image: string): string {
|
|
let src = image
|
|
.trim()
|
|
.replaceAll(""", '"')
|
|
.replace(/^["']+|["']+$/g, "");
|
|
|
|
try {
|
|
if (src.includes("%")) src = decodeURIComponent(src);
|
|
} catch {
|
|
/* оставляем как есть */
|
|
}
|
|
|
|
if (src.startsWith("//")) {
|
|
src = `https:${src}`;
|
|
}
|
|
if (
|
|
src.startsWith("http://") ||
|
|
src.startsWith("https://") ||
|
|
src.startsWith("data:")
|
|
) {
|
|
return src;
|
|
}
|
|
|
|
if (src.startsWith("/")) {
|
|
const base = import.meta.env.BASE_URL;
|
|
if (!base || base === "/") return src;
|
|
return `${base.replace(/\/$/, "")}${src}`;
|
|
}
|
|
|
|
const s3BaseRaw =
|
|
typeof import.meta.env.VITE_S3_BUCKET === "string"
|
|
? import.meta.env.VITE_S3_BUCKET.trim()
|
|
: "";
|
|
if (!s3BaseRaw) return src;
|
|
const base = s3BaseRaw.replace(/\/?$/, "/");
|
|
const path = src.replace(/^\//, "");
|
|
return `${base}${path}`;
|
|
}
|