Content Sharing System
Synopsis
Implement functionality for users to post images, add comments, and share product links with proper moderation, storage, and display capabilities.
A content sharing system allows users to contribute to your community and share their work.
Database Tables for Content
Table Name | Purpose | Key Columns |
---|---|---|
posts | Store user posts/images | id, user_id, image_url, caption, created_at |
comments | Store comments on posts | id, post_id, user_id, content, created_at |
products | Store product links | id, post_id, product_url, title, description |
File Upload for Images
Implement secure image upload with compression and format conversion:
// Example: Client-side image compression before upload
function compressImage(file, maxWidth, maxHeight, quality) {
return new Promise((resolve) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = (event) => {
const img = new Image();
img.src = event.target.result;
img.onload = () => {
const canvas = document.createElement('canvas');
let width = img.width;
let height = img.height;
// Calculate new dimensions
if (width > height) {
if (width > maxWidth) {
height *= maxWidth / width;
width = maxWidth;
}
} else {
if (height > maxHeight) {
width *= maxHeight / height;
height = maxHeight;
}
}
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, width, height);
// Convert to compressed JPEG
canvas.toBlob(resolve, 'image/jpeg', quality);
};
};
});
}
Product Link Handling
Implement safe link sharing with preview generation:
// Example: Server-side link preview generation
async function generateLinkPreview(url) {
try {
const response = await fetch(url);
const html = await response.text();
// Extract metadata from HTML
const titleMatch = html.match(/(.*?)<\/title>/i);
const descriptionMatch = html.match(/
Moderation Tip: Implement content moderation to prevent spam and inappropriate content. Consider using automated filters and reporting systems.