Replace fix for tiles command

This commit is contained in:
Dennis Postma 2024-10-01 00:30:25 +02:00
parent ce1708a55e
commit 4f643269eb

View File

@ -3,6 +3,7 @@ import sharp from 'sharp'
import { commandLogger } from '../utilities/logger'
import { Server } from 'socket.io'
import { getPublicPath } from '../utilities/utilities'
import path from 'path'
export default class TilesCommand {
constructor(private readonly io: Server) {}
@ -18,6 +19,7 @@ export default class TilesCommand {
}
for (const tile of tiles) {
console.log(getPublicPath('tiles', tile))
// Check if tile is already 66x34
const metadata = await sharp(getPublicPath('tiles', tile)).metadata()
if (metadata.width === 66 && metadata.height === 34) {
@ -26,7 +28,7 @@ export default class TilesCommand {
}
const inputPath = getPublicPath('tiles', tile)
const outputPath = getPublicPath('tiles', tile)
const tempPath = getPublicPath('tiles', `temp_${tile}`)
try {
await sharp(inputPath)
@ -36,14 +38,22 @@ export default class TilesCommand {
fit: 'fill',
kernel: 'nearest'
})
.toFile(outputPath)
.toFile(tempPath)
commandLogger.info(`Processed: ${tile}`)
// Replace original file with processed file
fs.unlinkSync(inputPath)
fs.renameSync(tempPath, inputPath)
commandLogger.info(`Processed and replaced: ${tile}`)
} catch (error) {
console.error(`Error processing ${tile}:`, error)
// Clean up temp file if it exists
if (fs.existsSync(tempPath)) {
fs.unlinkSync(tempPath)
}
}
}
commandLogger.info('Tile processing completed.')
}
}
}