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 { mkdir, readFile, writeFile } from "node:fs/promises";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
|
|
||||||
@@ -25,13 +25,13 @@ export const storageMode = hasBlobToken() ? "Vercel Blob" : "Local file";
|
|||||||
export async function readCards(): Promise<BoardCard[]> {
|
export async function readCards(): Promise<BoardCard[]> {
|
||||||
if (hasBlobToken()) {
|
if (hasBlobToken()) {
|
||||||
try {
|
try {
|
||||||
const result = await get(dataBlobPath, { access: "public" });
|
const { blobs } = await list({ prefix: dataBlobPath, limit: 1 });
|
||||||
if (!result?.stream) {
|
if (blobs.length === 0) return [];
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
const text = await streamToText(result.stream);
|
const res = await fetch(blobs[0].url);
|
||||||
const parsed = JSON.parse(text) as BoardCard[];
|
if (!res.ok) return [];
|
||||||
|
|
||||||
|
const parsed = (await res.json()) as BoardCard[];
|
||||||
return Array.isArray(parsed) ? parsed : [];
|
return Array.isArray(parsed) ? parsed : [];
|
||||||
} catch {
|
} catch {
|
||||||
return [];
|
return [];
|
||||||
@@ -119,18 +119,3 @@ export function publicCards(cards: BoardCard[]) {
|
|||||||
function safeFileName(name: string) {
|
function safeFileName(name: string) {
|
||||||
return name.replace(/[^a-zA-Z0-9._-]/g, "-").replace(/-+/g, "-");
|
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