Skip to content

Commit

Permalink
get media from r2 api done
Browse files Browse the repository at this point in the history
  • Loading branch information
farooqpk committed Jun 23, 2024
1 parent 660c831 commit 4181d9e
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/controllers/chat/generateSignedUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const generateSignedUrl = async (req: Request, res: Response) => {
});
}

const uniqueKey = `messages/${uuidv4()}`;
const uniqueKey = uuidv4();

const command = new PutObjectCommand({
Bucket: R2_BUCKET_NAME!,
Expand Down
40 changes: 40 additions & 0 deletions src/controllers/chat/getMediaFromR2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { GetObjectCommand } from "@aws-sdk/client-s3";
import { Request, Response } from "express";
import { R2_BUCKET_NAME } from "../../config";
import { s3Client } from "../../utils/r2";

export const getMediaFromR2 = async (req: Request, res: Response) => {
try {
const { mediapath } = req.params;

if (!mediapath) {
return res.status(400).json({
success: false,
message: "mediapath is required",
});
}

const command = new GetObjectCommand({
Bucket: R2_BUCKET_NAME!,
Key: mediapath,
});

const response = await s3Client.send(command);

if (!response.Body) {
return res.status(404).json({
success: false,
message: "media not found",
});
}

res.setHeader("Content-Type", "application/octet-stream");

const uint8Array = await response.Body.transformToByteArray();

return res.status(200).send(Buffer.from(uint8Array));
} catch (error) {
console.error("Error fetching media from R2:", error);
return res.status(500).json(error);
}
};
2 changes: 1 addition & 1 deletion src/controllers/chat/messageList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const messageList = async (req: Request, res: Response) => {
await setDataInRedis({
key: `messages:${chatId}`,
data: messages,
expirationTimeInSeconds: 4 * 60 * 60,
expirationTimeInSeconds: 2 * 60 * 60,
});

res.status(200).json(messages);
Expand Down
2 changes: 1 addition & 1 deletion src/routes/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ authRouter.post(
updateUsername
);

authRouter.delete("/logout",logout)
authRouter.post("/logout",logout)
3 changes: 3 additions & 0 deletions src/routes/message.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import Express, { Router } from "express";
import { verifyToken } from "../middlewares/verifyToken";
import { messageList } from "../controllers/chat/messageList";
import { getMediaFromR2 } from "../controllers/chat/getMediaFromR2";

export const messageRouter: Router = Express.Router();

messageRouter.get("/messages/:chatId", verifyToken, messageList);

messageRouter.get("/get-media/:mediapath", verifyToken, getMediaFromR2);
1 change: 1 addition & 0 deletions src/socket/handlers/send-group-msg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export const sendGroupMsgHandler = async (
content: !IS_IMAGE_OR_AUDIO ? content : null,
createdAt: new Date(),
senderId: payload.userId,
mediaPath: IS_IMAGE_OR_AUDIO ? mediaPath : null,
},
include: {
sender: {
Expand Down
3 changes: 1 addition & 2 deletions src/socket/handlers/send-private-msg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ export const sendPrivateMsgHandler = async (
}));

if (isAlreadyChatExist) {
console.log("Chat already exist");


const msg = await prisma.message.create({
data: {
content: !IS_IMAGE_OR_AUDIO ? content : null,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/configureSocketIO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function configureSocketIO(io: Server) {
socket.rooms.forEach((room) => socket.leave(room));
await clearFromRedis({ key: `socket:${payload.userId}` });
socket.broadcast.emit(SocketEvents.IS_DISCONNECTED, payload.userId);
console.log("Socket.IO: Disconnected");
console.log(`Socket.IO: ${payload.username} disconnected`);
});
} catch (error) {
console.error("Socket.IO: Authentication failed", error);
Expand Down
15 changes: 0 additions & 15 deletions src/utils/getMediaFromR2.ts

This file was deleted.

0 comments on commit 4181d9e

Please sign in to comment.