Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/api/admin/shares/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export async function GET(req: NextRequest) {
if (auth === 'missing') return Response.json({ error: 'Admin not configured' }, { status: 503 });
if (auth === 'unauthorized') return Response.json({ error: 'Unauthorized' }, { status: 401 });

const ids = await listShares();
const metas = await listShares();
const proto = req.headers.get('x-forwarded-proto') ?? 'http';
const host = req.headers.get('host') ?? 'localhost:3000';
const base = process.env.APP_URL ?? `${proto}://${host}`;
const shares = ids.map((id) => ({ id, view: `${base}/view/${id}` }));
const shares = metas.map(({ id, createdAt }) => ({ id, createdAt, view: `${base}/view/${id}` }));
return Response.json({ count: shares.length, shares });
}

Expand Down
36 changes: 30 additions & 6 deletions lib/share-store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import crypto from 'crypto';

export interface ShareMeta {
id: string;
createdAt: string | null;
}

export function computeShareId(params: Record<string, string>): string {
const canonical = JSON.stringify(Object.fromEntries(Object.entries(params).sort()));
return crypto.createHash('sha256').update(canonical).digest('hex').slice(0, 16);
Expand All @@ -10,16 +15,17 @@ function useUpstash(): boolean {
}

export async function saveShare(id: string, params: Record<string, string>): Promise<void> {
const data = { ...params, _created_at: new Date().toISOString() };
if (!useUpstash()) {
const { writeFileSync, mkdirSync } = await import('fs');
const { join } = await import('path');
const dir = join(process.cwd(), 'shares');
mkdirSync(dir, { recursive: true });
writeFileSync(join(dir, `${id}.json`), JSON.stringify(params), 'utf8');
writeFileSync(join(dir, `${id}.json`), JSON.stringify(data), 'utf8');
} else {
const { Redis } = await import('@upstash/redis');
const redis = new Redis({ url: process.env.KV_REST_API_URL!, token: process.env.KV_REST_API_TOKEN! });
await redis.set(`share:${id}`, params);
await redis.set(`share:${id}`, data);
}
}

Expand All @@ -37,15 +43,19 @@ export async function loadShare(id: string): Promise<Record<string, string> | nu
}
}

export async function listShares(): Promise<string[]> {
export async function listShares(): Promise<ShareMeta[]> {
if (!useUpstash()) {
const { readdirSync, existsSync } = await import('fs');
const { readdirSync, existsSync, statSync } = await import('fs');
const { join } = await import('path');
const dir = join(process.cwd(), 'shares');
if (!existsSync(dir)) return [];
return readdirSync(dir)
.filter((f) => f.endsWith('.json'))
.map((f) => f.slice(0, -5));
.map((f) => ({
id: f.slice(0, -5),
createdAt: new Date(statSync(join(dir, f)).mtimeMs).toISOString(),
}))
.sort((a, b) => b.createdAt!.localeCompare(a.createdAt!));
} else {
const { Redis } = await import('@upstash/redis');
const redis = new Redis({ url: process.env.KV_REST_API_URL!, token: process.env.KV_REST_API_TOKEN! });
Expand All @@ -56,7 +66,21 @@ export async function listShares(): Promise<string[]> {
cursor = Number(next);
for (const key of keys) ids.push((key as string).replace(/^share:/, ''));
} while (cursor !== 0);
return ids;
if (ids.length === 0) return [];
const pipeline = redis.pipeline();
for (const id of ids) pipeline.get(`share:${id}`);
const results = await pipeline.exec();
return ids
.map((id, i) => {
const data = results[i] as Record<string, string> | null;
return { id, createdAt: data?._created_at ?? null };
})
.sort((a, b) => {
if (!a.createdAt && !b.createdAt) return 0;
if (!a.createdAt) return 1;
if (!b.createdAt) return -1;
return b.createdAt.localeCompare(a.createdAt);
});
}
}

Expand Down
Loading