Add Dropbox integration for secure image storage and retrieval
Integrates Dropbox OAuth for authentication, adds API routes for authorization, token exchange, and fetching gallery images, and updates the gallery API to support fetching images directly from Dropbox. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 517dfa7b-26ac-463d-a6e1-a58c6df97188 Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Event-Id: e2398e24-3175-4cbf-91ff-003984c8d042 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/f209e72a-0939-48fa-84fc-57854de71967/517dfa7b-26ac-463d-a6e1-a58c6df97188/ls5p9ni Replit-Helium-Checkpoint-Created: true
This commit is contained in:
+52
-5
@@ -6,6 +6,7 @@ import { seedDatabase } from "./seed";
|
||||
import { generateDailyHoroscopes, getHoroscopesForToday, getOrGenerateHoroscope } from "./horoscope-generator";
|
||||
import { analyzeAllArticleImages, getCachedFocalPoints } from "./focal-point";
|
||||
import { optimizeImage } from "./image-optimizer";
|
||||
import { getAuthUrl, exchangeCodeForTokens, isConnected, fetchGalleryFromDropbox } from "./dropbox";
|
||||
import multer from "multer";
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
@@ -195,12 +196,58 @@ export async function registerRoutes(
|
||||
}
|
||||
});
|
||||
|
||||
// Gallery API - serves shuffled photos from Dropbox
|
||||
app.get("/api/gallery", (_req, res) => {
|
||||
// Dropbox OAuth
|
||||
app.get("/api/dropbox/auth", (_req, res) => {
|
||||
const host = _req.headers.host || "news-folx-tv.replit.app";
|
||||
const protocol = _req.headers["x-forwarded-proto"] || "https";
|
||||
const redirectUri = `${protocol}://${host}/api/dropbox/callback`;
|
||||
const url = getAuthUrl(redirectUri);
|
||||
res.redirect(url);
|
||||
});
|
||||
|
||||
app.get("/api/dropbox/callback", async (req, res) => {
|
||||
const code = req.query.code as string;
|
||||
if (!code) {
|
||||
return res.status(400).send("Manjka avtorizacijska koda.");
|
||||
}
|
||||
try {
|
||||
const galleryPath = path.join(process.cwd(), "server/gallery-data.json");
|
||||
const data = JSON.parse(fs.readFileSync(galleryPath, "utf-8"));
|
||||
// Shuffle using Fisher-Yates
|
||||
const host = req.headers.host || "news-folx-tv.replit.app";
|
||||
const protocol = req.headers["x-forwarded-proto"] || "https";
|
||||
const redirectUri = `${protocol}://${host}/api/dropbox/callback`;
|
||||
await exchangeCodeForTokens(code, redirectUri);
|
||||
res.send("<html><body style='background:#111;color:#fff;font-family:sans-serif;text-align:center;padding:60px'><h1>Dropbox povezan!</h1><p>Lahko zaprete to stran.</p></body></html>");
|
||||
} catch (err: any) {
|
||||
res.status(500).send(`Napaka: ${err.message}`);
|
||||
}
|
||||
});
|
||||
|
||||
app.get("/api/dropbox/status", (_req, res) => {
|
||||
res.json({ connected: isConnected() });
|
||||
});
|
||||
|
||||
app.post("/api/dropbox/refresh-gallery", async (_req, res) => {
|
||||
try {
|
||||
const images = await fetchGalleryFromDropbox();
|
||||
res.json({ count: images.length });
|
||||
} catch (err: any) {
|
||||
res.status(500).json({ message: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Gallery API - serves shuffled photos from Dropbox
|
||||
app.get("/api/gallery", async (_req, res) => {
|
||||
try {
|
||||
let data: any[] = [];
|
||||
|
||||
if (isConnected()) {
|
||||
data = await fetchGalleryFromDropbox();
|
||||
}
|
||||
|
||||
if (data.length === 0) {
|
||||
const galleryPath = path.join(process.cwd(), "server/gallery-data.json");
|
||||
data = JSON.parse(fs.readFileSync(galleryPath, "utf-8"));
|
||||
}
|
||||
|
||||
const shuffled = [...data];
|
||||
for (let i = shuffled.length - 1; i > 0; i--) {
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
|
||||
Reference in New Issue
Block a user