#can i use 2 Dynamic Segment in a api route?

1 messages · Page 1 of 1 (latest)

vale finch
#

I have this api path app/api/downloads/[userId]/[userId]/route.tsx.
The problem it dosen't work

dusty ospreyBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

vale finch
#

Full code

#

import { promises as fs } from 'fs';
import path from 'path';
import { NextResponse } from 'next/server';
import { getClientId } from '@/app/upload/server';
import { FileMetadata } from '@/app/uploads/server';

interface RouteParams {
    params: Promise<{
        userId: string;
        fileId: string;
    }>;
}

export async function GET(req: Request, { params }: RouteParams) {
    const { fileId, userId } = await params;

    if (!fileId || userId) {
        return new NextResponse("ID-ul fișierului sau ID-ul utilizatorului lipsește.", { status: 400 });
    }

    try {
        const clientId = await getClientId();

        if (!clientId) {
            return new NextResponse("Autentificare eșuată. ID client lipsă.", { status: 401 });
        }

        const userStoragePath = path.join(process.env.FILESSTORAGEPATH!, clientId);
        const fileDestDir = path.join(userStoragePath, fileId);

        const metadataFilePath = path.join(fileDestDir, 'metadata.json');

        const metadataContent = await fs.readFile(metadataFilePath, 'utf-8');
        const metadata = JSON.parse(metadataContent) as FileMetadata;

        if (userId !== clientId && !metadata.shared && !(metadata.shares || []).some((s: any) => s.recipientId === clientId)) {
            console.warn("Acces Interzis")
            return new NextResponse("Acces interzis.", { status: 403 });
        }

        const metadataPath = path.join(metadata.storagePath, 'metadata.json');
        const fileContent = await fs.readFile(metadataPath);

        console.log("Path ul gasit: ", metadata.storagePath)
        return new NextResponse(fileContent, {
            headers: {
                'Content-Type': metadata.mimeType || 'application/octet-stream',
                'Content-Disposition': `attachment; filename="${metadata.originalName}"`,
                'Content-Length': fileContent.length.toString()
            },
        });
#
    } catch (error) {
        console.log(error)
        return new NextResponse("Fișierul nu a fost găsit sau accesul este interzis.", { status: 405 });
    }
}
acoustic storm
#

yes this is possible

vale finch
#

but why does it return 404

#

every time

#

if i remove the [userId] segment it works. but otherwise with both of them i get 404

#

GET /api/download/a2af6204-21d6-468d-83a1-8f19efefacf9/f8bb5828-9690-4c39-95b3-d952e43e68f9 404 in 148ms (compile: 85ms, render: 63ms)

#

aparently i needed to restart next js