Skip to content

Commit

Permalink
aws s3 bucket support added
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik-chausali committed Oct 24, 2024
1 parent 5d1c40f commit 3fa25e9
Show file tree
Hide file tree
Showing 1,808 changed files with 26,828 additions and 373 deletions.
File renamed without changes.
File renamed without changes.
97 changes: 97 additions & 0 deletions app/api/user/roomWanted/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { NextRequest, NextResponse } from "next/server";
import {S3Client, PutObjectCommand} from '@aws-sdk/client-s3'
import fs from 'fs'
import path from "path";
import formidable from 'formidable'
import { promisify } from "util";
import { NextApiRequest, NextApiResponse } from "next";
import {IncomingForm, Fields, Files} from 'formidable';
//Since you're now sending the file itself (not the file path),
//you'll need to use a middleware like formidable to parse the file from the request.
// Disable Next.js's default body parser for this API route
// export const config = {
// api: {
// bodyParser: false,
// },
// };

// Helper function to parse the form data using formidable
// const parseForm = async (req: NextApiRequest): Promise<{ fields: Fields; files: Files }> => {
// const form = new IncomingForm(); // Correct way to instantiate the form

// return new Promise((resolve, reject) => {
// form.parse(req, (err, fields, files) => {
// if (err) reject(err);
// resolve({ fields, files });
// });
// });
// };

export async function POST(req:Request){

console.log("inside post");


// const body = await req.json();
// const {filePath, name} = body;
try{
// const {fields, files} = await parseForm(req);
// const file = files.file ? (Array.isArray(files.file) ? files.file[0] : files.file) : undefined; // Assuming "file" is the form field name for file upload
// if (!file) {
// return res.status(400).json({ message: 'No file uploaded' });
// }
// const filePath = file.filepath; // Get the temporary path of the uploaded file
// const fileName = file.originalFilename || 'unknown_file'; // Get the original filename

const formData = await req.formData();
const file = formData.get('file');
const fileName = formData.get('name');

const accessKeyId = process.env.AWS_ACCESS_KEY
const secretAccessKey= process.env.AWS_SECRET_KEY
const region = process.env.AWS_REGION
const bucketName = process.env.S3_BUCKET_NAME
if (!accessKeyId || !secretAccessKey || !region || !bucketName) {
return new Response(JSON.stringify({ message: 'AWS credentials or bucket configuration missing' }), { status: 500 });
}

const s3 = new S3Client({
region,
credentials:{
accessKeyId,
secretAccessKey
}
});


if (!file || !(file instanceof Blob)) {
return new Response(JSON.stringify({ message: 'No file uploaded or invalid file' }), { status: 400 });
}

if (typeof fileName !== 'string') {
return new Response(JSON.stringify({ message: 'Invalid file name' }), { status: 400 });
}

// Convert the file into a buffer
const arrayBuffer = await file.arrayBuffer();
const fileBuffer = Buffer.from(arrayBuffer);

const uploadParams = {
Bucket: process.env.S3_BUCKET_NAME,
Key: fileName || 'unknown_file',
Body:fileBuffer,
ContentType: file.type || 'application/octet-stream',
}

const result = await s3.send(new PutObjectCommand(uploadParams));

const imageUrl = `https://${process.env.S3_BUCKET_NAME}.s3.${process.env.AWS_REGION}.amazonaws.com/${fileName}`

return NextResponse.json({ message: 'File uploaded successfully', imageUrl })
}catch (error) {
console.error('Error during file upload:', error);
return NextResponse.json({ message: 'File upload failed', error })
}

}
4 changes: 2 additions & 2 deletions app/components/AppBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ export default function AppBar(){
// return () => window.removeEventListener('scroll', handleScroll)
// },[])

console.log("sessino", session);



return <div className="flex justify-center items-center ">
return <div className="flex justify-center items-center m-20 ">
<div className={`w-fit p-2 shadow-lg fixed transition-colors duration-500 [background:linear-gradient(45deg,#172033,theme(colors.slate.800)_50%,#172033)_padding-box,conic-gradient(from_var(--border-angle),theme(colors.slate.600/.48)_80%,_theme(colors.indigo.500)_86%,_theme(colors.indigo.300)_90%,_theme(colors.indigo.500)_94%,_theme(colors.slate.600/.48))_border-box] top-0 left-0 right-0 z-50 mx-auto m-3 flex flex-wrap gap-2 justify-center items-center rounded-2xl border border-transparent animate-border`}>
<div className="flex items-center">
<Image src={roomate} alt="room" className="h-10 w-10 rounded-lg"/>
Expand Down
20 changes: 10 additions & 10 deletions app/components/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import { useEffect, useState , useRef} from "react";
export default function Chat(){
const chat = useIsChatOpen((state)=> state.isChatOpen)
const setChat = useIsChatOpen((state)=> state.setIsChatOpen)
const receiversChat = useUser((state)=> state.receiversChat);
const sendersChat = useUser((state)=> state.sendersChat);
// const receiversChat = useUser((state)=> state.receiversChat);
// const sendersChat = useUser((state)=> state.sendersChat);
const name = useUser((state)=> state.name);
const image = useUser((state)=>state.image);
const setReceiversChat = useUser((state)=> state.setReceiversChat);
const setSendersChat = useUser((state)=> state.setSendersChat);
// const setReceiversChat = useUser((state)=> state.setReceiversChat);
// const setSendersChat = useUser((state)=> state.setSendersChat);
const [socket, setSocket]= useState<WebSocket | null>(null);
const [latestSenderMessage, setLatestSenderMessages] = useState("")
const [latestReceiverMessage, setLatestReceiverMessages] = useState("");
// const [latestSenderMessage, setLatestSenderMessages] = useState("")
// const [latestReceiverMessage, setLatestReceiverMessages] = useState("");
const inputRef = useRef<HTMLInputElement>(null);
const[mergedChats, setMergedChats] = useState<{message:string , sender:string}[]>([])
useEffect(()=>{
Expand All @@ -24,8 +24,8 @@ export default function Chat(){
}

socket.onmessage = (message)=>{
setReceiversChat([...receiversChat, message.data])
setLatestReceiverMessages(message.data);
// setReceiversChat([...receiversChat, message.data])
// setLatestReceiverMessages(message.data);
setMergedChats((prev)=>[...prev, {message:message.data, sender:'receiver'}])
// console.log("message received ", message);
}
Expand Down Expand Up @@ -106,8 +106,8 @@ export default function Chat(){
<button id="send-button" onClick={()=>{
if(inputRef.current && inputRef.current.value.length>0 && socket){
socket.send(inputRef.current.value);
setSendersChat([...sendersChat, inputRef.current.value])
setLatestSenderMessages(inputRef.current.value);
// setSendersChat([...sendersChat, inputRef.current.value])
// setLatestSenderMessages(inputRef.current.value);
setMergedChats([...mergedChats, {message:inputRef.current.value, sender:"sender"}])
inputRef.current.value = "";
}
Expand Down
50 changes: 34 additions & 16 deletions app/components/Recomended.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,38 @@ import Room from "./Room";


export default function Recomended(){
return <div className="flex flex-col items-center justify-center m-4 px-4 bg-white mx-auto my-auto w-full max-w-fit">
<div className="flex items-center justify-center">
<RecomendedIcon/>
<span className="text-3xl mb-4 text-white">Recomended</span>
</div>
<div className="flex items-center justify-center">
<Tabs defaultValue="account" className="w-[400px]">
<TabsList>
<TabsTrigger value="room">Room</TabsTrigger>
<TabsTrigger value="roomate">Roomate</TabsTrigger>
</TabsList>
<TabsContent value="room"><Room/></TabsContent>
<TabsContent value="roomate"><Roomate/></TabsContent>
</Tabs>
</div>
</div>
// return <div className="flex flex-col items-center justify-center m-4 px-4 bg-white mx-auto my-auto w-full max-w-fit">
// <div className="flex items-center justify-center">
// <RecomendedIcon/>
// <span className="text-3xl mb-4 text-white">Recomended</span>
// </div>
// <div className="flex items-center justify-center">
// <Tabs defaultValue="account" className="w-[400px]">
// <TabsList>
// <TabsTrigger value="room">Room</TabsTrigger>
// <TabsTrigger value="roomate">Roomate</TabsTrigger>
// </TabsList>
// <TabsContent value="room"><Room/></TabsContent>
// <TabsContent value="roomate"><Roomate/></TabsContent>
// </Tabs>
// </div>
// </div>

return <div className=" mx-auto w-full max-w-7xl px-3 xl:px-0 flex flex-col items-center justify-center mt-10">
<h2 className="mx-auto max-w-5xl text-white text-center tracking-tight text-balance font-medium text-3xl md:text-5xl md:leading-tight">Explore</h2>
<h2 className="mx-auto my-4 max-w-4xl text-sm md:text-base text-balance text-neutral-500 text-center font-normal ">Explore according to your needs</h2>

<Tabs defaultValue="account" className="w-[400px] mt-6 flex flex-col items-center justify-center">
<TabsList >
<TabsTrigger value="room">Room</TabsTrigger>
<TabsTrigger value="roomate">Roomate</TabsTrigger>
</TabsList>
<TabsContent value="room"><Room/></TabsContent>
<TabsContent value="roomate"><Roomate/></TabsContent>
</Tabs>

</div>



}
14 changes: 7 additions & 7 deletions app/components/Room.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,32 @@ export default function Room(){
return <div className="flex justify-center items-center">
<div className="grid grid-cols-3 ">

<div className=" w-fit shadow-md rounded-sm bg-cover p-4 gap-x-96">
<div className=" w-fit shadow-md rounded-xl bg-cover p-4 gap-x-96 border border-white border-opacity-55 ">

<div className="relative">
<img className="w-full h-48 object-cover" src="https://images.unsplash.com/photo-1557862921-37829c790f19?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=M3w0NzEyNjZ8MHwxfHNlYXJjaHw4fHx1c2VyfGVufDB8MHx8fDE2OTQwOTU5Nzl8MA&ixlib=rb-4.0.3&q=80&w=1080" alt="Profile Image"/>
</div>

<span className="text-3xl font-bold pt-8 lg:pt-0 bg-cover">Your Name</span>
<span className="text-3xl font-bold pt-8 lg:pt-0 bg-cover text-white">Your Name</span>
<div className="mx-auto lg:mx-0 w-4/5 pt-3 border-b-2 border-green-500 opacity-25"></div>

<p className="pt-4 text-base font-bold flex items-center justify-center lg:justify-start">
<p className="pt-4 text-base font-bold flex items-center justify-center lg:justify-start text-white">
<svg className="h-4 fill-current text-green-700 pr-4" xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20">
<path
d="M9 12H1v6a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-6h-8v2H9v-2zm0-1H0V5c0-1.1.9-2 2-2h4V2a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v1h4a2 2 0 0 1 2 2v6h-9V9H9v2zm3-8V2H8v1h4z" />
</svg> What you do
</p>

<p className="pt-2 text-gray-600 text-xs lg:text-sm flex items-center justify-center lg:justify-start whitespace-nowrap">
<p className="pt-2 text-neutral-500 text-xs lg:text-sm flex items-center justify-center lg:justify-start whitespace-nowrap">
<svg className="h-4 fill-current text-green-700 pr-4" xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20">
<path
d="M10 20a10 10 0 1 1 0-20 10 10 0 0 1 0 20zm7.75-8a8.01 8.01 0 0 0 0-4h-3.82a28.81 28.81 0 0 1 0 4h3.82zm-.82 2h-3.22a14.44 14.44 0 0 1-.95 3.51A8.03 8.03 0 0 0 16.93 14zm-8.85-2h3.84a24.61 24.61 0 0 0 0-4H8.08a24.61 24.61 0 0 0 0 4zm.25 2c.41 2.4 1.13 4 1.67 4s1.26-1.6 1.67-4H8.33zm-6.08-2h3.82a28.81 28.81 0 0 1 0-4H2.25a8.01 8.01 0 0 0 0 4zm.82 2a8.03 8.03 0 0 0 4.17 3.51c-.42-.96-.74-2.16-.95-3.51H3.07zm13.86-8a8.03 8.03 0 0 0-4.17-3.51c.42.96.74 2.16.95 3.51h3.22zm-8.6 0h3.34c-.41-2.4-1.13-4-1.67-4S8.74 3.6 8.33 6zM3.07 6h3.22c.2-1.35.53-2.55.95-3.51A8.03 8.03 0 0 0 3.07 6z" />
</svg> Looking Around - 25.0000° N, 71.0000° W
</p>

<p className="pt-8 text-sm">Totally optional short description about yourself, what you do and so on.</p>
<p className="pt-8 text-sm text-white">Totally optional short description about yourself, what you do and so on.</p>

{/* <div className="pt-12 pb-8">
<button className="bg-green-700 hover:bg-green-900 text-white font-bold py-2 px-4 rounded-full">
Expand All @@ -77,15 +77,15 @@ export default function Room(){
}else{
setChat(true);
}
}} className="group relative cursor-pointer w-fit h-fit overflow-hidden m-4 ring-1 ring-green-500 ring-opacity-25 transition-all duration-300 hover:-translate-y-1 hover:shadow-2xl sm:mx-auto sm:max-w-sm sm:rounded-lg sm:px-10">
}} className="group relative cursor-pointer w-fit h-fit overflow-hidden m-4 transition-all duration-300 hover:-translate-y-1 hover:shadow-2xl sm:mx-auto sm:max-w-sm sm:rounded-lg sm:px-10">
<span className="absolute z-0 h-12 w-12 rounded-full bg-sky-500 transition-all duration-300 group-hover:scale-[10]"></span>
<div className="relative z-10 mx-auto max-w-md flex items-center">
<span className="grid h-12 w-12 place-items-center rounded-full bg-sky-500 transition-all duration-300 group-hover:bg-sky-400">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" className="h-10 w-10 text-white transition-all">
<path stroke-linecap="round" stroke-linejoin="round" d="M8.625 9.75a.375.375 0 11-.75 0 .375.375 0 01.75 0zm0 0H8.25m4.125 0a.375.375 0 11-.75 0 .375.375 0 01.75 0zm0 0H12m4.125 0a.375.375 0 11-.75 0 .375.375 0 01.75 0zm0 0h-.375m-13.5 3.01c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.184-4.183a1.14 1.14 0 01.778-.332 48.294 48.294 0 005.83-.498c1.585-.233 2.708-1.626 2.708-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0012 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018z" />
</svg>
</span>
<span className="font-bold py-2 px-4">Chat</span>
<span className="font-bold py-2 px-4 text-white">Chat</span>
</div>
</div>

Expand Down
20 changes: 11 additions & 9 deletions app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,27 @@ export default function RootLayout({
{/* <div className="fixed inset-0 -z-10 h-full w-full bg-slate-950"><div className="absolute inset-0 bg-[linear-gradient(to_right,#4f4f4f2e_1px,transparent_1px),linear-gradient(to_bottom,#4f4f4f2e_1px,transparent_1px)] bg-[size:14px_24px] [mask-image:radial-gradient(ellipse_60%_50%_at_50%_0%,#000_70%,transparent_100%)]">
</div></div> */}
<div className="relative h-screen w-screen overflow-auto bg-black">
{/* <div className="relative h-screen w-screen overflow-auto bg-black"> */}
<div className="fixed inset-0 -z-10 h-full w-full">
<div className="absolute inset-0 bg-black">
<div className="absolute bottom-0 left-0 right-0 top-0 bg-[linear-gradient(to_right,#4f4f4f2e_1px,transparent_1px),linear-gradient(to_bottom,#8080800a_1px,transparent_1px)] bg-[size:14px_24px]">

<div className="absolute left-0 right-0 top-[-10%] h-[1000px] w-[1000px] rounded-full bg-[radial-gradient(circle_400px_at_50%_300px,#fbfbfb36,#000)]">


<div className="absolute left-0 right-0 top-[-10%] h-full w-full rounded-full bg-[radial-gradient(circle_400px_at_50%_300px,#fbfbfb36,#000)]"></div>
{/* </div> */}
</div>
</div>
</div>


{/* <div className="relative min-h-screen"> */}


<div className="relative min-h-screen">
<Providers>
{children}
<Toaster/>
</Providers>
</div>
</div>
</div>


{/* </div> */}
</body>
</html>
Expand Down
50 changes: 47 additions & 3 deletions app/profiles/create/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Input } from "@/components/ui/input";
import axios from "axios";
import { useEffect, useRef, useState } from "react";
import { Textarea } from "@/components/ui/textarea"

import Image from 'next/image'
interface suggestionsType{
id: number,
wikiDataId: string,
Expand All @@ -32,6 +32,8 @@ import {
SelectValue,
} from "@/components/ui/select"
import { Button } from "@/components/ui/button";
import { useSession } from "next-auth/react";
import prisma from "@/lib/singletonDb";


export default function Home(){
Expand All @@ -40,6 +42,9 @@ export default function Home(){
const[suggestions ,setSuggestions] = useState<suggestionsType[]>([]);
const[badges, setBadges] = useState<string[]>([]);
const inputRef = useRef<HTMLInputElement>(null);
const session = useSession();

console.log("session", session.data?.user?.id);

const fetchSuggestions = async()=>{
try{
Expand All @@ -66,6 +71,41 @@ export default function Home(){
}
},[query])

function handlePost(){
const file = inputRef.current?.files?.[0]
console.log("file", file)
if(!file){
alert("Please select a file");
return ;
}

const formData = new FormData();
formData.append('file', file); // Append the file to FormData
formData.append('name', file.name); // You can pass the file name as well

try{
axios.post('/api/user/roomWanted', formData).then(response => {
console.log('Upload success', response);
}).catch(error => {
console.error('Upload failed', error);
});
}catch(error){
console.log("error while uploading image", error);
}


// try{
// await prisma.roomPost.create({
// data:{
// userId: session.data?.user.id || "",

// }
// })
// }catch(error){
// console.log("error" , error);
// }
}

return <div className="flex flex-col justify-center items-center">
<div className="flex flex-col p-4">
<h1 className="text-4xl font-heading text-blue-700">Looking for a Room?</h1>
Expand Down Expand Up @@ -146,15 +186,19 @@ export default function Home(){
</Select>
</div>

<div className="m-4">
<label className="font-semibold m-1 text-md">Your photo</label>
<input type="file" ref={inputRef}/>
</div>

</div>

<div className="w-1/2">
<label>Pitch yourself about "why you will be an ideal roomate"</label>
<Textarea className="" placeholder="I am very friendly and hygenic.."/>
</div>

<Button className="w-1/3 m-4">Post</Button>
<Button className="w-1/3 m-4" onClick={handlePost}>Post</Button>

</div>
}

Loading

0 comments on commit 3fa25e9

Please sign in to comment.