Advertisement
NHeroOP

get-file api route

Sep 29th, 2024
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { SESSION_COOKIE } from "@/const";
  2. import { db, noteAttachmentBucket, noteCollection } from "@/models/name";
  3. import { createSessionClient } from "@/models/server/config";
  4. import { NextRequest, NextResponse } from "next/server";
  5.  
  6. export async function GET(req: NextRequest) {
  7.   try {
  8.     const note_id = req.nextUrl.searchParams.get("note_id")
  9.    
  10.  
  11.     if (!note_id) {
  12.       return NextResponse.json({
  13.         success: false,
  14.         error: "Note ID is required"
  15.       }, { status: 400 })
  16.     }
  17.  
  18.     const session = req.cookies.get(SESSION_COOKIE)
  19.     if (!session || !session.value) {
  20.       return NextResponse.json({
  21.         success: false,
  22.         error: "Unauthorized"
  23.       }, { status: 401 })
  24.     }
  25.  
  26.     const { databases, storage } = await createSessionClient(session)
  27.     const note = await databases.getDocument(db, noteCollection, note_id)
  28.  
  29.     if (!note) {
  30.       return NextResponse.json({
  31.         success: false,
  32.         error: "Note not found"
  33.       }, { status: 404 })
  34.     }
  35.  
  36.     const viewFile = await storage.getFileView(noteAttachmentBucket, note.file_id)
  37.  
  38.     return NextResponse.json({
  39.       success: true,
  40.       noteData: {
  41.         title: note.title,
  42.         description: note.description,
  43.         subject: note.subject,
  44.         tags: note.tags,
  45.       }
  46.     })
  47.  
  48.   }
  49.   catch (err) {
  50.     return NextResponse.json({
  51.       success: false,
  52.       error: "Internal server error"
  53.     }, { status: 500 })
  54.   }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement