Fix readCards for public blob store
The get() function doesn't work with relative paths on public stores. Use list() to find the blob URL, then fetch it directly. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
+7
-22
@@ -1,4 +1,4 @@
|
||||
import { del, get, put } from "@vercel/blob";
|
||||
import { del, list, put } from "@vercel/blob";
|
||||
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
|
||||
@@ -25,13 +25,13 @@ export const storageMode = hasBlobToken() ? "Vercel Blob" : "Local file";
|
||||
export async function readCards(): Promise<BoardCard[]> {
|
||||
if (hasBlobToken()) {
|
||||
try {
|
||||
const result = await get(dataBlobPath, { access: "public" });
|
||||
if (!result?.stream) {
|
||||
return [];
|
||||
}
|
||||
const { blobs } = await list({ prefix: dataBlobPath, limit: 1 });
|
||||
if (blobs.length === 0) return [];
|
||||
|
||||
const text = await streamToText(result.stream);
|
||||
const parsed = JSON.parse(text) as BoardCard[];
|
||||
const res = await fetch(blobs[0].url);
|
||||
if (!res.ok) return [];
|
||||
|
||||
const parsed = (await res.json()) as BoardCard[];
|
||||
return Array.isArray(parsed) ? parsed : [];
|
||||
} catch {
|
||||
return [];
|
||||
@@ -119,18 +119,3 @@ export function publicCards(cards: BoardCard[]) {
|
||||
function safeFileName(name: string) {
|
||||
return name.replace(/[^a-zA-Z0-9._-]/g, "-").replace(/-+/g, "-");
|
||||
}
|
||||
|
||||
async function streamToText(stream: ReadableStream<Uint8Array>) {
|
||||
const reader = stream.getReader();
|
||||
const chunks: Uint8Array[] = [];
|
||||
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) {
|
||||
break;
|
||||
}
|
||||
chunks.push(value);
|
||||
}
|
||||
|
||||
return Buffer.concat(chunks).toString("utf8");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user