mirror of
https://github.com/Tony0410/readlater.git
synced 2026-05-24 22:01:41 +08:00
Initial commit: ReadLater v1.0
- Save articles via URL or bookmarklet - Clean dark reader with customizable fonts/sizing - Text-to-speech with browser + Kokoro support - Speed control up to 3x - Favorites and archive - SQLite database with Drizzle ORM - Docker deployment ready Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
112
src/app/api/articles/[id]/route.ts
Normal file
112
src/app/api/articles/[id]/route.ts
Normal file
@@ -0,0 +1,112 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { db, schema } from "@/lib/db";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
||||
interface RouteParams {
|
||||
params: Promise<{ id: string }>;
|
||||
}
|
||||
|
||||
// GET /api/articles/[id] - Get single article
|
||||
export async function GET(request: NextRequest, { params }: RouteParams) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
|
||||
const article = await db
|
||||
.select()
|
||||
.from(schema.articles)
|
||||
.where(eq(schema.articles.id, id))
|
||||
.limit(1);
|
||||
|
||||
if (article.length === 0) {
|
||||
return NextResponse.json({ error: "Article not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
return NextResponse.json(article[0]);
|
||||
} catch (error) {
|
||||
console.error("Error fetching article:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to fetch article" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// PATCH /api/articles/[id] - Update article
|
||||
export async function PATCH(request: NextRequest, { params }: RouteParams) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const body = await request.json();
|
||||
|
||||
// Only allow updating specific fields
|
||||
const allowedFields = [
|
||||
"isFavorite",
|
||||
"isArchived",
|
||||
"readingProgress",
|
||||
"tags",
|
||||
"readAt",
|
||||
];
|
||||
const updates: Record<string, unknown> = { updatedAt: new Date() };
|
||||
|
||||
for (const field of allowedFields) {
|
||||
if (field in body) {
|
||||
if (field === "tags" && Array.isArray(body[field])) {
|
||||
updates[field] = JSON.stringify(body[field]);
|
||||
} else if (field === "readAt" && body[field]) {
|
||||
updates[field] = new Date(body[field]);
|
||||
} else {
|
||||
updates[field] = body[field];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await db
|
||||
.update(schema.articles)
|
||||
.set(updates)
|
||||
.where(eq(schema.articles.id, id));
|
||||
|
||||
const article = await db
|
||||
.select()
|
||||
.from(schema.articles)
|
||||
.where(eq(schema.articles.id, id))
|
||||
.limit(1);
|
||||
|
||||
if (article.length === 0) {
|
||||
return NextResponse.json({ error: "Article not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
return NextResponse.json(article[0]);
|
||||
} catch (error) {
|
||||
console.error("Error updating article:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to update article" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE /api/articles/[id] - Delete article
|
||||
export async function DELETE(request: NextRequest, { params }: RouteParams) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
|
||||
const existing = await db
|
||||
.select()
|
||||
.from(schema.articles)
|
||||
.where(eq(schema.articles.id, id))
|
||||
.limit(1);
|
||||
|
||||
if (existing.length === 0) {
|
||||
return NextResponse.json({ error: "Article not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
await db.delete(schema.articles).where(eq(schema.articles.id, id));
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error("Error deleting article:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to delete article" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
93
src/app/api/articles/route.ts
Normal file
93
src/app/api/articles/route.ts
Normal file
@@ -0,0 +1,93 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { db, schema } from "@/lib/db";
|
||||
import { extractArticle } from "@/lib/utils/extract";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import { desc, eq } from "drizzle-orm";
|
||||
|
||||
// GET /api/articles - List all articles
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const searchParams = request.nextUrl.searchParams;
|
||||
const filter = searchParams.get("filter"); // all, favorites, archived
|
||||
|
||||
let query = db.select().from(schema.articles);
|
||||
|
||||
if (filter === "favorites") {
|
||||
query = query.where(eq(schema.articles.isFavorite, true)) as typeof query;
|
||||
} else if (filter === "archived") {
|
||||
query = query.where(eq(schema.articles.isArchived, true)) as typeof query;
|
||||
} else {
|
||||
// Default: show non-archived
|
||||
query = query.where(eq(schema.articles.isArchived, false)) as typeof query;
|
||||
}
|
||||
|
||||
const articles = await query.orderBy(desc(schema.articles.createdAt));
|
||||
|
||||
return NextResponse.json(articles);
|
||||
} catch (error) {
|
||||
console.error("Error fetching articles:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to fetch articles" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// POST /api/articles - Save a new article
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { url } = body;
|
||||
|
||||
if (!url) {
|
||||
return NextResponse.json({ error: "URL is required" }, { status: 400 });
|
||||
}
|
||||
|
||||
// Check if article already exists
|
||||
const existing = await db
|
||||
.select()
|
||||
.from(schema.articles)
|
||||
.where(eq(schema.articles.url, url))
|
||||
.limit(1);
|
||||
|
||||
if (existing.length > 0) {
|
||||
return NextResponse.json(
|
||||
{ error: "Article already saved", article: existing[0] },
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
// Extract article content
|
||||
const extracted = await extractArticle(url);
|
||||
|
||||
const id = uuidv4();
|
||||
const newArticle: schema.NewArticle = {
|
||||
id,
|
||||
url,
|
||||
title: extracted.title,
|
||||
author: extracted.author,
|
||||
siteName: extracted.siteName,
|
||||
excerpt: extracted.excerpt,
|
||||
content: extracted.content,
|
||||
textContent: extracted.textContent,
|
||||
leadImage: extracted.leadImage,
|
||||
wordCount: extracted.wordCount,
|
||||
};
|
||||
|
||||
await db.insert(schema.articles).values(newArticle);
|
||||
|
||||
const article = await db
|
||||
.select()
|
||||
.from(schema.articles)
|
||||
.where(eq(schema.articles.id, id))
|
||||
.limit(1);
|
||||
|
||||
return NextResponse.json(article[0], { status: 201 });
|
||||
} catch (error) {
|
||||
console.error("Error saving article:", error);
|
||||
return NextResponse.json(
|
||||
{ error: error instanceof Error ? error.message : "Failed to save article" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
140
src/app/api/save/route.ts
Normal file
140
src/app/api/save/route.ts
Normal file
@@ -0,0 +1,140 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { db, schema } from "@/lib/db";
|
||||
import { extractArticle } from "@/lib/utils/extract";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
||||
// GET /api/save?url=... - Save article and return HTML response for bookmarklet
|
||||
export async function GET(request: NextRequest) {
|
||||
const searchParams = request.nextUrl.searchParams;
|
||||
const url = searchParams.get("url");
|
||||
|
||||
const htmlResponse = (status: "success" | "error" | "exists", message: string) => {
|
||||
const bgColor = status === "success" ? "#22c55e" : status === "exists" ? "#eab308" : "#ef4444";
|
||||
const html = `
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>ReadLater</title>
|
||||
<style>
|
||||
* { margin: 0; padding: 0; box-sizing: border-box; }
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||
background: #000;
|
||||
color: #fff;
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 20px;
|
||||
}
|
||||
.container {
|
||||
text-align: center;
|
||||
max-width: 300px;
|
||||
}
|
||||
.icon {
|
||||
width: 60px;
|
||||
height: 60px;
|
||||
border-radius: 50%;
|
||||
background: ${bgColor};
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
margin: 0 auto 20px;
|
||||
}
|
||||
.icon svg {
|
||||
width: 30px;
|
||||
height: 30px;
|
||||
fill: white;
|
||||
}
|
||||
h1 {
|
||||
font-size: 18px;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
p {
|
||||
color: #888;
|
||||
font-size: 14px;
|
||||
line-height: 1.5;
|
||||
}
|
||||
.close {
|
||||
margin-top: 20px;
|
||||
padding: 10px 20px;
|
||||
background: #333;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
color: #fff;
|
||||
cursor: pointer;
|
||||
font-size: 14px;
|
||||
}
|
||||
.close:hover {
|
||||
background: #444;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="icon">
|
||||
${status === "success" ? '<svg viewBox="0 0 24 24"><path d="M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z"/></svg>' : status === "exists" ? '<svg viewBox="0 0 24 24"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-2 15l-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z"/></svg>' : '<svg viewBox="0 0 24 24"><path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"/></svg>'}
|
||||
</div>
|
||||
<h1>${status === "success" ? "Saved!" : status === "exists" ? "Already Saved" : "Error"}</h1>
|
||||
<p>${message}</p>
|
||||
<button class="close" onclick="window.close()">Close</button>
|
||||
</div>
|
||||
<script>
|
||||
setTimeout(() => window.close(), 3000);
|
||||
</script>
|
||||
</body>
|
||||
</html>`;
|
||||
return new NextResponse(html, {
|
||||
headers: { "Content-Type": "text/html" },
|
||||
});
|
||||
};
|
||||
|
||||
if (!url) {
|
||||
return htmlResponse("error", "No URL provided");
|
||||
}
|
||||
|
||||
try {
|
||||
const decodedUrl = decodeURIComponent(url);
|
||||
|
||||
// Check if article already exists
|
||||
const existing = await db
|
||||
.select()
|
||||
.from(schema.articles)
|
||||
.where(eq(schema.articles.url, decodedUrl))
|
||||
.limit(1);
|
||||
|
||||
if (existing.length > 0) {
|
||||
return htmlResponse("exists", `"${existing[0].title}" is already in your reading list`);
|
||||
}
|
||||
|
||||
// Extract article content
|
||||
const extracted = await extractArticle(decodedUrl);
|
||||
|
||||
const id = uuidv4();
|
||||
const newArticle: schema.NewArticle = {
|
||||
id,
|
||||
url: decodedUrl,
|
||||
title: extracted.title,
|
||||
author: extracted.author,
|
||||
siteName: extracted.siteName,
|
||||
excerpt: extracted.excerpt,
|
||||
content: extracted.content,
|
||||
textContent: extracted.textContent,
|
||||
leadImage: extracted.leadImage,
|
||||
wordCount: extracted.wordCount,
|
||||
};
|
||||
|
||||
await db.insert(schema.articles).values(newArticle);
|
||||
|
||||
return htmlResponse("success", `"${extracted.title}" has been added to your reading list`);
|
||||
} catch (error) {
|
||||
console.error("Error saving article:", error);
|
||||
return htmlResponse(
|
||||
"error",
|
||||
error instanceof Error ? error.message : "Failed to save article"
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user