Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions api/images.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// This is a mock implementation - replace with your actual database/storage logic
const images = new Map(); // In production, use a real database

module.exports = async (request, response) => {
const { method } = request;

switch (method) {
case 'GET':
// Get user's images with pagination
try {
const userId = request.headers['user-id'] || 'default-user'; // Replace with your auth logic
const page = parseInt(request.query.page) || 1;
const pageSize = parseInt(request.query.pageSize) || 12;

const userImages = Array.from(images.values())
.filter(img => img.userId === userId)
.sort((a, b) => new Date(b.created_at) - new Date(a.created_at));

const startIndex = (page - 1) * pageSize;
const endIndex = startIndex + pageSize;
const paginatedImages = userImages.slice(startIndex, endIndex);

response.status(200).json({
total_count: userImages.length,
page_number: page,
page_size: pageSize,
data: paginatedImages.map(img => ({
id: img.id,
filename: img.filename,
blob_link: img.blob_link,
file_path: img.file_path,
created_at: img.created_at,
updated_at: img.updated_at,
})),
});
} catch (error) {
response.status(500).json({ error: 'Failed to fetch images' });
}
break;

case 'POST':
// Save uploaded image metadata
try {
const { id, filename, blob_link, file_path, userId } = request.body;
const imageData = {
id,
filename,
blob_link,
file_path,
userId,
created_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
};

images.set(id, imageData);
response.status(200).json({ success: true });
} catch (error) {
response.status(500).json({ error: 'Failed to save image metadata' });
}
break;

case 'DELETE':
// Delete image
try {
const { imageId } = request.query;
const userId = request.headers['user-id'] || 'default-user';

const image = images.get(imageId);
if (!image || image.userId !== userId) {
return response.status(404).json({ error: 'Image not found' });
}

images.delete(imageId);
response.status(200).json({ success: true });
} catch (error) {
response.status(500).json({ error: 'Failed to delete image' });
}
break;

default:
response.status(405).json({ error: 'Method not allowed' });
}
};
56 changes: 42 additions & 14 deletions api/upload.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,47 @@
import cloudinary from 'cloudinary';
import { v4 as uuidv4 } from 'uuid';

module.exports = async (request, response) => {
const timestamp = Math.round(new Date().getTime() / 1000);
const signature = cloudinary.v2.utils.api_sign_request(
{
timestamp: timestamp,
folder: `easy-email-demo`,
},
process.env.CLOUDINARY_API_SECRET,
);
if (request.method === 'POST') {
try {
const timestamp = Math.round(new Date().getTime() / 1000);
const signature = cloudinary.v2.utils.api_sign_request(
{
timestamp: timestamp,
folder: `easy-email-demo`,
},
process.env.CLOUDINARY_API_SECRET,
);

response.status(200).send({
timestamp,
signature,
cloudName: process.env.CLOUDINARY_NAME,
apiKey: process.env.CLOUDINARY_API_KEY,
});
response.status(200).send({
timestamp,
signature,
cloudName: process.env.CLOUDINARY_NAME,
apiKey: process.env.CLOUDINARY_API_KEY,
});
} catch (error) {
response.status(500).json({ error: 'Failed to generate upload signature' });
}
} else if (request.method === 'PUT') {
// Save image metadata after successful upload
try {
const { url, name, userId } = request.body;
const imageId = uuidv4();

// Save to your database/storage
// This is where you'd save the image metadata to your database
console.log('Saving image metadata:', { imageId, url, name, userId });

response.status(200).json({
success: true,
imageId,
url,
name,
});
} catch (error) {
response.status(500).json({ error: 'Failed to save image metadata' });
}
} else {
response.status(405).json({ error: 'Method not allowed' });
}
};
14 changes: 14 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@

<%- buildTime %>
<title>easy email</title>
<!-- Google Fonts -->
<link
rel="preconnect"
href="https://fonts.googleapis.com"
/>
<link
rel="preconnect"
href="https://fonts.gstatic.com"
crossorigin
/>
<link
href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&family=Open+Sans:wght@400;600&family=Lato:wght@400;700&family=Montserrat:wght@400;500;600&family=Poppins:wght@400;500;600&family=Playfair+Display:wght@400;500;600&family=Merriweather:wght@400;700&family=Source+Sans+Pro:wght@400;600&family=Raleway:wght@400;500;600&family=Ubuntu:wght@400;500;700&display=swap"
rel="stylesheet"
/>
<!-- Google Tag Manager -->
<script>
(function (w, d, s, l, i) {
Expand Down
Loading