-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path+server.js
More file actions
173 lines (148 loc) · 4.49 KB
/
Copy path+server.js
File metadata and controls
173 lines (148 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { json } from '@sveltejs/kit';
import sharp from 'sharp';
import { Octokit } from '@octokit/rest';
import { ACCESS_TOKEN, POSTGRES_URL } from '$env/static/private';
import { Pool } from '@neondatabase/serverless';
const pool = new Pool({
connectionString: POSTGRES_URL
});
const octokit = new Octokit({
auth: ACCESS_TOKEN
});
// Process images sequentially instead of concurrently
async function processAndUploadImage(imageBuffer, size, format, userId, timestamp) {
try {
const resizedImage = await sharp(imageBuffer)
.resize(size, size, { fit: 'inside' })
.toFormat(format)
.toBuffer();
const path = `${userId}/${timestamp}/image_${size}.${format}`;
await octokit.repos.createOrUpdateFileContents({
owner: 'ifsvivek01',
repo: 'CDN',
path,
message: `Upload ${path}`,
content: resizedImage.toString('base64'),
branch: 'main'
});
return true;
} catch (error) {
console.error(`Error processing image size ${size}:`, error);
return false;
}
}
export async function POST({ request }) {
try {
const formData = await request.formData();
const image = formData.get('image');
const format = formData.get('format');
const userId = formData.get('userId');
console.log('Received userId:', userId); // Debug log
if (!image || !format || !userId) {
return json(
{
error: `Missing required fields. Image: ${!!image}, Format: ${!!format}, UserId: ${!!userId}`
},
{ status: 400 }
);
}
if (userId === 'undefined' || !userId) {
return json({ error: 'Invalid user ID' }, { status: 401 });
}
// Check file size (e.g., 10MB limit)
const MAX_SIZE = 10 * 1024 * 1024;
if (image.size > MAX_SIZE) {
return json({ error: 'File too large' }, { status: 400 });
}
const imageBuffer = Buffer.from(await image.arrayBuffer());
const timestamp = Math.floor(Date.now() / 1000);
const sizes = [256, 512, 1024, 2048];
const results = [];
// Process images sequentially
for (const size of sizes) {
const success = await processAndUploadImage(imageBuffer, size, format, userId, timestamp);
results.push(success);
}
// Check if all uploads were successful
if (results.every((result) => result)) {
await pool.query(
'INSERT INTO user_images (user_id, foldername, format) VALUES ($1, $2, $3)',
[userId, timestamp, format]
);
return json({
success: true,
urls: sizes.map(
(size) => `https://cdn.ifsvivek.tech/${userId}/${timestamp}/image_${size}.${format}`
)
});
} else {
return json({ error: 'Some images failed to upload' }, { status: 500 });
}
} catch (error) {
console.error('Upload error:', error);
return json({ error: 'Upload failed' }, { status: 500 });
}
}
export async function GET({ url }) {
try {
const userId = url.searchParams.get('userId');
if (!userId) {
return json({ error: 'User ID required' }, { status: 400 });
}
const result = await pool.query(
'SELECT foldername, format FROM user_images WHERE user_id = $1 ORDER BY foldername DESC',
[userId]
);
return json({ images: result.rows });
} catch (error) {
console.error('Error fetching images:', error);
return json({ error: 'Failed to fetch images' }, { status: 500 });
} finally {
}
}
export async function DELETE({ request }) {
try {
const { userId, timestamp } = await request.json();
// Get format from database first
const result = await pool.query(
'SELECT format FROM user_images WHERE user_id = $1 AND foldername = $2',
[userId, timestamp]
);
if (result.rows.length === 0) {
return json({ error: 'Image not found' }, { status: 404 });
}
const format = result.rows[0].format;
const sizes = [256, 512, 1024, 2048];
// Delete files sequentially
for (const size of sizes) {
const path = `${userId}/${timestamp}/image_${size}.${format}`;
try {
// Get file content first to obtain SHA
const fileData = await octokit.repos.getContent({
owner: 'ifsvivek01',
repo: 'CDN',
path
});
// Delete file using SHA
await octokit.repos.deleteFile({
owner: 'ifsvivek01',
repo: 'CDN',
path,
message: `Delete ${path}`,
sha: fileData.data.sha,
branch: 'main'
});
console.log(`Successfully deleted ${path}`);
} catch (error) {
console.error(`Failed to delete ${path}:`, error);
}
}
await pool.query('DELETE FROM user_images WHERE user_id = $1 AND foldername = $2', [
userId,
timestamp
]);
} catch (error) {
console.error('Delete error:', error);
return json({ error: 'Failed to delete image' }, { status: 500 });
}
}