-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from miljan-code/feat/uploadthing
🚧 feat: uploadthing
- Loading branch information
Showing
19 changed files
with
206 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import ora from "ora"; | ||
import chalk from "chalk"; | ||
|
||
import { InstallPackagesOpts } from "@/commands/init/helpers/install-packages.js"; | ||
import { depInstaller } from "../helpers/dep-installer.js"; | ||
import { logger } from "@/utils/logger.js"; | ||
import { fsUploadthing } from "@/commands/common/fs-helpers.js"; | ||
import { updateKickstartConfig } from "@/commands/common/update-kickstart-config.js"; | ||
|
||
export const uploadthingInstaller = async ({ | ||
packages, | ||
projectDir, | ||
}: InstallPackagesOpts) => { | ||
const loader = ora("Installing package dependencies").start(); | ||
await depInstaller({ | ||
projectDir, | ||
deps: ["uploadthing", "@uploadthing/react"], | ||
isDev: false, | ||
}); | ||
loader.stop(); | ||
logger.success(`Dependencies has been installed successfully.`); | ||
|
||
// Copy configuration files | ||
fsUploadthing({ projectDir }); | ||
logger.success("Package setup files are successfully scaffolded.\n"); | ||
|
||
// Update next-kickstarter config | ||
updateKickstartConfig(projectDir, "uploadthing"); | ||
|
||
// Next steps | ||
logger.info("Find out more about Uploadthing:"); | ||
logger.info(` ${chalk.white("https://docs.kickstart.miljan.xyz")}`); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import path from "node:path"; | ||
|
||
import { fsUploadthing } from "@/commands/common/fs-helpers.js"; | ||
import { addPackageDeps } from "@/commands/common/add-package-deps.js"; | ||
import { type Dependency } from "@/commands/common/dependencies.js"; | ||
import { type InstallPackagesOpts } from "../helpers/install-packages.js"; | ||
|
||
export const uploadthingInstaller = ({ | ||
projectDir, | ||
packages, | ||
}: InstallPackagesOpts) => { | ||
const pkgJsonPath = path.join(projectDir, "package.json"); | ||
const deps: Dependency[] = ["uploadthing", "@uploadthing/react"]; | ||
addPackageDeps({ deps, isDev: false, pkgJsonPath }); | ||
fsUploadthing({ projectDir }); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/cli/template/libs/uploadthing/app/api/uploadthing/core.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { createUploadthing, type FileRouter } from "uploadthing/next"; | ||
|
||
import { getUserSession } from "@/lib/auth"; | ||
|
||
const f = createUploadthing(); | ||
|
||
// FileRouter for your app, can contain multiple FileRoutes | ||
export const ourFileRouter = { | ||
// Define as many FileRoutes as you like, each with a unique routeSlug | ||
imageUploader: f({ image: { maxFileSize: "4MB" } }) | ||
// Set permissions and file types for this FileRoute | ||
.middleware(async () => { | ||
// This code runs on your server before upload | ||
const { session } = await getUserSession(); | ||
if (!session?.user.id) throw new Error("Unauthorized"); | ||
|
||
// Whatever is returned here is accessible in onUploadComplete as `metadata` | ||
return { userId: session.user.id }; | ||
}) | ||
.onUploadComplete(({ metadata, file }) => { | ||
// This code RUNS ON YOUR SERVER after upload | ||
console.log("Upload complete for userId:", metadata.userId); | ||
|
||
console.log("file url", file.url); | ||
|
||
// !!! Whatever is returned here is sent to the clientside `onClientUploadComplete` callback | ||
return { uploadedBy: metadata.userId }; | ||
}), | ||
} satisfies FileRouter; | ||
|
||
export type OurFileRouter = typeof ourFileRouter; |
7 changes: 7 additions & 0 deletions
7
packages/cli/template/libs/uploadthing/app/api/uploadthing/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { ourFileRouter } from "./core"; | ||
import { createRouteHandler } from "uploadthing/next"; | ||
|
||
// Export routes for Next App Router | ||
export const { GET, POST } = createRouteHandler({ | ||
router: ourFileRouter, | ||
}); |
27 changes: 27 additions & 0 deletions
27
packages/cli/template/libs/uploadthing/components/uploader.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"use client"; | ||
|
||
import { useUploadThing } from "@/lib/uploadthing"; | ||
|
||
export const Uploader = () => { | ||
const { startUpload, isUploading } = useUploadThing("imageUploader", { | ||
onClientUploadComplete: (res) => { | ||
if (!res) return; | ||
const imageUrl = res[0]?.url || ""; | ||
console.log(imageUrl); | ||
}, | ||
}); | ||
|
||
const handleImageUpload = async (e: React.ChangeEvent<HTMLInputElement>) => { | ||
const file = e.target.files?.[0]; | ||
if (!file) return; | ||
await startUpload([file]); | ||
}; | ||
|
||
return ( | ||
<input | ||
type="file" | ||
disabled={isUploading} | ||
onChange={(e) => void handleImageUpload(e)} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { generateReactHelpers } from "@uploadthing/react/hooks"; | ||
|
||
import type { OurFileRouter } from "@/app/api/uploadthing/core"; | ||
|
||
export const { useUploadThing, uploadFiles } = | ||
generateReactHelpers<OurFileRouter>(); |