-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
454 lines (378 loc) · 16.5 KB
/
server.js
File metadata and controls
454 lines (378 loc) · 16.5 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
const express = require('express');
const multer = require('multer');
const sharp = require('sharp');
const { v4: uuidv4 } = require('uuid');
const path = require('path');
const fs = require('fs').promises;
// Configure Sharp for large images
sharp.cache({ files: 0 }); // Disable file cache to save memory
sharp.concurrency(1); // Process images one at a time to save memory
const app = express();
// FIXED: File-based persistent storage
const DATABASE_FILE = './images-database.json';
async function loadImageDatabase() {
try {
const data = await fs.readFile(DATABASE_FILE, 'utf8');
return JSON.parse(data);
} catch (error) {
console.log('No existing database found, starting fresh');
return [];
}
}
async function saveImageDatabase(database) {
try {
await fs.writeFile(DATABASE_FILE, JSON.stringify(database, null, 2));
console.log('Database saved successfully');
} catch (error) {
console.error('Error saving database:', error);
}
}
async function cleanupDatabase() {
console.log('Checking database integrity...');
const cleanedDatabase = [];
for (const image of imageDatabase) {
const tilePath = path.join('tiles', image.id);
try {
await fs.access(tilePath);
cleanedDatabase.push(image);
} catch (error) {
console.log(`Removing missing image from database: ${image.originalName}`);
}
}
if (cleanedDatabase.length !== imageDatabase.length) {
imageDatabase = cleanedDatabase;
await saveImageDatabase(imageDatabase);
console.log(`Database cleaned: ${imageDatabase.length} images remaining`);
}
}
// Initialize database
let imageDatabase = [];
// Set up file upload handling
const upload = multer({
dest: 'uploads/',
limits: {
fileSize: 300 * 1024 * 1024 // 300MB max file size
},
fileFilter: (req, file, cb) => {
// Only allow image files
if (file.mimetype.startsWith('image/')) {
cb(null, true);
} else {
cb(new Error('Only image files are allowed!'));
}
}
});
// Serve static files from public folder
app.use(express.static('public'));
app.use('/tiles', express.static('tiles'));
// FIXED: Function to generate tiles with JPG support and better error handling
async function generateTiles(inputPath, imageId, tileSize = 512) {
console.log(`Starting tile generation for image ${imageId}...`);
try {
// Get metadata from original image with large file support
const metadata = await sharp(inputPath, {
limitInputPixels: false, // Allow large images
sequentialRead: true // Better for large files
}).metadata();
const { width, height } = metadata;
console.log(`Original image size: ${width}x${height}`);
// Calculate how many zoom levels we need, but limit to reasonable number
// For very large images, reduce zoom levels even more
let maxZoom = Math.ceil(Math.log2(Math.max(width, height) / tileSize));
if (width * height > 100000000) { // 100 megapixels
maxZoom = Math.min(5, maxZoom);
console.log('Very large image detected, limiting to 5 zoom levels');
} else {
maxZoom = Math.min(7, maxZoom);
}
// Create output directory
const outputDir = path.join('tiles', imageId);
await fs.mkdir(outputDir, { recursive: true });
console.log(`Will generate ${maxZoom + 1} zoom levels (0 to ${maxZoom})`);
// Start from the highest zoom (original size) and work down for efficiency
let currentBuffer = null;
let currentWidth = width;
let currentHeight = height;
for (let zoom = maxZoom; zoom >= 0; zoom--) {
const zoomDir = path.join(outputDir, zoom.toString());
await fs.mkdir(zoomDir, { recursive: true });
// Calculate size at this zoom level
const scale = Math.pow(2, maxZoom - zoom);
const targetWidth = Math.max(1, Math.floor(width / scale));
const targetHeight = Math.max(1, Math.floor(height / scale));
console.log(`Zoom level ${zoom}: scaling to ${targetWidth}x${targetHeight}`);
// For highest zoom level, process original image
if (zoom === maxZoom) {
try {
currentBuffer = await sharp(inputPath, {
limitInputPixels: false,
sequentialRead: true,
pages: 1 // Only process first page for multi-page images
})
.rotate() // Auto-rotate based on EXIF
.jpeg({ quality: 85, progressive: true })
.toBuffer();
const metadata = await sharp(currentBuffer, {
limitInputPixels: false
}).metadata();
currentWidth = metadata.width;
currentHeight = metadata.height;
} catch (error) {
console.error(`Error processing zoom level ${zoom}:`, error.message);
throw error;
}
} else {
// For lower zoom levels, downscale from previous level (much faster)
currentBuffer = await sharp(currentBuffer, {
limitInputPixels: false
})
.resize(targetWidth, targetHeight, {
kernel: sharp.kernel.lanczos2, // Faster than lanczos3
withoutEnlargement: true,
fit: 'inside'
})
.jpeg({ quality: 85, progressive: true })
.toBuffer();
const metadata = await sharp(currentBuffer).metadata();
currentWidth = metadata.width;
currentHeight = metadata.height;
}
console.log(`Actual dimensions: ${currentWidth}x${currentHeight}`);
// Calculate number of tiles needed
const cols = Math.ceil(currentWidth / tileSize);
const rows = Math.ceil(currentHeight / tileSize);
console.log(`Creating ${cols}x${rows} = ${cols * rows} tiles for zoom level ${zoom}`);
// Cut into tiles with batch processing to reduce memory usage
const tilePromises = [];
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
const left = col * tileSize;
const top = row * tileSize;
const right = Math.min(left + tileSize, currentWidth);
const bottom = Math.min(top + tileSize, currentHeight);
const tileWidth = right - left;
const tileHeight = bottom - top;
// Skip invalid tiles
if (tileWidth <= 0 || tileHeight <= 0) {
continue;
}
const tilePromise = sharp(currentBuffer, {
limitInputPixels: false
})
.extract({
left: Math.max(0, left),
top: Math.max(0, top),
width: tileWidth,
height: tileHeight
})
.jpeg({ quality: 85, progressive: true })
.toFile(path.join(zoomDir, `${col}_${row}.jpg`))
.catch(tileError => {
console.error(`Error creating tile ${col}_${row}:`, tileError.message);
});
tilePromises.push(tilePromise);
// Process tiles in batches of 20 to avoid memory issues
if (tilePromises.length >= 20) {
await Promise.all(tilePromises);
tilePromises.length = 0; // Clear array
// Force garbage collection hint
if (global.gc) {
global.gc();
}
}
}
}
// Process remaining tiles
if (tilePromises.length > 0) {
await Promise.all(tilePromises);
}
// Force garbage collection after each zoom level
if (global.gc) {
global.gc();
}
}
console.log(`Tile generation complete for ${imageId}!`);
return {
width,
height,
tileSize,
maxZoom
};
} catch (error) {
console.error('Error in generateTiles:', error);
throw error;
}
}
// Handle image upload
app.post('/upload', upload.single('image'), async (req, res) => {
try {
if (!req.file) {
return res.status(400).json({ error: 'No file uploaded' });
}
console.log('File uploaded:', req.file.originalname);
console.log('File size:', Math.round(req.file.size / 1024 / 1024 * 100) / 100, 'MB');
console.log('File type:', req.file.mimetype);
// Validate the uploaded file with better error handling for large files
try {
const testImage = sharp(req.file.path, {
limitInputPixels: false, // Allow large images
sequentialRead: true // Better for large files
});
const metadata = await testImage.metadata();
// Additional validation for supported formats
if (!metadata.format || !['jpeg', 'jpg', 'png', 'webp', 'tiff', 'gif'].includes(metadata.format)) {
throw new Error(`Unsupported image format: ${metadata.format}`);
}
console.log('Image metadata:', {
width: metadata.width,
height: metadata.height,
format: metadata.format,
channels: metadata.channels,
density: metadata.density
});
} catch (validationError) {
console.error('Invalid image file:', validationError.message);
return res.status(400).json({ error: 'Invalid or corrupted image file: ' + validationError.message });
}
// Generate unique ID for this image
const imageId = uuidv4();
// Generate tiles
const tileInfo = await generateTiles(req.file.path, imageId);
// Save image information
const imageData = {
id: imageId,
originalName: req.file.originalname,
uploadDate: new Date().toISOString(),
width: tileInfo.width,
height: tileInfo.height,
maxZoom: tileInfo.maxZoom,
tileSize: tileInfo.tileSize,
fileSize: req.file.size
};
imageDatabase.push(imageData);
console.log('Image processed successfully:', imageId);
// FIXED: Save database to file
await saveImageDatabase(imageDatabase);
// Clean up original uploaded file
await fs.unlink(req.file.path);
res.json({
success: true,
imageId: imageId,
imageData: imageData
});
} catch (error) {
console.error('Upload error:', error);
// Clean up uploaded file if it exists
if (req.file && req.file.path) {
try {
await fs.unlink(req.file.path);
} catch (unlinkError) {
console.error('Error cleaning up uploaded file:', unlinkError.message);
}
}
res.status(500).json({ error: 'Failed to process image: ' + error.message });
}
});
// Get list of all uploaded images
app.get('/api/images', (req, res) => {
res.json(imageDatabase);
});
// Get specific image information
app.get('/api/images/:id', (req, res) => {
const image = imageDatabase.find(img => img.id === req.params.id);
if (image) {
res.json(image);
} else {
res.status(404).json({ error: 'Image not found' });
}
});
// Function to adjust brightness and contrast of a tile
async function adjustTileBrightnessContrast(tilePath, brightness = 1.0, contrast = 1.0) {
try {
console.log(`Adjusting tile: ${tilePath}, brightness: ${brightness}, contrast: ${contrast}`);
let pipeline = sharp(tilePath, { limitInputPixels: false });
// Apply brightness and contrast adjustments using linear transformation
// brightness: 1.0 = normal, >1.0 = brighter, <1.0 = darker
// contrast: 1.0 = normal, >1.0 = more contrast, <1.0 = less contrast
if (brightness !== 1.0 || contrast !== 1.0) {
console.log(`Applying transformation: brightness=${brightness}, contrast=${contrast}`);
// Apply brightness using modulate (more reliable)
if (brightness !== 1.0) {
console.log(`Applying brightness: ${brightness}`);
pipeline = pipeline.modulate({ brightness: brightness });
}
// Apply contrast using gamma for better control
if (contrast !== 1.0) {
console.log(`Applying contrast: ${contrast}`);
// Convert contrast to gamma value (inverse relationship)
const gamma = 1.0 / contrast;
pipeline = pipeline.gamma(gamma);
}
console.log('Transformations applied successfully');
}
console.log('Processing tile to buffer...');
const result = await pipeline.jpeg({ quality: 85 }).toBuffer();
console.log(`Successfully processed tile. Buffer size: ${result.length} bytes`);
return result;
} catch (error) {
console.error('Error adjusting tile:', error.message);
console.error('Error details:', error);
throw error;
}
}
// Serve image tiles with optional brightness/contrast adjustment
app.get('/tiles/:imageId/:zoom/:tile', async (req, res) => {
const { imageId, zoom, tile } = req.params;
const { brightness, contrast } = req.query;
// Extract x and y from tile parameter (format: "x_y.jpg")
const match = tile.match(/^(\d+)_(\d+)\.jpg$/);
if (!match) {
return res.status(400).json({ error: 'Invalid tile format' });
}
const [, x, y] = match;
const tilePath = path.join('tiles', imageId, zoom, `${x}_${y}.jpg`);
try {
await fs.access(tilePath);
// If no adjustments requested, serve original file
if (!brightness && !contrast) {
res.sendFile(path.resolve(tilePath));
return;
}
console.log(`Tile adjustment requested: brightness=${brightness}, contrast=${contrast}`);
// Apply brightness/contrast adjustments
const adjustedTile = await adjustTileBrightnessContrast(
tilePath,
parseFloat(brightness) || 1.0,
parseFloat(contrast) || 1.0
);
console.log(`Sending adjusted tile. Size: ${adjustedTile.length} bytes`);
res.set('Content-Type', 'image/jpeg');
res.set('Cache-Control', 'no-cache'); // Prevent caching of adjusted tiles
res.send(adjustedTile);
} catch (error) {
console.log(`Tile not found: ${tilePath}`);
res.status(404).json({ error: 'Tile not found' });
}
});
// Create required directories
async function initializeDirectories() {
try {
await fs.mkdir('uploads', { recursive: true });
await fs.mkdir('tiles', { recursive: true });
console.log('Directories initialized');
} catch (error) {
console.error('Error creating directories:', error);
}
}
// Start server
initializeDirectories().then(async () => {
// Load existing database
imageDatabase = await loadImageDatabase();
console.log(`Loaded ${imageDatabase.length} images from database`);
// Clean up any missing images
await cleanupDatabase();
app.listen(process.env.PORT || 3000, () => {
console.log('Server running on http://localhost:3000');
console.log('Ready to accept image uploads!');
});
});