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
for (let attempt = 1; attempt <= 3; attempt++) { // 30 days or destination file was created in the last day
try { const fileAgeA = dateDiff(path)
await converter const fileAgeB = dateDiff(outputFilePath)
.convertFile(this.path, {
height: 32, if (fileAgeA > 30 || fileAgeB < 1) {
width: 32, return
outputFilePath: this.convertPath, }
}) }
.catch((error) => {
throw new Error(`${error} (${fileName})`) // Convert and copy file.
}) for (let attempt = 1; attempt <= 3; attempt++) {
} catch (error) { try {
if (attempt >= 3) { await converter
throw error .convertFile(path, {
} else { height: 32,
await new Promise((resolve) => width: 32,
setTimeout(resolve, 500 * attempt) outputFilePath,
) })
} .catch((error) => {
} throw new Error(`${error} (${fileName})`)
})
break break
} } catch (error) {
}, if (attempt >= 3) {
async processFile() { throw error
// Setup variables. } else {
const ext = getFileExtension(this.path) // eslint-disable-next-line no-console
console.error(`${error.message || error} (attempt ${attempt})`)
// If SVG, run checks.
if (ext === '.svg') { await new Promise((resolve) =>
// Check if converted file exists. setTimeout(resolve, 500 * attempt)
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.
await this.convertAndCopy()
} else {
// If PNG or other, just copy the file as-is.
// eslint-disable-next-line no-lonely-if
if (checkIfFile(this.path)) {
copyFiles(this.path, this.convertPath)
} }
} }
}, }
} else if (this.ext === '.png') {
// If PNG, just copy the file as-is.
// eslint-disable-next-line no-lonely-if
if (checkIfFile(this.path)) {
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()
})() })()
/** /**