Compressed img & fixed img in postpage

This commit is contained in:
Rykkel
2024-08-10 13:32:44 +08:00
parent 047361615d
commit 196c9d875f
3 changed files with 34 additions and 9 deletions

View File

@@ -261,8 +261,7 @@ export default function CommunityPage() {
<img <img
src={`${config.serverAddress}/post/post-image/${post.id}`} src={`${config.serverAddress}/post/post-image/${post.id}`}
className="w-[300px] h-auto rounded-lg object-cover" className="w-[300px] h-auto rounded-lg object-cover"
onError={() => handleImageError(post.id)} onError={() => handleImageError(post.id)} />
/>
</div> </div>
)} )}
</div> </div>

View File

@@ -54,10 +54,18 @@ const PostPage: React.FC = () => {
useEffect(() => { useEffect(() => {
if (id) { if (id) {
console.log("useEffect for fetching post called, id:", id);
instance.get(`${config.serverAddress}/post/${id}`) instance.get(`${config.serverAddress}/post/${id}`)
.then((res) => setPost(res.data)) .then((res) => {
setPost(res.data);
console.log("Post fetch successfully");
})
.catch((error) => console.error("Error fetching post:", error)); .catch((error) => console.error("Error fetching post:", error));
} }
}, [id]);
useEffect(() => {
console.log("PostPage component mounted");
}, []); }, []);
useEffect(() => { useEffect(() => {
@@ -189,7 +197,8 @@ const PostPage: React.FC = () => {
</div> </div>
{!imageErrorFlags[post.id] && post.postImage && post.postImage !== null && ( {!imageErrorFlags[post.id] && post.postImage && post.postImage !== null && (
<div> <div>
<img src={`${config.serverAddress}/post/post-image/${post.id}`} <img
src={`${config.serverAddress}/post/post-image/${post.id}`}
alt="PostImage" alt="PostImage"
className="w-[300px] h-auto rounded-lg object-cover" className="w-[300px] h-auto rounded-lg object-cover"
onError={() => handleImageError(post.id)} /> onError={() => handleImageError(post.id)} />

View File

@@ -61,6 +61,17 @@ router.post(
let postImage = files.postImage ? files.postImage[0].buffer : null; let postImage = files.postImage ? files.postImage[0].buffer : null;
// Resize and compress image if it exists
if (postImage) {
postImage = await sharp(postImage)
.resize(512, 512, {
fit: sharp.fit.inside,
withoutEnlargement: true,
})
.jpeg({ quality: 80 })
.toBuffer();
}
// Process valid data // Process valid data
let result = await Post.create({ ...data, postImage }); let result = await Post.create({ ...data, postImage });
res.json(result); res.json(result);
@@ -82,7 +93,6 @@ router.post(
router.get("/", async (req, res) => { router.get("/", async (req, res) => {
let condition = { let condition = {
where: {}, where: {},
attributes: { exclude: ["postImage"] },
order: [["createdAt", "DESC"]], order: [["createdAt", "DESC"]],
}; };
@@ -102,9 +112,8 @@ router.get("/", async (req, res) => {
router.get("/:id", async (req, res) => { router.get("/:id", async (req, res) => {
let id = req.params.id; let id = req.params.id;
let post = await Post.findByPk(id, { let post = await Post.findByPk(id);
attributes: { exclude: ["postImage"] },
});
if (!post) { if (!post) {
res.sendStatus(404); res.sendStatus(404);
return; return;
@@ -170,8 +179,16 @@ router.put(
}); });
} }
// Include the postImage if present // Include the postImage if present
if (postImage) { if (postImage) {
postImage = await sharp(postImage)
.resize(512, 512, {
fit: sharp.fit.inside,
withoutEnlargement: true,
})
.jpeg({ quality: 80 })
.toBuffer();
data.postImage = postImage; data.postImage = postImage;
} }