UNPKG

@reliverse/rse

Version:

@reliverse/rse is your all-in-one companion for bootstrapping and improving any kind of projects (especially web apps built with frameworks like Next.js) — whether you're kicking off something new or upgrading an existing app. It is also a little AI-power

32 lines (31 loc) 916 B
import { UTApi } from "uploadthing/server"; export async function uploadToUploadthing(files) { const apiKey = process.env.UPLOADTHING_API_KEY; if (!apiKey) { throw new Error("Missing UPLOADTHING_API_KEY"); } const utapi = new UTApi({ token: apiKey }); const results = []; for (const file of files) { const blob = new Blob([file.data], { type: file.type }); const fileEsque = Object.assign(blob, { name: file.name, lastModified: Date.now() }); const res = await utapi.uploadFiles(fileEsque); const uploadedFile = res; if (!uploadedFile.data) { throw new Error("Failed to upload file"); } results.push({ // @ts-expect-error TODO fix ts url: res.data.ufsUrl, // @ts-expect-error TODO fix ts key: res.data.key, // @ts-expect-error TODO fix ts size: res.data.size, name: file.name }); } return results; }