You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

47 lines
1.4 KiB

/**
* Utilities for nostr.build media URLs.
*
* Thumbnails at `/thumb/<path>` are served on **i.nostr.build** only. Other hosts
* (e.g. **cdn.nostr.build**) do not provide that route — never rewrite those URLs.
*
* The /thumb/ route is for **images** only — never apply it to video URLs.
*/
import { isVideo } from './url'
const I_NOSTR_BUILD = 'i.nostr.build'
/**
* True when we may rewrite `url` to i.nostr.build’s `/thumb/…` variant.
* Only **i.nostr.build** serves generated thumbs; cdn.nostr.build does not.
*/
export function canUseNostrBuildThumb(url: string): boolean {
const u = (url ?? '').trim()
if (!u) return false
if (isVideo(u)) return false
try {
const parsed = new URL(u)
if (parsed.hostname !== I_NOSTR_BUILD) return false
const p = parsed.pathname
return p !== '/thumb' && !p.startsWith('/thumb/')
} catch {
return false
}
}
/**
* Returns the i.nostr.build thumbnail URL for `url` (insert `/thumb` before the path).
* Returns `url` unchanged if not on i.nostr.build, already under /thumb/, or invalid.
*/
export function toNostrBuildThumbUrl(url: string): string {
const u = (url ?? '').trim()
if (!canUseNostrBuildThumb(u)) return u
try {
const parsed = new URL(u)
const p = parsed.pathname || '/'
parsed.pathname = '/thumb' + (p.startsWith('/') ? p : `/${p}`)
return parsed.toString()
} catch {
return u
}
}