-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstorage.ts
More file actions
62 lines (49 loc) · 1.48 KB
/
storage.ts
File metadata and controls
62 lines (49 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const PRODUCTION_SUPABASE_HOST = "mfnaqdyunuafbwukbbyr.supabase.co";
const STORAGE_PUBLIC_PATH = "/storage/v1/object/public";
function normaliseAssetPath(assetPath: string) {
return assetPath.replace(/^\/+/, "");
}
function getSupabaseHost() {
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
if (!supabaseUrl) return null;
try {
return new URL(supabaseUrl).hostname;
} catch {
return null;
}
}
export function getStoragePublicUrl(bucket: string, assetPath: string) {
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
if (!supabaseUrl) return null;
return `${supabaseUrl.replace(/\/$/, "")}${STORAGE_PUBLIC_PATH}/${bucket}/${normaliseAssetPath(assetPath)}`;
}
export function usesHostedStaticAssets() {
return getSupabaseHost() === PRODUCTION_SUPABASE_HOST;
}
export function getStaticAssetUrl(
assetPath: string,
localFallbackPath: string
) {
if (!usesHostedStaticAssets()) {
return localFallbackPath;
}
return getStoragePublicUrl("static", assetPath) ?? localFallbackPath;
}
export function getStaticFontUrl(assetPath: string) {
return getStoragePublicUrl(
"static",
`fonts/${normaliseAssetPath(assetPath)}`
);
}
export function getPromoKitUrl() {
return getStaticAssetUrl("promo-kit.zip", "/fallbacks/promo-kit.txt");
}
export function getNewsletterIssueImageUrl(
issueNumber: number | string,
assetFile: string
) {
return getStaticAssetUrl(
`newsletter/${issueNumber}/${assetFile}`,
"/map-tiles/hero.jpg"
);
}