The first commit, the core and all files is adding to repo
This commit is contained in:
62
lib/catalog.ts
Normal file
62
lib/catalog.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import "server-only";
|
||||
import { admin, required } from "./supabase";
|
||||
export type MapListing = {
|
||||
id: string;
|
||||
name: string;
|
||||
description: string;
|
||||
source: "plugin" | "fabric";
|
||||
state: string;
|
||||
bounds: { minX: number; minZ: number; maxX: number; maxZ: number };
|
||||
panorama_count: number;
|
||||
server_id: string;
|
||||
owner_id: string;
|
||||
cover_path: string | null;
|
||||
map_path: string | null;
|
||||
rejection_reason: string | null;
|
||||
image?: string;
|
||||
mapImage?: string;
|
||||
credit?: string;
|
||||
};
|
||||
export async function mediaUrl(path: string | null, expires = 3600) {
|
||||
if (!path) return null;
|
||||
const { data, error } = await admin()
|
||||
.storage.from("minegeo-media")
|
||||
.createSignedUrl(path, expires);
|
||||
if (error) throw new Error(error.message);
|
||||
return data.signedUrl;
|
||||
}
|
||||
export async function catalog() {
|
||||
const client = admin();
|
||||
const maps = required(
|
||||
await client
|
||||
.from("datasets")
|
||||
.select("*")
|
||||
.eq("state", "published")
|
||||
.order("created_at", { ascending: false }),
|
||||
) as unknown as MapListing[];
|
||||
const servers = required(
|
||||
await client.from("catalog_servers").select("*"),
|
||||
) as { id: string; name: string; address: string | null }[];
|
||||
return Promise.all(
|
||||
maps.map(async (m) => ({
|
||||
...m,
|
||||
image: (await mediaUrl(m.cover_path)) ?? undefined,
|
||||
server: servers.find((s) => s.id === m.server_id),
|
||||
})),
|
||||
);
|
||||
}
|
||||
export async function getMap(id: string) {
|
||||
const map = required(
|
||||
await admin()
|
||||
.from("datasets")
|
||||
.select("*")
|
||||
.eq("id", id)
|
||||
.eq("state", "published")
|
||||
.single(),
|
||||
) as MapListing;
|
||||
return {
|
||||
...map,
|
||||
image: await mediaUrl(map.cover_path),
|
||||
mapImage: await mediaUrl(map.map_path),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user