Skip to content

Pathname isn't being set by onBeforeGenerateToken #863

@SamJbori

Description

@SamJbori

The title says it all!

Ive checked and my console logs are working just fine, all look legit (I think!)

PathName: tmp/StoreRequests/bacd36d979d76e760dabcc3f/images/bacd460c72902d264959fc8d.jpg
 POST /api/upload 200 in 269ms
export async function POST(request: Request): Promise<NextResponse> {
  const body = (await request.json()) as HandleUploadBody;
  try {
    const jsonResponse = await handleUpload({
      body,
      request,
      onBeforeGenerateToken: async (fileName, storeId) => {
        const user = await getServerUser();
        if (!user) {
          throw new Error("Unauthorized");
        }
        const id = generateUUID();
        const collection: TCollections = "StoreRequests";
        const pathname = `tmp/${collection}/${storeId}/images/${id}.jpg`;

        console.log("PathName:", pathname);

        return {
          pathname,
          allowedContentTypes: ["image/jpeg"],
          maximumSizeInBytes: 2 * 1024 * 1024, // 2MB
          tokenPayload: JSON.stringify({
            userId: user.id,
            storeId,
            filename: fileName, // Keep track of original name
            pathname,
            timestamp: Date.now(),
          }),
          cacheControlMaxAge: 365 * 24 * 60 * 60,
        } satisfies GenerateClientTokenOptions & { tokenPayload: string };
      },
      onUploadCompleted: async ({ blob, tokenPayload: _tokenPayload }) => {
        console.log("Blob upload completed:", blob.pathname);
      },
    });

    return NextResponse.json(jsonResponse);
  } catch (error) {
    console.error("Upload error:", error);
    return NextResponse.json(
      { error: (error as Error).message },
      { status: 400 },
    );
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions