Skip to content

Commit 12c565f

Browse files
committed
Store files in supabase storage
1 parent ba8be71 commit 12c565f

File tree

6 files changed

+146
-16
lines changed

6 files changed

+146
-16
lines changed

supa-vacation-nextjs/.prettierrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
"useTabs": false,
44
"trailingComma": "all",
55
"endOfLine": "lf",
6-
"semi": true
6+
"semi": true,
7+
"printWidth": 80
78
}

supa-vacation-nextjs/components/ListingForm.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { toast } from 'react-hot-toast';
66
import { Formik, Form } from 'formik';
77
import Input from '@/components/Input';
88
import ImageUpload from '@/components/ImageUpload';
9+
import axios from "axios"
910

1011
const ListingSchema = Yup.object().shape({
1112
title: Yup.string().trim().required(),
@@ -27,8 +28,22 @@ const ListingForm = ({
2728
const [disabled, setDisabled] = useState(false);
2829
const [imageUrl, setImageUrl] = useState(initialValues?.image ?? '');
2930

30-
const upload = async image => {
31-
// TODO: Upload image to remote storage
31+
const upload = async (image) => {
32+
if (!image) return;
33+
34+
let toastId;
35+
try {
36+
setDisabled(true);
37+
toastId = toast.loading('Uploading...');
38+
const { data } = await axios.post('/api/image-upload', { image });
39+
setImageUrl(data?.url);
40+
toast.success('Successfully uploaded', { id: toastId });
41+
} catch (e) {
42+
toast.error('Unable to upload', { id: toastId });
43+
setImageUrl('');
44+
} finally {
45+
setDisabled(false);
46+
}
3247
};
3348

3449
const handleOnSubmit = async (values = null) => {

supa-vacation-nextjs/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@
3131
"@prisma/client": "^3.15.1",
3232
"@tailwindcss/aspect-ratio": "^0.4.0",
3333
"@types/node": "^17.0.42",
34-
"autoprefixer": "^10.4.2",
34+
"autoprefixer": "10.4.5",
35+
"axios": "^0.27.2",
36+
"base64-arraybuffer": "^1.0.2",
3537
"eslint": "8.8.0",
3638
"eslint-config-next": "12.0.10",
39+
"nanoid": "^4.0.0",
3740
"postcss": "^8.4.14",
3841
"prisma": "^3.15.1",
3942
"tailwindcss": "^3.1.3",

supa-vacation-nextjs/pages/api/image-upload.js

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,51 @@
1-
const handler = async (req, res) => {
1+
import { createClient } from "@supabase/supabase-js"
2+
import { nanoid } from "nanoid";
3+
4+
const supabase = createClient(
5+
process.env.SUPABASE_URL,
6+
process.env.SUPABASE_KEY
7+
);
8+
9+
const handler = async (req, res) => {
210
// Upload image to Supabase
311
if (req.method === 'POST') {
4-
// TODO
12+
let { image } = req.body;
13+
14+
if (!image) {
15+
return res.status(500).json({ message: 'No image provided' });
16+
}
17+
18+
try {
19+
const contentType = image.match(/data:(.*);base64/)?.[1];
20+
const base64FileData = image.split('base64,')?.[1];
21+
22+
if (!contentType || !base64FileData) {
23+
return res.status(500).json({ message: 'Image data not valid' });
24+
}
25+
26+
const fileName = nanoid();
27+
const ext = contentType.split('/')[1];
28+
const path = `${fileName}.${ext}`;
29+
30+
const { data, error: uploadError } = await supabase.storage
31+
.from(process.env.SUPABASE_BUCKET)
32+
.upload(path, decode(base64FileData), {
33+
contentType,
34+
upsert: true,
35+
});
36+
37+
if (uploadError) {
38+
console.log(uploadError);
39+
throw new Error('Unable to upload image to storage');
40+
}
41+
42+
// Construct public URL
43+
const url = `${process.env.SUPABASE_URL}/storage/v1/object/public/${data.Key}`;
44+
45+
return res.status(200).json({ url });
46+
} catch (e) {
47+
res.status(500).json({ message: 'Something went wrong' });
48+
}
549
}
650
// HTTP method not supported!
751
else {

supa-vacation-nextjs/pnpm-lock.yaml

Lines changed: 77 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

supa-vacation-nextjs/utils/supabaseOps.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)