-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for drag drop file upload (#117)
Also create an award item with the same url
- Loading branch information
Showing
8 changed files
with
572 additions
and
54 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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,74 @@ | ||
"use client"; | ||
|
||
import { useCallback } from "react"; | ||
import { type DropzoneOptions, useDropzone } from "react-dropzone"; | ||
|
||
import { trpc } from "~/lib/trpc/client"; | ||
import { cn } from "~/lib/utils"; | ||
|
||
type ChildrenRenderProps = { | ||
isLoading: boolean; | ||
}; | ||
|
||
type Props = { | ||
className?: string; | ||
|
||
children: React.ReactNode | ((props: ChildrenRenderProps) => React.ReactNode); | ||
}; | ||
|
||
export function ImageDropzone({ children, className }: Props) { | ||
// bring over the hooks | ||
|
||
const utils = trpc.useContext(); | ||
|
||
const uploadDocumentMutation = trpc.awardRouter.uploadImageToS3.useMutation(); | ||
|
||
const onDropRaw: DropzoneOptions["onDrop"] = (acceptedFiles, _, e) => { | ||
const file = acceptedFiles[0]; | ||
|
||
if (!file) return; | ||
|
||
const reader = new FileReader(); | ||
|
||
reader.onload = async () => { | ||
const result = reader.result; | ||
|
||
if (typeof result !== "string") return; | ||
|
||
const base64String = result.split(",")[1]; | ||
|
||
if (!base64String) return; | ||
|
||
await uploadDocumentMutation.mutateAsync({ | ||
filename: file.name, | ||
fileDataBase64: base64String, | ||
fileMimeType: file.type, | ||
}); | ||
|
||
// invalidate everything for now | ||
await utils.invalidate(); | ||
}; | ||
|
||
reader.readAsDataURL(file); | ||
}; | ||
|
||
const onDrop = useCallback(onDropRaw, [uploadDocumentMutation, utils]); | ||
|
||
const { getRootProps, getInputProps, isDragActive } = useDropzone({ | ||
onDrop, | ||
noClick: true, | ||
noDragEventsBubbling: true, | ||
}); | ||
|
||
return ( | ||
<div className={cn({ "ring-primary ring-2": isDragActive }, className)}> | ||
<div {...getRootProps()}> | ||
<input {...getInputProps()} /> | ||
|
||
{typeof children === "function" | ||
? children({ isLoading: uploadDocumentMutation.isLoading }) | ||
: children} | ||
</div> | ||
</div> | ||
); | ||
} |
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,46 @@ | ||
import { type NextApiHandler } from "next"; | ||
|
||
import { env } from "~/env.mjs"; | ||
import { s3 } from "~/server/api/routers/awardRouter"; | ||
import { getServerAuthSession } from "~/server/auth"; | ||
|
||
const handler: NextApiHandler = async (req, res) => { | ||
// hit the database and return the file | ||
|
||
const { id } = req.query; | ||
|
||
const session = await getServerAuthSession({ req, res }); | ||
|
||
if (!session) { | ||
return res.status(401).json({ message: "Unauthorized" }); | ||
} | ||
|
||
// load image from S3 | ||
|
||
const data = await s3 | ||
.getObject({ | ||
Bucket: env.S3_BUCKET_NAME, | ||
Key: id as string, | ||
}) | ||
.promise(); | ||
|
||
if (!data) { | ||
return res.status(404).json({ message: "Not found" }); | ||
} | ||
|
||
// convert blob data to buffer | ||
|
||
// Determine the content type (e.g., 'image/jpeg', 'image/png') | ||
const contentType = data.ContentType ?? "image/png"; | ||
const doc = data.Body as any; | ||
|
||
res.setHeader("Content-Type", contentType); | ||
res.setHeader("Content-Length", doc.length); | ||
|
||
// include file name | ||
res.setHeader("Content-Disposition", `attachment; filename=${doc.filename}`); | ||
|
||
res.end(doc); | ||
}; | ||
|
||
export default handler; |
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