Files
anwar-pi-website/web/src/app/api/files/[id]/route.ts
T
AHMED 0161924088 Free early-access launch: LAUNCH_MODE policy, CRO, pipeline hardening, AI-SEO, ops
- launch.ts policy: LAUNCH_MODE=free_early_access default; effective access =
  published && (free || early); intended tier (access) never rewritten
- Billing off server-side: /api/subscribe 503 honest copy; subscribe CTAs
  render disabled 'قريباً' from first paint; /subscription early-access panel
  + /api/notify (LaunchNotifications) with honest 503 when mail transport off
- Signup trust: verification email at signup (Users afterChange), no
  confirm-password, 429 message fidelity, typo suggestion, live password hint
- Agent pipeline: pickFields strips access/readingMinutes, sourceId dedup,
  secret-scan 422 (pattern names only), string-content rejected (was silently
  replaced), publish stays explicit opt-in
- lexical.ts: root unwrap fix — lexicalToText/countLexicalWords returned 0
  for real serialized trees (secret scan + readingMinutes were inert)
- llms-full.txt: fetchIndexDocs(true) — bodies were never selected
- Content honesty: contentType/authorName/sourceId fields + migration,
  readingMinutes computed at write (160 wpm) + backfill script, byline,
  'حُدّث' freshness
- SEO/AI-SEO: metadata rewrite (published-only, og:image 1200x630), Article +
  BreadcrumbList + FAQPage + Organization JSON-LD, isAccessibleForFree honest,
  table renderer, robots AI-crawler policy (10 named bots), llms.txt +
  llms-full.txt, /topics/* hubs + sitemap, noindex auth pages, www 301
- Ops: backup-db.sh (restore rehearsed on VPS), docs copies (launch
  readiness + marketing audit + owner-inputs for L-03), GLOSSARY.md entries

Verified: tsc --noEmit 0, npm run build 0, local smoke (ghost VIP read,
subscribe/notify/agent APIs, signup token, 429 on 6th, llms/topics/sitemap,
og:image/JSON-LD)
2026-10-10 18:03:45 +03:00

54 lines
2.3 KiB
TypeScript

import { NextResponse } from 'next/server'
import { readFile } from 'node:fs/promises'
import path from 'node:path'
import { getPayload } from 'payload'
import configPromise from '@payload-config'
import { FILES_DIR } from '@/collections/Files'
import { getViewer } from '@/lib/viewer'
import { freeEarlyAccess } from '@/lib/launch'
export const dynamic = 'force-dynamic'
type FileDoc = { id: number; access: 'free' | 'vip'; filename: string; mimeType: string }
/* GET /api/files/[id] — per-post work files, gated per file.
`free` streams to anyone; `vip` requires a signed-in vip/admin (same style
as the comments route). The Files collection read rule is defense in depth;
this route is the enforcement point for the bytes. */
export async function GET(_req: Request, ctx: { params: Promise<{ id: string }> }) {
const { id } = await ctx.params
const payload = await getPayload({ config: configPromise })
let doc: FileDoc | null = null
try {
doc = (await payload.findByID({ collection: 'files', id, depth: 0, overrideAccess: true })) as unknown as FileDoc
} catch {
doc = null // unknown id / DB error → not found
}
if (!doc?.filename) return NextResponse.json({ error: 'الملف غير موجود.' }, { status: 404 })
// L-02: all files open during free early access (owner curates file
// access in the admin; ALL vip files download while billing is off).
// Viewer checks apply only when billing is enabled.
if (doc.access === 'vip' && !freeEarlyAccess()) {
const viewer = await getViewer()
if (!viewer) return NextResponse.json({ error: 'سجّل الدخول لتنزيل الملفات.' }, { status: 401 })
if (viewer.role !== 'vip' && viewer.role !== 'admin') {
return NextResponse.json({ error: 'التنزيل متاح لأعضاء العضوية.' }, { status: 403 })
}
}
try {
const buf = await readFile(path.join(FILES_DIR, doc.filename))
return new NextResponse(new Uint8Array(buf), {
headers: {
'Content-Type': doc.mimeType,
'Content-Disposition': `attachment; filename*=UTF-8''${encodeURIComponent(doc.filename)}`,
},
})
} catch {
// Row exists but the file is missing on disk — do not leak a fake download.
return NextResponse.json({ error: 'الملف غير موجود.' }, { status: 404 })
}
}