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 { commandLogger } from '../utilities/logger'
import { Server } from 'socket.io' import { Server } from 'socket.io'
import { getPublicPath } from '../utilities/utilities' import { getPublicPath } from '../utilities/utilities'
import path from 'path'
export default class TilesCommand { export default class TilesCommand {
constructor(private readonly io: Server) {} constructor(private readonly io: Server) {}
@ -18,6 +19,7 @@ export default class TilesCommand {
} }
for (const tile of tiles) { for (const tile of tiles) {
console.log(getPublicPath('tiles', tile))
// Check if tile is already 66x34 // Check if tile is already 66x34
const metadata = await sharp(getPublicPath('tiles', tile)).metadata() const metadata = await sharp(getPublicPath('tiles', tile)).metadata()
if (metadata.width === 66 && metadata.height === 34) { if (metadata.width === 66 && metadata.height === 34) {
@ -26,7 +28,7 @@ export default class TilesCommand {
} }
const inputPath = getPublicPath('tiles', tile) const inputPath = getPublicPath('tiles', tile)
const outputPath = getPublicPath('tiles', tile) const tempPath = getPublicPath('tiles', `temp_${tile}`)
try { try {
await sharp(inputPath) await sharp(inputPath)
@ -36,11 +38,19 @@ export default class TilesCommand {
fit: 'fill', fit: 'fill',
kernel: 'nearest' 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) { } catch (error) {
console.error(`Error processing ${tile}:`, error) console.error(`Error processing ${tile}:`, error)
// Clean up temp file if it exists
if (fs.existsSync(tempPath)) {
fs.unlinkSync(tempPath)
}
} }
} }