Rocky_Mountain_Vending/lib/manuals-storage.ts

60 lines
1.4 KiB
TypeScript

import { businessConfig } from "@/lib/seo-config"
function cleanBaseUrl(value: string) {
return value.replace(/\/$/, "")
}
function getSiteBaseUrl() {
return cleanBaseUrl(
process.env.NEXT_PUBLIC_SITE_URL ||
process.env.VOICE_ASSISTANT_SITE_URL ||
businessConfig.website
)
}
export function getManualsAssetBaseUrl() {
return cleanBaseUrl(
process.env.NEXT_PUBLIC_MANUALS_BASE_URL ||
`${getSiteBaseUrl()}/api/manuals`
)
}
export function getThumbnailsAssetBaseUrl() {
return cleanBaseUrl(
process.env.NEXT_PUBLIC_THUMBNAILS_BASE_URL ||
`${getSiteBaseUrl()}/api/thumbnails`
)
}
export function getManualsAssetSource() {
return process.env.NEXT_PUBLIC_MANUALS_BASE_URL &&
process.env.NEXT_PUBLIC_THUMBNAILS_BASE_URL
? "external-object-storage"
: "site-proxy"
}
export function buildManualAssetUrl(relativePath: string) {
const encodedPath = relativePath
.split("/")
.map((segment) => encodeURIComponent(decodeURIComponentSafe(segment)))
.join("/")
return `${getManualsAssetBaseUrl()}/${encodedPath}`
}
export function buildThumbnailAssetUrl(relativePath: string) {
const encodedPath = relativePath
.split("/")
.map((segment) => encodeURIComponent(decodeURIComponentSafe(segment)))
.join("/")
return `${getThumbnailsAssetBaseUrl()}/${encodedPath}`
}
function decodeURIComponentSafe(value: string) {
try {
return decodeURIComponent(value)
} catch {
return value
}
}