Refine schedule/todo UI and integrate Google Photos API

This commit is contained in:
kihong.kim
2026-02-01 00:30:32 +09:00
parent 7ddd29dfed
commit c614c883d4
15 changed files with 2124 additions and 565 deletions

View File

@@ -2,13 +2,24 @@ const express = require("express");
const path = require("path");
const fs = require("fs");
const multer = require("multer");
const { google } = require("googleapis");
const axios = require("axios");
const Photo = require("../models/Photo");
const GoogleConfig = require("../models/GoogleConfig");
const router = express.Router();
const uploadsDir = path.join(__dirname, "..", "uploads");
fs.mkdirSync(uploadsDir, { recursive: true });
const oauth2Client = new google.auth.OAuth2(
process.env.GOOGLE_CLIENT_ID,
process.env.GOOGLE_CLIENT_SECRET,
process.env.GOOGLE_REDIRECT_URI
);
const PHOTOS_SCOPE = ["https://www.googleapis.com/auth/photoslibrary.readonly"];
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, uploadsDir);
@@ -22,15 +33,106 @@ const storage = multer.diskStorage({
const upload = multer({ storage });
// OAuth Endpoints
router.get("/auth/url", (req, res) => {
const url = oauth2Client.generateAuthUrl({
access_type: "offline",
scope: PHOTOS_SCOPE,
prompt: "consent",
});
res.json({ url });
});
router.get("/auth/callback", async (req, res) => {
const { code } = req.query;
try {
const { tokens } = await oauth2Client.getToken(code);
await GoogleConfig.findOneAndUpdate(
{ key: "photos_auth" },
{ tokens },
{ upsert: true }
);
res.send("<h1>Authentication successful!</h1><p>You can close this window and return to the application.</p>");
} catch (error) {
console.error("Auth error:", error);
res.status(500).send("Authentication failed");
}
});
router.get("/status", async (req, res) => {
try {
const config = await GoogleConfig.findOne({ key: "photos_auth" });
res.json({ connected: !!config && !!config.tokens });
} catch (error) {
res.status(500).json({ message: "Failed to check status" });
}
});
router.get("/disconnect", async (req, res) => {
try {
await GoogleConfig.deleteOne({ key: "photos_auth" });
res.json({ ok: true });
} catch (error) {
res.status(500).json({ message: "Failed to disconnect" });
}
});
// Fetching Routes
router.get("/", async (req, res) => {
try {
const filter = {};
if (req.query.active === "true") {
filter.active = true;
}
const photos = await Photo.find(filter).sort({ createdAt: -1 });
res.json(photos);
// Get local photos
const localPhotos = await Photo.find(filter).sort({ createdAt: -1 });
// Check if Google Photos is connected
const config = await GoogleConfig.findOne({ key: "photos_auth" });
if (config && config.tokens) {
try {
oauth2Client.setCredentials(config.tokens);
// Refresh token if needed
if (config.tokens.expiry_date <= Date.now()) {
const { tokens } = await oauth2Client.refreshAccessToken();
config.tokens = tokens;
await config.save();
oauth2Client.setCredentials(tokens);
}
const tokensInfo = await oauth2Client.getAccessToken();
const accessToken = tokensInfo.token;
const response = await axios.get(
"https://photoslibrary.googleapis.com/v1/mediaItems?pageSize=100",
{
headers: { Authorization: `Bearer ${accessToken}` },
}
);
const googlePhotos = (response.data.mediaItems || []).map((item) => ({
id: item.id,
url: `${item.baseUrl}=w2048-h1024`,
caption: item.description || "Google Photo",
active: true,
source: "google"
}));
// Combine local and google photos
return res.json([...localPhotos, ...googlePhotos]);
} catch (gError) {
console.error("Error fetching Google Photos:", gError);
// Fallback to local photos only if Google fails
return res.json(localPhotos);
}
}
res.json(localPhotos);
} catch (error) {
console.error("Fetch error:", error);
res.status(500).json({ message: "Failed to fetch photos" });
}
});