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
7 changes: 6 additions & 1 deletion src/app/api/photos/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import { fetchPhotosWithMetadata, getFolders, getTags } from "@/lib/photos";
export const revalidate = 30;

export async function GET(request: NextRequest) {
console.log("[v0] /api/photos GET request received");
try {
const { searchParams } = new URL(request.url);
const limit = Number(searchParams.get("limit")) || 0;
const offset = Number(searchParams.get("offset")) || 0;

console.log("[v0] Calling fetchPhotosWithMetadata...");
const allPhotos = await fetchPhotosWithMetadata();
console.log("[v0] fetchPhotosWithMetadata returned", allPhotos.length, "photos");
const folders = getFolders(allPhotos);
const tags = getTags(allPhotos);
const total = allPhotos.length;
Expand All @@ -28,6 +31,7 @@ export async function GET(request: NextRequest) {
}, allPhotos[0].processedAt)
: "";

console.log("[v0] /api/photos responding with", photos.length, "photos,", folders.length, "folders,", tags.length, "tags");
return NextResponse.json({
photos,
folders,
Expand All @@ -36,7 +40,8 @@ export async function GET(request: NextRequest) {
total,
hasMore: limit > 0 ? offset + limit < total : false,
});
} catch {
} catch (error) {
console.error("[v0] /api/photos ERROR:", error);
return NextResponse.json(
{ error: "Failed to fetch photos" },
{ status: 500 }
Expand Down
5 changes: 5 additions & 0 deletions src/lib/drive-sa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,13 @@ let cachedToken: { token: string; expiry: number } | null = null;
async function getAccessToken(): Promise<string> {
// Return cached token if still valid (with 5 min buffer)
if (cachedToken && cachedToken.expiry > Date.now() + 5 * 60 * 1000) {
console.log("[v0] Using cached access token");
return cachedToken.token;
}

console.log("[v0] Creating new JWT for token exchange...");
const jwt = await createJWT();
console.log("[v0] JWT created, exchanging for access token...");

const res = await fetch("https://oauth2.googleapis.com/token", {
method: "POST",
Expand All @@ -86,10 +89,12 @@ async function getAccessToken(): Promise<string> {

if (!res.ok) {
const error = await res.text();
console.error("[v0] Token exchange FAILED:", res.status, error);
throw new Error(`Failed to get access token: ${error}`);
}

const data = await res.json();
console.log("[v0] Token exchange SUCCESS, expires in", data.expires_in, "seconds");
cachedToken = {
token: data.access_token,
expiry: Date.now() + data.expires_in * 1000,
Expand Down
15 changes: 14 additions & 1 deletion src/lib/photos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,25 @@ export async function fetchPhotosFromDriveFolder(): Promise<PhotoRecord[]> {
const { driveFolderId, googleApiKey } = config;
const useServiceAccount = isServiceAccountConfigured();

console.log("[v0] fetchPhotosFromDriveFolder START");
console.log("[v0] - driveFolderId:", driveFolderId || "MISSING");
console.log("[v0] - useServiceAccount:", useServiceAccount);
console.log("[v0] - googleApiKey:", googleApiKey ? "SET" : "MISSING");

if (!driveFolderId) {
console.log("[v0] No driveFolderId, returning empty");
return [];
}

// Must have either Service Account or API Key configured
if (!useServiceAccount && !googleApiKey) {
console.log("[v0] No auth method configured, returning empty");
return [];
}

try {
const opts = { revalidate: 30 };
console.log("[v0] Fetching from Drive with", useServiceAccount ? "Service Account" : "API Key");

// Use Service Account if configured, otherwise fall back to API Key
const [rootFiles, subfolders] = useServiceAccount
Expand All @@ -146,6 +154,9 @@ export async function fetchPhotosFromDriveFolder(): Promise<PhotoRecord[]> {
listDriveSubfolders(driveFolderId, googleApiKey, opts),
]);

console.log("[v0] Root files found:", rootFiles.length);
console.log("[v0] Subfolders found:", subfolders.length);

const allPhotos = driveFilesToPhotos(rootFiles, "Root");

if (subfolders.length > 0) {
Expand All @@ -165,8 +176,10 @@ export async function fetchPhotosFromDriveFolder(): Promise<PhotoRecord[]> {
);
allPhotos.forEach((p, i) => { p.id = String(i + 1); });

console.log("[v0] fetchPhotosFromDriveFolder COMPLETE - total photos:", allPhotos.length);
return allPhotos;
} catch {
} catch (error) {
console.error("[v0] fetchPhotosFromDriveFolder ERROR:", error);
return [];
}
}
Expand Down
Loading