#[SOLVED] How to view File from appwrite storage

6 messages · Page 1 of 1 (latest)

solar wraith
#

I want to get the file from an api route, but how do i send the file from api route, as its an array buffer and i cant send it using NextResponse.json()

I haven't worked with files, this is my first time

#

My API Route Code

import { SESSION_COOKIE } from "@/const";
import { db, noteAttachmentBucket, noteCollection } from "@/models/name";
import { createSessionClient } from "@/models/server/config";
import { NextRequest, NextResponse } from "next/server";

export async function GET(req: NextRequest) {
  try {
    const note_id = req.nextUrl.searchParams.get("note_id")
    

    if (!note_id) {
      return NextResponse.json({
        success: false,
        error: "Note ID is required"
      }, { status: 400 })
    }

    const session = req.cookies.get(SESSION_COOKIE)
    if (!session || !session.value) {
      return NextResponse.json({
        success: false,
        error: "Unauthorized"
      }, { status: 401 })
    }

    const { databases, storage } = await createSessionClient(session)
    const note = await databases.getDocument(db, noteCollection, note_id)

    if (!note) {
      return NextResponse.json({
        success: false,
        error: "Note not found"
      }, { status: 404 })
    }

    const viewFile = await storage.getFileView(noteAttachmentBucket, note.file_id)

    return NextResponse.json({
      success: true,
      noteData: {
        title: note.title,
        description: note.description,
        subject: note.subject,
        tags: note.tags,
      }
    })

  }
  catch (err) {
    return NextResponse.json({
      success: false,
      error: "Internal server error"
    }, { status: 500 })
  }
}

https://pastebin.com/N7Nr0e3G

solar wraith
#

i sent the file to frontend using this

const viewFile = await storage.getFileView(noteAttachmentBucket, note.file_id)
const buffer = new Uint8Array(viewFile)
const decodedFile = new TextDecoder().decode(buffer)
return NextResponse.json({
  success: true,
  noteData: {
    title: note.title,
    description: note.description,
    subject: note.subject,
    tags: note.tags,
    file: decodedFile,
  }
})

How can i show the file in frontend now ?

solar wraith
#
const file_url = `${endpoint}/storage/buckets/${noteAttachmentBucket}/files/${file_id}/view?project=${projectId}`

im saving the file_url in my note doucment

#

and showing the url in the frontend for file