Theo's Typesafe CultTTC
Theo's Typesafe Cult14mo ago
2 replies
Taylor

How to upload a PDF file to uploadthing from a route.ts file

export async function uploadFileToProfile(
  pdfBuffer: Uint8Array<ArrayBufferLike>,
  uniqueMetadataId: string,
) {
  const { userId } = await auth();

  if (!userId) {
    return {
      success: false,
      error: "Unauthorized",
    };
  }

  const blob = new Blob([pdfBuffer]);
  const file = new File([blob], `${uniqueMetadataId}.pdf`);

  const result = await utapi.uploadFiles(file);

This function is being called from a route.ts file. Im getting a vague error relating to some regex match in uploadthings source:

207 | // Pull out numbers from strings like 6.0.0, ^6.4, ~6.4.0
208 | const semverRegex = /(\d+).?(\d+)?.?(\d+)?/;
209 | const requiredMatch = required.match(semverRegex);
| ^
210 | if (!requiredMatch?.[0]) {
211 | throw new Error(Invalid semver requirement: ${required});
212 | } {

From the documentation it seems like this is done very similarly with FormData files in an in-component server action, and I dont see what the difference would be here.
Was this page helpful?