Fix image convert issue

main
Elbert Alias 3 years ago
parent dca59356be
commit a2577205b9

@ -4,7 +4,7 @@ const { createConverter } = require('convert-svg-to-png')
const terminalOverwrite = require('terminal-overwrite') const terminalOverwrite = require('terminal-overwrite')
// Fix memoryleak warning // Fix memoryleak warning
const maxConvertProcesses = 50 const maxConvertProcesses = 1
process.setMaxListeners(maxConvertProcesses + 1) process.setMaxListeners(maxConvertProcesses + 1)
const appPaths = () => { const appPaths = () => {
@ -71,12 +71,6 @@ function checkIfFile(filePath) {
return fs.statSync(filePath).isFile() return fs.statSync(filePath).isFile()
} }
function diffFiles(fileOne, fileTwo) {
const f1 = fs.readFileSync(fileOne)
const f2 = fs.readFileSync(fileTwo)
return f1.equals(f2)
}
function dateModified(file) { function dateModified(file) {
return fs.statSync(file).mtime return fs.statSync(file).mtime
} }
@ -84,11 +78,9 @@ function dateModified(file) {
function dateDiff(file) { function dateDiff(file) {
const now = new Date().getTime() const now = new Date().getTime()
const then = dateModified(file).getTime() const then = dateModified(file).getTime()
return Math.round(Math.abs((then - now) / 86400000)) return Math.round(Math.abs(((then - now) / 1000) * 60 * 60 * 24))
} }
const converter = createConverter()
;(async () => { ;(async () => {
// Main script // Main script
const files = fs.readdirSync(appPaths().iconPath) const files = fs.readdirSync(appPaths().iconPath)
@ -96,6 +88,8 @@ const converter = createConverter()
const batchNum = Math.ceil(totalFiles / maxConvertProcesses) const batchNum = Math.ceil(totalFiles / maxConvertProcesses)
let batchCount = 1 let batchCount = 1
const converter = createConverter()
do { do {
const percentComplete = `${ const percentComplete = `${
100 - Math.round((100 / totalFiles) * files.length) 100 - Math.round((100 / totalFiles) * files.length)
@ -106,76 +100,67 @@ const converter = createConverter()
await Promise.all( await Promise.all(
files.splice(0, maxConvertProcesses).map(async (fileName) => { files.splice(0, maxConvertProcesses).map(async (fileName) => {
const image = { const path = `${appPaths().iconPath}/${fileName}`
id: fileName, const outputFilePath = getConvertFileName(fileName)
path: `${appPaths().iconPath}/${fileName}`, const ext = getFileExtension(path)
convertPath: getConvertFileName(fileName),
/** if (ext === '.svg') {
* Convert SVG to PNG and copy to new folder. // Check if converted file exists.
*/ if (checkFileExists(outputFilePath)) {
async convertAndCopy() { // Skip if destination file exists and source file hasn't changed in
// 30 days or destination file was created in the last day
const fileAgeA = dateDiff(path)
const fileAgeB = dateDiff(outputFilePath)
if (fileAgeA > 30 || fileAgeB < 1) {
return
}
}
// Convert and copy file.
for (let attempt = 1; attempt <= 3; attempt++) { for (let attempt = 1; attempt <= 3; attempt++) {
try { try {
await converter await converter
.convertFile(this.path, { .convertFile(path, {
height: 32, height: 32,
width: 32, width: 32,
outputFilePath: this.convertPath, outputFilePath,
}) })
.catch((error) => { .catch((error) => {
throw new Error(`${error} (${fileName})`) throw new Error(`${error} (${fileName})`)
}) })
break
} catch (error) { } catch (error) {
if (attempt >= 3) { if (attempt >= 3) {
throw error throw error
} else { } else {
// eslint-disable-next-line no-console
console.error(`${error.message || error} (attempt ${attempt})`)
await new Promise((resolve) => await new Promise((resolve) =>
setTimeout(resolve, 500 * attempt) setTimeout(resolve, 500 * attempt)
) )
} }
} }
break
}
},
async processFile() {
// Setup variables.
const ext = getFileExtension(this.path)
// If SVG, run checks.
if (ext === '.svg') {
// Check if converted file exists.
const convertFilePath = getConvertFileName(this.path)
if (checkFileExists(convertFilePath)) {
// If file has changed in past 30 days.
const fileAge = dateDiff(this.path)
if (fileAge > 30) {
return null
}
} }
// Convert and copy file. } else if (this.ext === '.png') {
await this.convertAndCopy() // If PNG, just copy the file as-is.
} else {
// If PNG or other, just copy the file as-is.
// eslint-disable-next-line no-lonely-if // eslint-disable-next-line no-lonely-if
if (checkIfFile(this.path)) { if (checkIfFile(this.path)) {
copyFiles(this.path, this.convertPath) copyFiles(this.path, this.convertPath)
} }
} }
},
}
await image.processFile()
}) })
) )
batchCount++ batchCount++
} while (files.length) } while (files.length)
await converter.destroy() await converter.destroy()
// eslint-disable-next-line no-console
console.log(`Converted ${totalFiles.toLocaleString()} files.`) console.log(`Converted ${totalFiles.toLocaleString()} files.`)
process.exit()
})() })()
/** /**