Alaskan donut
Alaskan donut
Explore posts from servers
TTCTheo's Typesafe Cult
Created by Alaskan donut on 9/14/2024 in #questions
uploadthing replacing name and adding customId in middleware fails to run
This middleware does not run, despite this being close to what is provided in the documentation for changing file names and adding customIds (https://docs.uploadthing.com/file-routes#middleware). Logging the error provided by useUploadThing()'s onUploadError gives UploadThingError: Failed to run middleware... Caused by: Object { … }
.middleware(async ({ req, files }) => {
// This code runs on your server before upload
const { user } = await validateRequest();

// If you throw, the user will not be able to upload
if (!user) throw new UploadThingError("Unauthorized");

// Override file name and add customId
const fileOverrides = files.map((file) => {
const newName = uniqueNamesGenerator({
dictionaries: [adjectives, animals, colors],
length: 4,
});
const uuid = crypto.randomUUID();
return { ...file, name: newName, customId: uuid };
});

// Whatever is returned here is accessible in onUploadComplete as `metadata`
// Return userId to be used in onUploadComplete
return { userId: user.id, [UTFiles]: fileOverrides };
// return { userId: user.id };
})
.middleware(async ({ req, files }) => {
// This code runs on your server before upload
const { user } = await validateRequest();

// If you throw, the user will not be able to upload
if (!user) throw new UploadThingError("Unauthorized");

// Override file name and add customId
const fileOverrides = files.map((file) => {
const newName = uniqueNamesGenerator({
dictionaries: [adjectives, animals, colors],
length: 4,
});
const uuid = crypto.randomUUID();
return { ...file, name: newName, customId: uuid };
});

// Whatever is returned here is accessible in onUploadComplete as `metadata`
// Return userId to be used in onUploadComplete
return { userId: user.id, [UTFiles]: fileOverrides };
// return { userId: user.id };
})
1 replies