storage icon indicating copy to clipboard operation
storage copied to clipboard

Pathname isn't being set by onBeforeGenerateToken

Open SamJbori opened this issue 6 months ago • 2 comments

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 },
    );
  }
}

SamJbori avatar Jun 17 '25 07:06 SamJbori

Hey @SamJbori, sorry you're having this issue. The pathname property cannot be set as a response of onBeforeGenerateToken, it has to be set at the upload(pathname, .., ..) time only. onBeforeGenerateToken allows you to check the pathname is the one you're excepting but that's it as of today.

I understand this is a limitation and we'll work towards solving this soon.

vvo avatar Jun 17 '25 08:06 vvo

Hey @SamJbori, sorry you're having this issue. The pathname property cannot be set as a response of onBeforeGenerateToken, it has to be set at the upload(pathname, .., ..) time only. onBeforeGenerateToken allows you to check the pathname is the one you're excepting but that's it as of today.

I understand this is a limitation and we'll work towards solving this soon.

It's what it's, doesn't make it less of Kickass package, looking forward for this to be implemented!

SamJbori avatar Jun 17 '25 09:06 SamJbori